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 4 of 4

Thread: JPanel drawing question

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanel drawing question

    so heres what i have

    public class MyPanel extends JPanel { 
     
       public MyPanel(){ 
          setSize(100, 250); 
          setLocation(0, 0); 
          setOpaque(true); 
          this.setForeground(new Color(100,100,100)); 
          //setVisible(true); 
       } 
       public void paintComponents(Graphics g) 
       { 
          super.paintComponent(g); 
          g.fillRect(3, 5,100, 100); 
     
       } 
       public static void main(String [] args) 
       { 
          JFrame f = new JFrame("Frame"); 
                    f.setSize(500, 500); 
          f.setVisible(true); 
          f.getContentPane().add(new MyPanel()); 
          f.show(); 
       } 
     
     
    }


    when i run it i get a blank JFrame screen.........is there something i missed or did wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JPanel drawing question

    Are you sure your paint method is being called? Add a println to see.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JPanel drawing question

    Yeah, a few notes:
    • paintComponents() isn't completely right, as It's actually paintComponent()
    • You're using a lot of unnecessary code
    • f.show() is depreciated, stick to setVisible()
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel drawing question

    it works now......thanks alot

Similar Threads

  1. drawing a BufferedImage onto a JPanel
    By nemo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2010, 07:48 PM
  2. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 03:07 PM
  3. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  4. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM