赋值要求超类Shape和两个子类Circle和Square。
我要创建一个小程序,用于从三个独立的JComboBox中选择形状(圆形或方形),大小和颜色。
每次用户点击画布时,应根据组合框中的选择创建Shape类的对象,并将其存储在要打印的ArrayList中。
更新颜色变量时会出现某种“滞后”现象。每次我从与更新颜色相关联的ComboBox中进行选择并返回单击画布时,将使用先前选择的颜色打印形状,然后打印的下一个形状是相应更新的颜色。
以下是代码:
public class WholePanel extends JPanel
{
private Color currentColor;
private int size = 10;
private Shape shape;
private String SHAPE = "Circle";
private String SIZE, CURRENTCOLOR;
private CanvasPanel canvas;
private JPanel topPanel, topSplitPanel;
private JButton undo;
private ArrayList <Shape>shapeList;
private JComboBox shapeCombo, sizeCombo, colorCombo;
public WholePanel()
{
//default color to draw is black
shapeList = new ArrayList();
// label for displaying what to input
Label label1 = new Label("Choose Circle or Square, its size, and its color.");
// new button to undo last task
undo = new JButton ("Undo");
// combo box for selecting shape
String[] circleSquare = {"Circle", "Square"};
shapeCombo = new JComboBox(circleSquare);
//combo box for selecting size
String[] sizezzz = {"10","20","30","40","50"};
sizeCombo = new JComboBox(sizezzz);
//combo box for selecting color
String[] colores = {"black","red","blue","green","orange"};
colorCombo = new JComboBox(colores);
// panel for displaying the three combo boxes and the undo button
topPanel = new JPanel(new FlowLayout());
topPanel.add(shapeCombo);
topPanel.add(sizeCombo);
topPanel.add(colorCombo);
topPanel.add(undo);
// new panel with grid layout for displaying label above panel including combo boxes
topSplitPanel = new JPanel(new GridLayout(2,1));
topSplitPanel.add(label1);
topSplitPanel.add(topPanel);
// creates new object of CanvasPanel
canvas = new CanvasPanel();
// add ActionListeners to the undo button and each of JComboBox
undo.addActionListener (new ButtonListener());
shapeCombo.addActionListener (new ComboListener());
sizeCombo.addActionListener (new ComboListener());
colorCombo.addActionListener (new ComboListener());
//adds mouse listener to the canvas
MouseListener pointListener = new PointListener();
canvas.addMouseListener(pointListener);
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topSplitPanel, canvas);
setLayout(new BorderLayout());
add(sp);
}
//CanvasPanel is the panel where shapes will be drawn
private class CanvasPanel extends JPanel
{
//this method draws all shapes
public void paintComponent(Graphics page)
{
super.paintComponent(page);
setBackground(Color.WHITE);
/*for(int i=0; i<shapeList.size(); i++)
{
shape = shapeList.get(i);
shape.draw(page);
}*/
for (Shape shapee : shapeList)
{
shapee.draw(page);
}
}
} //end of CanvasPanel class
//ButtonListener defined actions to take in case
//"Undo" is chosen.
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
shapeList.remove(shapeList.size()-1);
}
} // end of ButtonListener
// listener class to set the color chosen by a user using
// color combo box, set the size chosen using size combo box
// or set the shape (circle or square) using shape combo box
private class ComboListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==shapeCombo)
{
SHAPE = (String) shapeCombo.getSelectedItem();
}
if(event.getSource()==sizeCombo)
{
SIZE = (String) sizeCombo.getSelectedItem();
if (SIZE.equals("10")){ size =10;}
if (SIZE.equals("20")){ size =20;}
if (SIZE.equals("30")){ size =30;}
if (SIZE.equals("40")){ size =40;}
if (SIZE.equals("50")){ size =50;}
}
if(event.getSource()==colorCombo)
{
CURRENTCOLOR = (String) colorCombo.getSelectedItem();
if(CURRENTCOLOR.equals("black")){currentColor = Color.black;}
if(CURRENTCOLOR.equals("red")){currentColor = Color.red;}
if(CURRENTCOLOR.equals("blue")){currentColor = Color.blue;}
if(CURRENTCOLOR.equals("green")){currentColor = Color.green;}
if(CURRENTCOLOR.equals("orange")){currentColor = Color.orange;}
}
}
} // end of ComboListener
// listener class that listens to the mouse
public class PointListener implements MouseListener
{
//in case that a user presses using a mouse,
//record the point where it was pressed.
public void mousePressed (MouseEvent event)
{
int X = event.getX();
int Y = event.getY();
if (SHAPE.equals("Square"))
{
shapeList.add(new Square(X-(size/2),Y-(size/2),size,currentColor));
}
if (SHAPE.equals("Circle"))
{
shapeList.add(new Circle(X-(size/2),Y-(size/2),size,currentColor));
}
}
public void mouseReleased (MouseEvent event) {}
public void mouseClicked (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
} // end of PointListener
} // end of Whole Panel Class