Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Really Easy Paintcomponent Question

setpreferredsize

  • Please log in to reply
2 replies to this topic

#1 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 07 July 2012 - 09:38 AM

I'm making a program that displays 50 random shapes on the screen. The shapes are either circles or squares. For some reason, the program only displays one shape at a time, instead of the whole 50. Can anyone help me figure out why?

Thanks

here is my code:
import javax.swing.JPanel;
import java.util.Random;
import java.awt.*;

public class NervousShapesPanel extends JPanel{

    Shape[] shapes;
    Random r;
    int width, height;
    
    public NervousShapesPanel(){
        shapes = new Shape[50];
        r = new Random();
        height = 200;
        width = 200;
        setPreferredSize(new Dimension(width, height));
    }
    
    public void paintComponent(Graphics g){
        int shape = r.nextInt(2);
        int radius = r.nextInt(11) + 10;
        int w = r.nextInt(11) + 10;
        int h = r.nextInt(11) + 10;
        int cX = r.nextInt(width-radius);
        int cY = r.nextInt(height-radius);
        int rX = r.nextInt(width-w);
        int rY = r.nextInt(height-h);
        Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        //initialize the shape
        for(int i = 0; i < shapes.length; i++){
            shapes[i] = (shape == 0) ? new Circle(cX, cY, c, radius) : new Rectangle(rX, rY, c, h, w);
            shapes[i].draw(g);
        }
    }
    
}

the shape class, as well as it's subclasses Circle and Rectangle were made by me. They both extend Shape, so they're both exactly similar except that circle has a radius variable, and rectangle has a width and height variable. Because they're nearly identical, I'll just show you circle:
import java.awt.*;
public class Circle extends Shape{
private int radius;

public Circle(int x, int y, Color color, int radius){
super(x, y, color);
this.radius=radius;
}
@Override
public int getHeight() {
return radius;
}
@Override
public int getWidth() {
return radius;
}

@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x, y, radius, radius);
}
}

thanks in advance!
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 07 July 2012 - 10:07 AM

For some reason, the program only displays one shape at a time, instead of the whole 50.


You most certainly are displaying all 50 shapes, but you've made all 50 shapes look completely identical to one another. Take a look at your code:

public void paintComponent(Graphics g){
int shape = r.nextInt(2);
int radius = r.nextInt(11) + 10;
int w = r.nextInt(11) + 10;
int h = r.nextInt(11) + 10;
int cX = r.nextInt(width-radius);
int cY = r.nextInt(height-radius);
int rX = r.nextInt(width-w);
int rY = r.nextInt(height-h);
Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
//initialize the shape
for(int i = 0; i < shapes.length; i++){
shapes[i] = (shape == 0) ? new Circle(cX, cY, c, radius) : new Rectangle(rX, rY, c, h, w);
shapes[i].draw(g);
}
}


All of the variables that determine the parameters of the shape are initialized outsize of the for loop. That means they get initialized once and they stay that way. If you want each shape to look different, you'll have to change the values of these variables with each loop iteration.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 07 July 2012 - 11:16 AM

hahahahahah!!! I can't believe I made that mistake.

Thank you, greg.
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download