我试图弄清楚如何将形状的颜色从一种颜色改变为另一种颜色,再改变为另一种颜色,然后再改回原始颜色。让我们说从红色到黄色再到绿色再回到红色。我已经得到它所以我可以改变第一次点击的颜色,但然后不能让它再次改变为不同的颜色。如果不再“选择”形状,它将仅更改回原始状态。如何让JButton在每次点击它的新时间执行不同的事件?以下是我对听众的看法:
(编者) 这是我到目前为止(对不起,它有点长,但你要求一些可编辑的东西):
public class TestFace extends JFrame {
//Instance Variables
private static Face mugShot = new Face();
private JScrollBar scrollBar = new JScrollBar (JScrollBar.HORIZONTAL);
private MessagePanel messagePanel = new MessagePanel ("Face Class");
//Source
public TestFace () {
JButton colorChange = new JButton ("Change Color");
//add buttons to panel
JPanel panel = new JPanel ();
panel.add(messagePanel, BorderLayout.CENTER);
panel.add(colorChange);
add(scrollBar, BorderLayout.SOUTH);
add(panel);
//Register Listeners
ColorListenerClass listener = new ColorListenerClass();
colorChange.addActionListener(listener);
scrollBar.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
double value = scrollBar.getValue(); //returns current value of scrollbar 0-100
double maximumValue = scrollBar.getMaximum();
double newX = (value * messagePanel.getWidth() / maximumValue);
messagePanel.setXCoordinate((int)newX);
}//end adjustment Value Changed
});//end adjustment listener
//register mouse click activity
addMouseMotionListener(new MouseMotionAdapter() {
@Override public void mouseDragged (MouseEvent event) {
mugShot.selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed (MouseEvent event) {
mugShot.selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
}//end cons
/*************EnlargeListener Class**********************************/
class ColorListenerClass implements ActionListener{
@Override// necessary to respond to the event
public void actionPerformed (ActionEvent e) {
if (mugShot.getSelected() == mugShot.getFace()){
//System.out.println("Face is selected");
mugShot.setColorFace(Color.blue);
repaint();
}//end if
else if (mugShot.getSelected() == mugShot.getMouth()){
System.out.println("Mouth is selected");
mugShot.setColorMouth(Color.yellow);
repaint();
}//end if
else if (mugShot.getSelected() == mugShot.getEyeLeft() || mugShot.getSelected() == mugShot.getEyeRight()){
System.out.println("Eyes are selected");
mugShot.setColorEyes(Color.ORANGE);
repaint();
}//end if
if (mugShot.getSelected() == null) {
JOptionPane.showMessageDialog(null, "Nothing is Selected");
repaint();
}
}//end actionPerformed
}//end OKListenerClass
/********************************************************************/
//////////////MAIN////////////////////////////////////////
public static final void main (String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Mugshot");
frame.add(mugShot);
frame.setSize(500,400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//draw toolbar panel
JFrame frame1 = new TestFace ();
frame1.setTitle("Toolbar");
frame1.setSize(200,150);
frame1.setLocation(200,100);
frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame1.setVisible(true);
}//end main
}//end face class
和实际的形状类:
public class Face extends JPanel{
///Instance Variables
private final Shape face = new Ellipse2D.Float(100, 20, 300, 300);//face component
private final Shape eyeLeft = new Ellipse2D.Float(152,85, 80, 55);//left eye
private final Shape eyeRight = new Ellipse2D.Float(265,85, 80, 55);//right eye
private final Shape pupilRight = new Ellipse2D.Float(285, 85, 40, 55);//right pupil
private final Shape pupilLeft = new Ellipse2D.Float(172, 85, 40, 55);//left pupil
private final Shape mouth = new Rectangle2D.Float(180, 230, 140, 20);//mouth
private Shape selected = null;
Color colorMouth = Color.red;
Color colorFace = Color.green;
Color colorEyes = Color.white;
public Color getColorMouth() {
return colorMouth;
}
public void setColorMouth(Color colorMouth) {
this.colorMouth = colorMouth;
}
public Color getColorEyes() {
return colorEyes;
}
public void setColorEyes(Color colorEyes) {
this.colorEyes = colorEyes;
}
public Color getColorFace() {
return colorFace;
}
public void setColorFace(Color colorFace) {
this.colorFace = colorFace;
}
public Face () {
//register mouse click activity
addMouseMotionListener(new MouseMotionAdapter() {
@Override public void mouseDragged (MouseEvent event) {
selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed (MouseEvent event) {
selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
}
protected void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D graphics = (Graphics2D)g;
graphics.setColor(colorFace);
graphics.fill(face);
graphics.setColor(colorMouth);
graphics.fill(mouth);
graphics.setColor(colorEyes);
graphics.fill(eyeLeft);
graphics.fill(eyeRight);
graphics.setColor(Color.BLACK);
graphics.fill(pupilLeft);
graphics.fill(pupilRight);
g.drawLine(220, 185, 270, 185);
g.drawLine(220, 185, 260, 130);
}//end pC
public Shape getSelected() {
return selected;
}//end getSelected
public void setSelected(Shape selected) {
this.selected = selected;
}//end setSelected
public Shape getFace() {
return face;
}//end getFace
public Shape getEyeLeft() {
return eyeLeft;
}//end getEyeLeft
public Shape getEyeRight() {
return eyeRight;
}//end getRightEye
public Shape getMouth() {
return mouth;
}//end getMouth
public void selectShapeUnder (int x, int y) {
Shape oldSelected = selected;
if (eyeLeft.contains(x, y)){
selected = eyeLeft;
}//end if
else if (eyeRight.contains(x, y)){
selected = eyeRight;
}//end else if
else if (mouth.contains(x, y)){
selected = mouth;
}//end else if
else if (face.contains(x, y)) {
selected = face;
}//end else if
else
selected = null;
if (selected != oldSelected)
repaint();
}//end selectShapeUnder
}//end Face class
答案 0 :(得分:2)
一种奇特的方法是保留您想要用于每个面部部件的所有颜色的ArrayList。例如:
List<Color> faceColors = new ArrayList<Color>();
然后,绘制脸部的组件可以有一个方法:
public void addFaceColor(Color color)
{
faceColors.add(color);
}
因此,在您的程序中,您可以根据需要添加任意数量的不同颜色。
然后面部的绘画代码可能是这样的:
graphics.setColor(faceColors.isEmpty() ? getForeground() : faceColors.get(0));
graphics.fill(face);
要更改面部颜色,您可以创建一个类似下面的方法,单击按钮时会调用该方法:
public void nextFaceColor()
{
faceColors.add( faceColors.remove(0) );
repaint();
}