Results 1 to 4 of 4
  1. #1
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default Graphics wont show up(paintComponent)

    i followed a tutorial , and every thing seems to be written right , but when i run it the graphics wont show up.
    Java Code:
    import javax.swing.*;
    
    
    public class Game {
    	
    	public
    		static int i , j;
    		static final int LEFT = 0;
    		static final int UP = 1;
    		static final int RIGHT = 2;
    		static final int DOWN = 3;
    		static Entity ent1 = new Entity();
    		static World map1 = new World();
    	
    	public static void main(String[] args) {
    		final JFrame f = new JFrame("Title");
    		final GraphDrawer gd = new GraphDrawer();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.add(gd);
    		f.setSize(640,420);
    		f.setVisible(true);
    	}
    	
    }
    Java Code:
    import java.awt.*;
    import java.awt.Event.*;
    import javax.swing.*;
     
    
    
    public class GraphDrawer extends JPanel {
    
    	public void paintComponet(Graphics g)
    	{
    		super.paintComponent(g);
    		this.setBackground(Color.BLACK);
    		g.setColor(new Color(143,46,56));
    		g.fillRect(0,0,50,52);
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your GraphDrawer is not overriding the paintComponent method of JPanel. Spelling matters. To find out if a method is truly overriding a super method, place the @Override annotation just above the menu, and the compiler will tell you:

    Java Code:
    class GraphDrawer extends JPanel {
    
       @Override // shows an error due to misspelling
       public void paintComponet(Graphics g) {
          super.paintComponent(g);
          this.setBackground(Color.BLACK);
          g.setColor(new Color(143, 46, 56));
          g.fillRect(0, 0, 50, 52);
       }
    }

    Also, you don't want to call setBackground from within the paintComponent method as by doing this it gets called repeatedly and unnecessarily. Better to put this in the class's constructor:

    Java Code:
    class GraphDrawer extends JPanel {
    
       public GraphDrawer() {
          setBackground(Color.black);
       }
       
       @Override
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          //this.setBackground(Color.BLACK);
          g.setColor(new Color(143, 46, 56));
          g.fillRect(0, 0, 50, 52);
    
       }
    }

  3. #3
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your GraphDrawer is not overriding the paintComponent method of JPanel. Spelling matters. To find out if a method is truly overriding a super method, place the @Override annotation just above the menu, and the compiler will tell you:

    Java Code:
    class GraphDrawer extends JPanel {
    
       @Override // shows an error due to misspelling
       public void paintComponet(Graphics g) {
          super.paintComponent(g);
          this.setBackground(Color.BLACK);
          g.setColor(new Color(143, 46, 56));
          g.fillRect(0, 0, 50, 52);
       }
    }

    Also, you don't want to call setBackground from within the paintComponent method as by doing this it gets called repeatedly and unnecessarily. Better to put this in the class's constructor:

    Java Code:
    class GraphDrawer extends JPanel {
    
       public GraphDrawer() {
          setBackground(Color.black);
       }
       
       @Override
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          //this.setBackground(Color.BLACK);
          g.setColor(new Color(143, 46, 56));
          g.fillRect(0, 0, 50, 52);
    
       }
    }
    thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. wont show entire JLabel
    By Billaguana in forum AWT / Swing
    Replies: 6
    Last Post: 02-03-2011, 11:18 PM
  2. Help with paintComponent!
    By joeyea in forum Java 2D
    Replies: 6
    Last Post: 12-27-2010, 02:59 PM
  3. Replies: 3
    Last Post: 05-27-2010, 11:54 AM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 07:20 AM
  5. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 05:59 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
  •