简体   繁体   中英

How to add multiple objects to JComponent?

This is my tester class:

public void start() {
    // We do our drawing here       
    JFrame frame = new JFrame("Animation");
    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));


    frame.setVisible(true);
}

Shape1 class:

public class Shape1 extends JComponent{
    protected double x, y, r;
    protected double height, width;
    protected Color col;
    protected int counter;

    public Shape1(double x, double y, double r) {
        this.x = x - 2*r;
        this.y = y - r;
        this.r = r;
        this.width = 4*r;
        this.height = 2*r;

        this.col = new Color((int)(Math.random() * 0x1000000));
    }

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        draw(g2);
    }

    public void draw(Graphics2D g2){
        Ellipse2D.Double face = new Ellipse2D.Double(this.x, this.y, this.width, this.height);

        g2.setColor(this.col);
        g2.fill(face);
    }
}

I'm instantiating the Shape1 class 3 times and adding them to the frame. But the shape is drawn only once, how can I draw it 3 times?

You can try to use a loop:

List<Shape1> shapes = new ArrayList<>();

@Override
protected void paintComponent(Graphics g) {
    super.paintCompoent(g);
    for (Shape1 s : shapes) {
        s.draw(g);
    }
}

JFrame is using a BorderLayout by default, which means only the last component is placed in the default/ CENTER position.

Start by changing the layout manager.

public void start() {
    // We do our drawing here       
    JFrame frame = new JFrame("Animation");
    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(1, 3));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));


    frame.setVisible(true);
}

Take a look at Laying Out Components Within a Container for more details

This assumes, of course, that you want to keep each instance of your shape in it's own component. If you want the shapes to interact in some way or overlap, you'd be better of creating a JComponent which could paint different shapes itself.

Have a look at 2D Graphics for more ideas

Also, you should be calling super.paintComponent(g) before you do any custom painting

Have a look at Painting in AWT and Swing and Performing Custom Painting for more details

I'm instantiating the Shape1 class 3 times and adding them to the frame. But the shape is drawn only once, how can I draw it 3 times?

If you want to position components randomly in a panel (ie the content pane of the JFrame in your example), then you need to set the layout of the panel to null. This means that you need to manually determine the size/location of the component.

So you would need to add:

frame.setLayout( null );

before you start adding the shapes to the frame.

However, in general it is never a good idea to use a null layout. So instead I suggest you use the Drag Layout as it is designed to replace a null layout in a situation like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM