如何绘制形状Java的arrayList

我的任务是如何在java中绘制形状的arrayList。

我觉得我的大部分都是正确的,然而有两种方法我很困惑

ShapeChooserPanel的方法名为public void setPoints(int x, int y)

由于Xs and Ys是数组,我如何设置x和y值?

另一个是ShapeChooserPanel中的最后一个方法我无法找到如何在数组中打印Shapes,它应该在单击鼠标的位置绘制当前形状。

我的代码如下

主要课程:

 import javax.swing.JFrame; public class Lab2 { public static void main (String[] args) { JFrame myFrame = new JFrame("Lab 2"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.add(new ShapeChooserPanel()); myFrame.pack(); myFrame.setVisible(true); } } 

ShapeChooserPanel类

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; public class ShapeChooserPanel extends JPanel { private int currentX; private int currentY; private Color currentColor; private int currentShape; private JButton clearBtn; private JRadioButton circle, square, triangle, box; private DrawingPanel drawingPanel; private JPanel controlsPanel; //constants representing shape choice private final int CIRCLE = 0; private final int SQUARE = 1; private final int TRIANGLE = 2; private final int BOX = 3; //constant delta used for setting distance between points private final int DELTA = 25; private int[] Xs; private int[] Ys; //store all the shapes to be painted UNCOMMENT when you have Shape.java defined ArrayList shapes; public ShapeChooserPanel(){ //provide some default values paints a circle at (10,10) in blue currentX = 10; currentY = 10; Xs = new int[4];//we will use all 4 points for the square, but only the first 3 for the triangle Ys = new int[4]; setPoints(currentX,currentY); currentShape = CIRCLE; currentColor = Color.red; shapes = new ArrayList(); //instantiate the controls panel and set its layout to display everything in a single column controlsPanel = new JPanel(); controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.Y_AXIS)); //TODO: add clear button * // TODO: define radio buttons * clearBtn = new JButton("Clear"); clearBtn.addActionListener(new ClearListener()); circle = new JRadioButton("Red Circle"); circle.addActionListener(new ShapeListener()); square = new JRadioButton("Cyan Square"); square.addActionListener(new ShapeListener()); triangle = new JRadioButton("Green Triangle"); triangle.addActionListener(new ShapeListener()); box = new JRadioButton("Blue Box"); box.addActionListener(new ShapeListener()); ButtonGroup group = new ButtonGroup(); group.add(clearBtn); group.add(circle); group.add(square); group.add(triangle); group.add(box); controlsPanel.add(clearBtn); controlsPanel.add(circle); controlsPanel.add(square); controlsPanel.add(triangle); controlsPanel.add(box); //TODO: add radio buttons to group * //TODO add listeners to radio buttons * //TODO: add radio buttons to controls panel * drawingPanel = new DrawingPanel(); drawingPanel.setBorder(BorderFactory.createLineBorder(Color.black)); //TODO: set a border around the drawing panel * drawingPanel.setPreferredSize(new Dimension(200,200)); drawingPanel.addMouseListener(new PanelListener()); add(drawingPanel); add(controlsPanel); setPreferredSize(new Dimension (300,400)); }//end constructor public void setPoints(int x, int y) { //TODO: set Xs and Ys * Xs[0] = x; Ys[0] = y; } private class ClearListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ shapes.removeAll(shapes); drawingPanel.repaint(); } } private class PanelListener implements MouseListener { public void mouseClicked(MouseEvent me) { currentX = me.getX(); currentY = me.getY(); //TODO: find coordinates of this mouse click * //TODO: add a new shape to the shapes list* shapes.addAll(shapes); setPoints(currentX, currentY); //TODO: call setPoints with current x and y values * drawingPanel.repaint(); } public void mouseExited(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mousePressed(MouseEvent me){} } //Class to listen for radio button changes private class ShapeListener implements ActionListener{ public void actionPerformed(ActionEvent me){ //TODO: determine which radio button was clicked * if(me.getSource() == circle){ currentShape = CIRCLE; currentColor = Color.red; } if(me.getSource() == square){ currentShape = SQUARE; currentColor = Color.cyan; } if(me.getSource() == triangle){ currentShape = TRIANGLE; currentColor = Color.green; } if(me.getSource() == box){ currentShape = BOX; currentColor = Color.blue; } //TODO: set current shape and color * drawingPanel.repaint(); } } private class DrawingPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); //TODO: paint all the shapes in our list } } } 

和我的Shape课程

 import java.awt.Color; public class Shape { private int x,y; private int type; private Color c; public Shape(int x, int y, int type, Color c) { this.x = x; this.y = y; this.type = type; this.c = c; } public int x(int x) { return x; } public int y(int y) { return y; } public int type(int type) { return type; } public Color c(Color c) { return c; } } 

super.paintComponent(g)从JPanel的超类(JComponent类)调用paintComponent方法来擦除当前在面板上绘制的内容。 这对于动画很有用,但不适用于设置颜色。

我猜你在创建它们时必须设置Arraylist shapes中的形状颜色,但是看看Shape类可能会有所帮助。 也许你可以在这个shape类中创建一个函数changeColor(Color ColorToBeSet)并循环遍历changeColor(Color ColorToBeSet)的形状,在ShapeListener的末尾调用它

你可以…

定义一个形状的“抽象”概念,它具有基本属性(位置和大小),可以自己绘制……

 public abstract class Shape { private int x, y; private int width, height; private Color c; public Shape(int x, int y, int width, int height, Color c) { this.x = x; this.y = y; this.c = c; this.width = width; this.height = height; } public int getHeight() { return height; } public int getWidth() { return width; } public int getX() { return x; } public int getY() { return y; } public Color getColor() { return c; } public abstract void paint(Graphics2D g2d); } 

然后你可以实现各个形状……

 public class Rectangle extends Shape { public Rectangle(int x, int y, int width, int height, Color c) { super(x, y, width, height, c); } @Override public void paint(Graphics2D g2d) { g2d.setColor(getColor()); g2d.drawRect(getX(), getY(), getWidth(), getHeight()); } } public class Oval extends Shape { public Oval(int x, int y, int width, int height, Color c) { super(x, y, width, height, c); } @Override public void paint(Graphics2D g2d) { g2d.setColor(getColor()); g2d.drawOval(getX(), getY(), getWidth(), getHeight()); } } 

然后,您可以根据需要简单地调用Shape的每个实例的paint方法…

形状

 public class TestPane extends JPanel { private List shapes; public TestPane() { shapes = new ArrayList<>(25); shapes.add(new Rectangle(10, 10, 20, 20, Color.RED)); shapes.add(new Oval(15, 15, 40, 20, Color.RED)); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (Shape shape : shapes) { Graphics2D g2d = (Graphics2D) g.create(); shape.paint(g2d); g2d.dispose(); } } }