trying to get an image to print into a window. Everything runs without errors, and it also works if I replace the drawImage with another graphics class. However, the window is missing the image, and i’m not sure why. Again, the JFrame stuff and Graphics work fine with drawing other graphics, but only doesn’t draw the image here. Thanks.
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class GraphicsMovement2 extends JApplet{
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
JApplet example = new GraphicsMovement2();
JFrame frame = new JFrame("Movement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(example);
frame.setSize(new Dimension(1366,768)); //Sets the dimensions of panel to appear when run
frame.setVisible(true);
}
public void paint (Graphics page){
page.drawImage(image, 100, 100, 100, 100, Color.RED, this);
}
}
You’ve defined
imagetwice…This essentially means that by the time you get to the
paintmethod, it isnullas you haven’t initialized the instance variable.Another problem you will have is the fact that you are trying to load the image from a static reference but the
imageisn’t declared asstatic. Better to move this logic into the constructor or instance method.Don’t use
JAppletas your container when you’re adding to aJFrame, you’re better of using something likeJPanel. It will help when it comes to adding things to the container.YOU MUST CALL
super.paint(g)…in fact, DON’T override thepaintmethod of top level containers likeJFrameorJApplet. Use something likeJPaneland override thepaintComponentmethod instead. Top level containers aren’t double buffered.The
paintmethods does a lot of important work and it’s just easier to useJComponent#paintComponent… but don’t forget to callsuper.paintComponentUPDATED
You need to define
imagewithin the context it is going to be used.Because you declared the
imageas an instance field ofGraphicsMovement2, you will require an instance ofGraphicsMovement2in order to reference it.However, in you
mainmethod, which isstatic, you also declared a variable namedimage.The
paintmethod ofGraphicsMovement2can’t see the variable you declared inmain, only the instance field (which isnull).In order to fix the problem, you need to move the loading of the image into the context of a instance of
GraphicsMovement2, this can be best achived (in your context), but moving the image loading into the constructor ofGraphicsMovement2The two examples below will produce the same result…
The Easy Way
The Hard Way
Take the time to read through the tutorials