【发布时间】:2013-11-30 03:21:53
【问题描述】:
我正在尝试弄清楚如何将形状的颜色从一种颜色更改为另一种颜色,然后再更改为原始颜色。假设从红色到黄色到绿色再到红色。我已经得到它,所以我可以在第一次单击时更改颜色,但随后无法让它再次更改为不同的颜色。如果不再“选择”形状,它只会变回原来的形状。如何让 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
【问题讨论】:
-
请帮忙!有很多方法,两个三个是正确的,那么这里的一切都可能只是在黑暗中拍摄,没有你的SSCCE,简短,可运行,可编译
-
我添加了目前所有的内容。
标签: java swing colors actionlistener paintcomponent