Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 29-04-2009, 11:43 PM
Junior Member
 

Join Date: Apr 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Richard_ is on a distinguished road
Question Java GUI problem - paintComponent?

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");
	}

}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 01-05-2009, 09:05 AM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,585
Thanks: 104
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Mellow
Default Re: Java GUI problem - paintComponent?

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");
    }
"Cannot make a static reference to a non static method repaint() from the type Component"

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.
Reply With Quote
  #3 (permalink)  
Old 01-05-2009, 01:19 PM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
Default Re: Java GUI problem - paintComponent?

ImageViewer.repaint();

What is it you hope to redraw when you call this? Becuase as far as I am aware, that is COMPLETELY Illegal. The only way to call repaint is on an instance not class, ie as a static function.

repaint is definded as
public void repaint();

not,
public static void repaint();

thus it cannot be called in a static manner.
This is confusing to explain lol. One last try,

You cannot call a non-static function on a class, you must call it on an instance of a class.

Regards,
Chris
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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

All times are GMT. The time now is 09:31 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.