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: Painted component won't show up

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Painted component won't show up

    Hello, I wonder why my oval doesn't show up on the frame.


    Class with the oval

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
     
    public class character extends JPanel
    {
     
        public character ()         
        {
            this.setBackground(Color.cyan);
     
        }
     
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponents(g);
            g.setColor(Color.red);
            g.fillOval(67, 150, 100, 100);       
     
        }
     
    }

    Class supposed to show oval

    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class spelplan extends JPanel
    {
     
        character player1;
     
        public spelplan()
        {
     
            player1 = new character();
            this.setBackground(Color.white);
            add(player1);
        }
     
        public static void main(String[] args) 
        {
            JFrame f = new JFrame();
            f.setSize(500,500);
            f.setLocation(200,200);
            f.setTitle("RogueTest");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            spelplan p = new spelplan();
            f.add(p);
            f.setVisible(true);
        }
     
     
    }//class

    I'm sure there is a simple solution but I can't see whats wrong. I even checked in my java-book and I can't find it out O.o


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Painted component won't show up

    Some suggestions:
    Add a getPreferredSize() method to the panel.
    Play with the values in the fillOval() method.

    Change class names to start with Cap letter and not conflict with Java class names.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Painted component won't show up

    Quote Originally Posted by Norm View Post
    Some suggestions:
    Add a getPreferredSize() method to the panel.
    Play with the values in the fillOval() method.

    Change class names to start with Cap letter and not conflict with Java class names.
    Thank you very much sir. Start to get the thing with the drawing now

Similar Threads

  1. Show files name in JFrame
    By altisw5 in forum AWT / Java Swing
    Replies: 15
    Last Post: November 30th, 2011, 08:30 AM
  2. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  3. Applet doesnt show
    By chonch in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 03:22 PM
  4. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  5. Show Text With cursor
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2010, 10:02 AM