|
||
|
|||
|
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.Java 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");
}
}
|
|
||||
|
Hello Richard,
When I attempt to compile this in Eclipse, I straight away get an error here in the MainScreen class: Java Code
protected void paintComponent(Graphics g) {
ImageViewer.repaint();
System.out.println("The main screen is redrawn");
}
I'm looking into how to fix this..
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [SOLVED] Abstract problem in java using vista | Uzual | AWT / Java Swing | 2 | 26-05-2009 04:23 PM |
| [SOLVED] JAVA JList Problem | antitru5t | AWT / Java Swing | 4 | 15-04-2009 08:09 PM |
|
100 most searched terms
Search Cloud
|
| action listener in java actionlistener actionlistener in java addactionlistener addactionlister arraylist value into hash buffered read two integer value in java create an abstract class called shape with abstract methods+java double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread main java.lang.outofmemoryerror java heap space in eclipse format double java get files from folder java by threads get mouse position java how to convert list to map in java how to format doubles in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/collections-generics/2688-grade-array-trouble.html http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java coding - user entered input java deallocate java double format java double to int java font attributes java format double java forum java forums java get mouse position java jbutton java nextline() java program to find dimensions of a room java programming codes using astirisks java programming forum java programming forums java.lang.outofmemoryerror: java heap space java.lang.reflect.invocationtargetexception jbutton actionlistener jtable questions in java jtext bold jxl.read.biff.biffexception: unable to recognize ole stream mean value decimal double java oops java assignments programmer forum smack api messagelistener transaction using gui and 2 dimensional array code in java two dimensional arraylist in java |