|
Java GUI problem - paintComponent problem?
|
| View previous topic :: View next topic |
| Author |
Message |
Richard Guest Programmer
|
Hi everyone. I can't seem to get my java code to work. I suspect it's a problem with the way I've coded my paintComponent methods, but for the life of me I can't figure out where the problem is.
Instead of drawing a pretty picture on screen, all I get is a vast expanse of nothing. Any/all input would be greatly appreciated Oh, and yes - the image file is definitely in the correct directory.
| Code: |
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrontEnd extends JFrame {
public FrontEnd() {
Image miniIcon = new ImageIcon("icon.gif").getImage();
this.setIconImage(miniIcon);
MainScreen mainScr = new MainScreen();
add(mainScr);
mainScr.repaint();
}
public static void main(String args[]) {
FrontEnd window = new FrontEnd();
window.setTitle("Blah");
window.setSize(400, 400);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
//#####################################################################################################
class MainScreen extends JPanel {
public MainScreen() {
JPanel p = new JPanel(new GridLayout(1, 1, 0, 0));
System.out.println("Container panel created");
Image image = new ImageIcon("icon.gif").getImage();
ImageViewer viewPanel = new ImageViewer(image);
add(viewPanel);
viewPanel.repaint();
}
protected void paintComponent(Graphics g) {
ImageViewer.repaint();
System.out.println("The main screen is redrawn");
}
}
//#####################################################################################################
class ImageViewer extends JPanel {
private java.awt.Image image;
public ImageViewer(Image image) {
this.image = image;
System.out.println("ImageViewer constructor called");
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
System.out.println("ImageViewer paintComponent called");
}
}
|
Reply with quote
|
| |
|
|
devunion New Programmer
Joined: 04 Feb 2009 Posts: 8
| |
|