Jump to content
  • Advertisement
Sign in to follow this  
Bloodtake

Help with my Image Loading

This topic is 2609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Im new to this forum and I was wondering if someone could help me? In my project I want my map (background image) to load, however It does not seem to do it at all and I have used mostly every single way. I you can see what my problem is could you show a correction :) . It is based in two classes!


This is the fist one and my main class!



package com.bloodtake.quest;




import java.awt.Canvas;
import java.awt.Dimension;
import
java.awt.Graphics;




import javax.swing.JFrame;




import com.bloodtake.quest.graphics.Screen;
import
com.bloodtake.quest.world.*;




public class QuestComponent extends Canvas implements Runnable
{
private static final long serialVersionUID =
1L;

private static final int WIDTH = 160;
private
static final int HIGHT = 120;
private static final int SCALE = 4;




private static final Graphics Graphics = null;
boolean
screenLoad = false;

private boolean running;
private
Thread thread;

private Game game;
private Screen
screen;

public QuestComponent() {
Dimension
size = new Dimension(WIDTH*SCALE,
HIGHT*SCALE);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);

game
= new Game();
screen = new Screen(WIDTH,
HIGHT);





}

public synchronized void
start(){
if (running) return;
running =
true;
new
Thread(this).start();
thread.start();

}

public
synchronized void stop(){
if (running)
return;
running = false;
try
{
thread.join();
} catch
(InterruptedException e)
{
e.printStackTrace();
}

}

public
void run(){
while (running)
{
tick();
render();
Map.printMap();
}
}







private void tick()
{
game.tick();

}
private void
render() {
screen.render(game);

}





public static void main(String[] args)
{
QuestComponent game = new
QuestComponent();

JFrame frame = new
JFrame("Quest: A Demo
Unleashed");
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
boolean
screenLoad = true;
if(screenLoad = true)
{
System.out.println("Screen Successfully
Loaded!");

game.start();

}

}





}

[/quote]

This is my class that loads my map :)

package com.bloodtake.quest.world;

// Imports
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.JFrame;

// Game Imports


public abstract class Map extends JFrame {

private static final long serialVersionUID = 1L;

public static BufferedImage map;

public void init(){
try{
map = ImageIO.read( getClass().getResource("A1.png"));
}
catch(IOException e) {
System.out.println("Load Image error:");
}
}


public static void printMap(){
System.out.println("Loading map...");

// This is the section where the image (A1.png) needs to print so in my main class is can load.

//known as Map.printMap()



}


}
[/quote]

Share this post


Link to post
Share on other sites
Advertisement
I don't know java very much, but my first quess is that "A1.png" as input for getClass().getResource() is too less. Try passing the whole directory, like "C:\Pictures\A1.png".

Share this post


Link to post
Share on other sites
First, when posting code in the forums use "code" tags, so it will ident the code and make it easier for reading.

As for your issue, I can't help you much directly with your code, however I can suggest you a way to do this:

1) Create a class that inherits from JPanel. This class could have name "ImagePanel" or "LoadImage", because it will be responsible for loading and painting your image.
2) A JPanel have the method void paint(Graphics g). We are going to overwrite it so that even when we don't have an image loaded, the panel can be drawn on screen without problems.
3) I then create a function to load the image that I want (get the absolute path to that), and a new function for drawing. This new drawing function will be able to draw objects of the type Graphics2D

An example is the following:

public class ImagePanel extends JPanel
{
private String filepath;
private BufferedImage img;

public ImagePanel()
{
this.setBackground(Color.white);
}
public ImagePanel(String filepath)
{
this.setBackground(Color.white);
this.filepath = filepath;
}

public void changePicture(String filepath)
{
this.filepath = filepath;
}

public void loadPicture()
{
try
{
img = ImageIO.read(new File(filepath));
}
catch(IOException ex)
{
ex.printStackTrace();
}
}

public void drawPicture()
{
Graphics2D g2D;

g2D = (Graphics2D)this.getGraphics();
g2D.drawImage(img, 0, -30, null);
}

public void cleanPanel(Color c)
{
Graphics2D g2D;

g2D = (Graphics2D)this.getGraphics();

g2D.setColor(c);
g2D.fillRect(this.getX()-20, this.getY()-130, this.getWidth()+20, this.getHeight()+120);
}

@Override
public void paint(Graphics g)
{
super.paint(g);

g.drawString("<No Picture>", 0, 0);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!