Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: graphics won't work

  1. #1
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default graphics won't work

    I followed a youtuber called TheNewBoston.
    The problem is that it just gives me a greyish screen, and I have no idea what the problem is...
    here is the code I used.

    class Main(the main class!)
    Java Code:
    package gui.basicGraphic;
    
    import javax.swing.*;
    
    public class Main {
    	public static void main(String[] args) {
    		
    		JFrame f = new JFrame("the title");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		DrawGraphic dg = new DrawGraphic();
    		f.add(dg);
    		f.setSize(400, 250);
    		f.setVisible(true);
    	}
    }
    and my second class.

    class DrawGraphic(the class with the graphics stuff!)
    Java Code:
    package gui.basicGraphic;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DrawGraphic extends JPanel{
    	
    	public void PaintComponent(Graphics g){
    		super.paintComponents(g);
    		this.setBackground(Color.WHITE);
    		
    		g.setColor(Color.BLUE);
    		g.fillRect(25, 25, 100, 30);
    		
    		g.setColor(new Color(190,81,215));
    		g.fillRect(25, 65, 100, 30);
    		
    		g.setColor(Color.RED);
    		g.drawString("this is some tecxt", 25, 120);
    	}
    }
    I hope someone know a solution to this :): cause I want to make GUI stuff, but if it doesn't work I have to do something else... :/

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: graphics won't work

    Java is case sensitive - check the case of your methods...the @Override annotation helps you verify you have not misspelled a method you are intending to override

    Java Code:
    @Override
    public void PaintComponent(Graphics g){//compile time error
    ...
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: graphics won't work

    it just tells me that the @Override has to go away, and it didn't fix anything.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: graphics won't work

    The method 'paintComponent' is with a lower case 'p'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: graphics won't work

    Thanks that worked!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: graphics won't work

    Quote Originally Posted by HelloWorld1234 View Post
    Thanks that worked!
    Hold on for a second: that was the reason your compiler protested at the @Override annotation because PaintComponent( ... ) didn't override paintComponent( ... ); see why it is a handy tool?

    kind regards,

    Jos
    doWhile likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

Similar Threads

  1. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  2. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  3. How to use graphics
    By Mattedatten in forum New To Java
    Replies: 3
    Last Post: 03-18-2010, 02:10 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •