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

Thread: paint only paints one obj

  1. #1
    Member
    Join Date
    May 2011
    Posts
    61
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default paint only paints one obj

    I'm just starting Java Swing and I want to create circles so I have a class that draws only circles and a constructor to set size and starting point, then in main, I create a new JFrame and add this obj. It's working, but when I try to add more circle objs to my JFrame, it doesn't show up. I know it's trivial...

    import javax.swing.*;
    import java.awt.*;
     
    public class Circle extends JPanel
    {
      private int starting_x;
      private int starting_y;
      private int width;
      private int height;
     
       public Circle(int x, int y,int w, int h)
       {
          this.starting_x = x;
          this.starting_y = y;
          this.width = w;
          this.height = h;
       }
     
      public void paintComponent(Graphics g)
      {
    	super.paintComponent(g);
     
    	Graphics2D g2d = (Graphics2D) g;
     
    	g2d.setColor(Color.black);
    	g2d.fillOval(start_x-1, start_y-1, width+2, height+2);
     
       }
    }

    //main in another file
    import javax.swing.*;
     
    public class Runner
    {
       public static void main(String[] args)
       {
          JFrame f = new JFrame();
          f.add( new Circle(100,100,34,34) );//this shows
          f.add( new Circle(300,305,34,34) );//this doesn't show though
          f.setVisible(true);
          f.setSize(1000,1000);  
       }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: paint only paints one obj

    You don't want to have multiple JPanels drawing single things at a time- you want to have a single JPanel drawing multiple things at a time.

    Think of a JPanel as a canvas. What you're doing is hanging up a canvas, drawing a circle on it, then hanging up another canvas right on top of the first one and drawing a circle on it- see why you can only see one circle at a time?

    (Technically I think that might be backwards- you might only be seeing the first thing you add, but the idea is the same.)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. new to forums help needed in paint program
    By saifbaeg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 29th, 2012, 07:14 AM
  2. [SOLVED] JScrollPane cant paint correctly when scroll
    By fuweng in forum AWT / Java Swing
    Replies: 4
    Last Post: December 6th, 2011, 01:27 PM
  3. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  4. Paint only part of screen
    By bonus_clip in forum AWT / Java Swing
    Replies: 7
    Last Post: October 16th, 2010, 07:21 PM
  5. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM