Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java GUI problem in paintComponent?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java GUI problem in 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.

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


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    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:

        protected void paintComponent(Graphics g) {
            [B]ImageViewer.repaint();
    [/B]        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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    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

Similar Threads

  1. [SOLVED] Error "TitleChanger is abstract; cannot be instantiated"
    By Uzual in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2009, 11:23 AM
  2. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM