-3

私はJavaとプログラミング全般に非常に慣れていないため、宿題に少し問題があります。4 つの異なる形状のバリエーション、各画像に少なくとも 20 のバリエーションを持つ 10 の形状、さらに少なくとも 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 に答える