如何将我的类添加到ArrayList?

时间:2013-10-07 20:35:26

标签: java random arraylist paintcomponent

我对Java和编程非常陌生,而且我在完成作业时遇到了一些麻烦。 我们应该给出4种不同形状的变化,每张图片中有10种形状,变化至少为20,还有至少20种不同颜色的变化和位置的变化。

我已为我的5个形状中的每一个制作了课程。但我不知道如何将它们添加到我的ArrayList randomShape中,以便可以在paintComponent部分中调用它们。 此外,我无法提供代码位置:(

我有点沮丧,因为我遇到了一个障碍。

感谢您给我的任何帮助/建议 非常感谢

这是我到目前为止的代码

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

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

public static final int MAX_AMOUNT_OF_SHAPES = 50,AMOUNT_OF_DISTINCT_SHAPES=5,         AMOUNT_OF_DISTINCT_COLORS = 20, SIZE_MAX_X_COORDINATE = 400, SIZE_MAX_Y_COORDINATE = 300;;
public static ArrayList<RandomShape> randomShape = new ArrayList<RandomShape>();

int x = 0;
int xMax = 400;
int y = 0;
int yMax = 300;
int width = 0;
int height = 0;
int arcWidth = 0;
int arcHeight = 0;

int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B = (int)(Math.random()*256);
Color randomColor = new Color(R,G,B);

public Cara(){
setPreferredSize( new Dimension(400,300));
}

abstract class RandomShape {
protected Color color;
protected int x,y;

abstract void draw(Graphics g);
}

public class Circle extends RandomShape{

@Override
public void draw(Graphics g) {
  g.drawOval(x,y,width,width);
  g.fillOval(x,y,width,width);
  g.setColor(randomColor);
}
//constructor for random position
Circle (){
  for ( int x; x < xMax; x++){
  }
  for ( int y; y < yMax; y++){
  }
}
}

public class Rectangle extends RandomShape{
@Override
public void draw( Graphics g){
  g.drawRect(x,y,width,height);
  g.fillRect(x,y,width,height);
  g.setColor(randomColor);
}
//constructor for random position
}

public class RoundRectangle extends RandomShape{
@Override
public void draw(Graphics g){
  g.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
  g.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
  g.setColor(randomColor);
}
//constructor for random position
}

public class Oval extends RandomShape{
@Override
public void draw(Graphics g){
  g.drawOval(x,y,width,height);
  g.fillOval(x,y,width,height); 
  g.setColor(randomColor);
}
//constructor for position
}

public class Square extends RandomShape{
Square square = new Square();
@Override
public void draw(Graphics g){
  g.drawRect(x,y,width,width); //because is a rectangle with sides of equal size
  g.fillRect(x,y,width,width);
  g.setColor(randomColor);
}
//constructor for position 
}

protected void paintComponent(Graphics g) {
//clear the background
super.paintComponent(g);
//draw all shapes 
for ( RandomShape rs: randomShape){
rs.draw(g);
} 
}


public void actionPerformed (ActionEvent e){
regenerate();
repaint();
}

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

// create random shapes
RandomShape shape = null;
for (int i = 0; i < 10 + random.nextInt(MAX_AMOUNT_OF_SHAPES); i++){
  int randomInt = random.nextInt(AMOUNT_OF_DISTINCT_SHAPES);

  switch (randomInt) {
    case 0: shape = new Oval(400,300);
    break;
    case 1: shape = new Circle(400,300);
    break;
    case 2: shape = new Rectangle(400,300);
    break;
    case 3: shape = new Square(400,300);
    break;
    case 4: shape = new RoundRectangle(400,300);
    break;
  } 
}

//random position
RandomShape position = null;
for (int i = 0; i < (MAX_SIZE_X_COORDINATE*MAX_SIZE_Y_COORDINATE) ; i++){
  int randomIntpos = random.nextInt();
}
}

public static void main(String[] args) {
final Cara cara = new Cara();
SwingUtilities.invokeLater(new Runnable(){
  @Override
  public void run(){
    final JFrame frame = new JFrame("Computer Assisted Random Artist");
    JButton button = new JButton("regenerate");
    button.addActionListener(cara);
    frame.add(button, BorderLayout.SOUTH);
    frame.pack();
    cara.regenerate();
    frame.setVisible(true);
  }
});
}
}

1 个答案:

答案 0 :(得分:0)

public class Cara extends JPanel implements ActionListener {
    private static ArrayList<RandomShape> randomShape;

    public Cara() {
        randomShape = new ArrayList<RandomShape>();
    }
}

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

    // create random shapes
    RandomShape shape = null;
    int randomInt;
    for(int i = 0; i < 10 + random.nextInt(MAX_AMOUNT_OF_SHAPES); i++) {
        randomInt = random.nextInt(AMOUNT_OF_DISTINCT_SHAPES);

        switch (randomInt) {
            case 0: 
                shape = new Oval(400,300);
                break;
            case 1: 
                shape = new Circle(400,300);
                break;
            case 2: 
                shape = new Rectangle(400,300);
                break;
            case 3: 
                shape = new Square(400,300);
                break;
            case 4: 
                shape = new RoundRectangle(400,300);
                break;
        } 
        randomShape.add(shape);
    }

    //random position
    RandomShape position = null;
    int randomIntpos;
    for (int i = 0; i < (MAX_SIZE_X_COORDINATE*MAX_SIZE_Y_COORDINATE) ; i++){
        randomIntpos = random.nextInt();
    }
}

public class RoundRectangle extends RandomShape {
    private int x;
    private int y;
    //constructor for random position
    public RoundRectangle(int x, int y) {
        this.x = x
        this.y = y
    }

    @Override
    public void draw(Graphics g){
        g.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
        g.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
        g.setColor(randomColor);
    }
}