-3

我对 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);
  }
});
}
}
4

1 回答 1

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);
    }
}
于 2013-10-07T21:00:45.927 回答