简体   繁体   English

使用相同的Jbutton多次更改形状的颜色

[英]Changing color of a shape multiple times with the same Jbutton

I am trying to figure out how to change the color of a shape from one color to another, to another, then back to the original color. 我试图弄清楚如何将形状的颜色从一种颜色改变为另一种颜色,再改变为另一种颜色,然后再改回原始颜色。 Let's say from Red to yellow to green and back to red. 让我们说从红色到黄色再到绿色再回到红色。 I've gotten it so I can change the color the first click, but then can't get it to change again to a different color. 我已经得到它所以我可以改变第一次点击的颜色,但然后不能让它再次改变为不同的颜色。 It will only change back to the original if the shape is no longer "selected." 如果不再“选择”形状,它将仅更改回原始状态。 How to I get a JButton to perform different events each new time its clicked? 如何让JButton在每次点击它的新时间执行不同的事件? Here is what I have for the listeners: 以下是我对听众的看法:

(EDITED) Here is what I have so far (sorry it's a little long, but you asked for something compilable): (编辑)这是我到目前为止(对不起,它有点长,但你要求一些可编辑的东西):

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

and the actual shape 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

A fancy way would be to keep an ArrayList of all the colors that your want to use for each face part. 一种奇特的方法是保留您想要用于每个面部部件的所有颜色的ArrayList。 For example: 例如:

List<Color> faceColors = new ArrayList<Color>();

Then your component that paints the face could have a method: 那么绘制面部的组件可以有一个方法:

public void addFaceColor(Color color)
{
    faceColors.add(color);
}

So in your program you can add as many different colors as you want. 因此,在您的程序中,您可以根据需要添加任意数量的不同颜色。

Then the painting code for the face could be something like: 然后面部的绘画代码可能是这样的:

graphics.setColor(faceColors.isEmpty() ? getForeground() : faceColors.get(0));
graphics.fill(face);

To change the face color you can create a method like the following that would be invoked when you click your button: 要更改面部颜色,可以创建一个类似下面的方法,单击按钮时将调用该方法:

public void nextFaceColor()
{
    faceColors.add( faceColors.remove(0) );
    repaint();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM