Following is the code I have so far:
All the imports are correct. I’m sure. 😀
When I run the program, all I get is a blank frame, without the picture. It should show up.
public class WindowPractice extends JFrame {
final static int width= 800;
final static int height= 400;
int x;
int y;
Image steve;
Dimension gamesize= new Dimension (width, height);
public WindowPractice(){
setTitle ("Hangman");
setSize (gamesize);
setVisible (true);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
new WindowPractice();
ImageIcon steve= new ImageIcon ("Libraries/Pictures/ba190cd951302bcebdf216239e156a4.jpg");
JLabel imageLabel = new JLabel(steve);
}
public void paint(Graphics g){
g.setColor(Color.red);
//g.fillRect( x, y, 100, 20);
g.drawImage(steve, x, y,this);
x= 150;
y= 250;
}
}
There are so many things wrong with this I’m not sure where to start…
Let’s start at the beginning…
Problem #1
You declare a instance field called
stevein youWindowPracticeclass, this is fine, but in your main method, you declare ANOTHER variable calledstevewhich you are using the reference to the loaded image…This means that the class instance variable is never initialised and remains
null.Problem #2
While not directly related, you never call
super.paintfrom yourpaintmethod. This is a BIG NO, NO. You are obligated to maintain the paint chain. The paint methods are complex and very, very important.Problem #3
You should never override a top level container (such as
JFrame) nor you should you override any of it’spaintmethods. There are lots of reasons for this, but among the top two are, most top level containers actually contain a number of components (JRootPane, which houses the glass pane, content pane, layer pane and menu bar) which can sit over your painting efforts and, generally, they’re not double buffered, meaning you paint updates will flicker and look horrible 😉You should also avoid using
paint, instead, you should look towards usingpaintComponentwhere it’s available.Problem #4
ImageIconis not you best choice for loading images. The main reason I don’t use them is that you have no idea of when the image being loaded will actually become available (actually there are ways, but to be frank,ImageIOis just simpler). This was a great feature back in 1999 when dial-up speeds where around the 14.4k mark, but now days…ImageIOsupports a wider range of picture formats, supports reading and writing of images and guarantees that when the method returns (successfully), the image pixel data is available to your application.Example
Here’s a better (IMHO) approach…
Have a read of
For some more information.