Damhuis
Damhuis

Reputation: 47

Graphics in an ArrayList

I am trying to create a code that prints 10 random graphics (oval,rectangle etc). I'm hopping to do this by adding my random ovals etc in an ArrayList and then let Java randomly pick a shape 10 times from this ArrayList and print these picked items.

Now I have no idea if this is even possible and how I would go about this.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Cara extends JPanel implements ActionListener {
    Random random = new Random();
    //...s

    public Cara() {
        setPreferredSize(new Dimension(400,300)); // make panel 400 by 300 pixels.
        // ... 
        this.setBackground(Color.WHITE);
    }

    protected class RandomShapesComponent extends JComponent{
    @Override

    protected void paintComponent(Graphics g) { 

        super.paintComponent(g);     // clears the background
        // ...
    }
    }

    /**
     * redraws the Cara JPanel, when the button is pressed. *
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        regenerate();
        repaint();
    }

    private void regenerate() {
        // clear the shapes list
        //...

        // create random shapes 
        // ... 
    }


    public static void main(String[] arg) {
        final Cara cara = new Cara();    
        // create the GUI on the event thread.
        // this is better than 
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Computer Assisted Random Artist");
                frame.add(cara, BorderLayout.CENTER);
                JButton button = new JButton("redraw");
                button.addActionListener(cara);
                frame.add(button, BorderLayout.SOUTH);
                frame.pack();
                cara.regenerate(); // can be done here if cara has a size!
                frame.setVisible(true);                
            }
        });
    }
}

And below is a way for me to draw a triangle (this is one of the shapes i would like to put into the array and be randomly picked):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tri extends Cara{
 public void paintComponent (Graphics g){
// create random variables for collor and shape of triangle
int x;
int y;
int x2;
int y2;

x = (int) Math.random()*100;
y = (int) Math.random()*100;
x2 = (int) Math.random()*100; 
y2 = (int) Math.random()*100;

int r1;
int g1;
int b1;
r1 = (int) Math.random()*255;
g1 = (int) Math.random()*255;
b1 = (int) Math.random()*255;
 Color color = new Color(r1,g1,b1);
 //draw triangle

 g.setColor(color);
 g.drawLine(x,y,y2,y);
 g.setColor(color);
 g.drawLine(x,y,y2,y2);
 g.setColor(color);
 g.drawLine(y2,y,y2,y2);


} 


}

Upvotes: 0

Views: 1903

Answers (2)

Ishan Rastogi
Ishan Rastogi

Reputation: 830

You need to create a service object which will draw a shape in the panel according to the number passed 1 to 10. It will have 10 methods which will draw different shapes.

Then when you want to draw any of the shapes- generate a random number less than 11 and use that number to call your service object to draw the respective shape.

Upvotes: 0

davecom
davecom

Reputation: 1498

This is definitely possible. Here's an idea:

  • Have an abstract base class called "Shape" and it has a method called "draw()" which has a parameter for a Graphics context
  • Have subclasses of Shape for Triangle, Circle, etc and fill in each's draw() method accordingly
  • Have an ArrayList instance variable in FruitPanel
  • in the FruitPanel constructor initialize the ArrayList with 10 Shape subclass objects
  • From your paintComponent() method in FruitPanel call draw(g) on each of the things in your ArrayList

You can figure out the randomization and such yourself... this is a basic structure. Good luck!

Upvotes: 1

Related Questions