Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Norm

Thread: Paintcomponent - Looping through classes

  1. #1
    musico is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default Paintcomponent - Looping through classes

    I am experimenting with the paintcomponent feature for multiple objects of a kind, in this case circles. Yes it compiles and gives the desired output, but I am convinced there is a more satisfactory way. Code so far

    Java Code:
    Canvas
    
    package paint;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.ArrayList;
    
    public class Canvas {
    
    	public static void main (String[] args)
    	{
    		JFrame f = new JFrame("Paintcomponent");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Paint p = new Paint();
    		f.add(p);
    		f.setLocation(50,50);
    		f.setSize(900,700);
    		f.setVisible(true);
    	}
    }
    Java Code:
    Paint
    
    package paint;
    
    import java.awt.*;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class Paint extends JPanel{
    
    	public void setCircles()
    	{
    		Circle c1 = new Circle(40, 100, 400, Color.blue);
    		Circle c2 = new Circle(40, 200, 350, Color.green);
    		Circle c3 = new Circle(30, 300, 300, Color.pink);
    		Circle c4 = new Circle(40, 400, 250, Color.yellow);
    		Circle c5 = new Circle(50, 500, 200, Color.cyan);
    		Circle c6 = new Circle(40, 100, 300, Color.blue);
    		Circle c7 = new Circle(40, 200, 250, Color.red);
    		Circle c8 = new Circle(30, 300, 200, Color.magenta);
    		Circle c9 = new Circle(40, 400, 150, Color.yellow);
    		Circle c10 = new Circle(50, 500, 100, Color.cyan);
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		setCircles();
    		
    		super.paintComponent(g);
    		this.setBackground(Color.black);
    
    		for (int i=0;i<10;i++){
    			g.setColor(Circle.getCircle(i).getColor());
    			g.fillOval(Circle.getCircle(i).getX(), Circle.getCircle(i).getY(),   Circle.getCircle(i).getDiameter(), Circle.getCircle(i).getDiameter());
    		}
    	}
    }
    Java Code:
    Circle
    
    package paint;
    
    import java.awt.Color;
    import java.util.ArrayList;
    
    public class Circle {
    
    	int diameter = 0;
    	int xPos;
    	int yPos;
    	Color col;
    	static ArrayList<Circle> myList = new ArrayList<Circle>();
    	
    	public Circle(int d, int x, int y, Color c)
    	{
    		myList.add(this);
    		diameter = d;
    		xPos = x;
    		yPos = y;
    		col = c;
    	}
    	
    	public static Circle getCircle(int a)
    	{
    		return myList.get(a);
    	}
    	
    	public int getDiameter()
    	{
    		return diameter;
    	}
    	
    	public int getX()
    	{
    		return xPos;
    	}
    	
    	public int getY()
    	{
    		return yPos;
    	}
    	
    	public Color getColor()
    	{
    		return col;
    	}
    }
    My first issue is with the for loop, having tried and failed to use a so called 'enhanced' loop. I was aiming at something like

    for each instance of the circle class
    g.setcolor of this circle
    g.filloval with relevant parameters of this circle

    Also I was trying to implement the painting method from within the circle class itself rather than dragging the parameters out to the Paint class, especially as my next target is thread based animation. Can't find any examples close enough to this.

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

    Default Re: Paintcomponent - Looping through classes

    Some suggestions:

    • I think that Circle should not hold a static ArrayList of Circle objects as you're asking this class to do too much and is also limiting. What if you want to use an array of Circles or a HashSet? You've still got this unnecessary list wasting resources.
    • Instead have it just represent the concept of Circle and Circle only -- a color, diameter, and position.
    • Have the classes that use Circle, here your "Paint" class, create and maintain a non-static collection if they need one.
    • Then you can iterate through this collection using an enhanced for-each loop if desired.
    • A small nit-pick: consider changing the "Paint" class to some other name as Paint is the name of a core class in Java Graphics that is not infrequently used when drawing in Swing, and your class name might confuse some.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Paintcomponent - Looping through classes

    Your setCircles method should use an array to hold the Circle objects, not individual variables.
    You should use not use a Java SE class's name: Paint for your class name.
    You should not call setCircles() every time the paintComponent method is called. Do it once.

    The use of static methods and variables is a poor technique.
    Fubarable likes this.

  4. #4
    musico is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default Re: Paintcomponent - Looping through classes

    Thanks both. I have changed the Paint class to Render and got rid of static variables. Will post updated code when I'm happy with it.

    Not into Hashset stuff yet but thanks for the heads up.

Similar Threads

  1. paintComponent
    By zock70 in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2011, 03:19 AM
  2. geting value from paintComponent
    By gedas in forum New To Java
    Replies: 3
    Last Post: 03-21-2011, 07:56 PM
  3. Help with paintComponent!
    By joeyea in forum Java 2D
    Replies: 6
    Last Post: 12-27-2010, 01:59 PM
  4. paintComponent vs paintComponents
    By alacn in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 03:33 AM
  5. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 04: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
  •