简体   繁体   中英

How can I draw an oval using jbuttons and actionlistener?

I looked into some of the other questions that may have already been answered and i didn't see anything related to mine, or at least that I understood.

I'm trying to create a traffic light and what i am having trouble with is the actual drawing of the red green and yellow circles when I click on a button. A speedy answer would be much appreciated, thanks.

public class TrafficLight extends JApplet implements ActionListener {

   private Image Hayden;

    JButton btn1;

    JButton btn2;

    JButton btn3;

    int x;

public void init() {

    setLayout(new FlowLayout());

    btn1 = new JButton("Stop");

    btn2 = new JButton("Wait");

    btn3 = new JButton("Go");

    Boolean Answer;

    add(btn1);

    btn1.addActionListener(this);

    add(btn2);

    btn2.addActionListener(this);

    add(btn3);

    btn3.addActionListener(this);

    Hayden = getImage(getDocumentBase(), "49.jpg");
}

public void actionPerformed(ActionEvent event){

    if (event.getSource()==btn1){
        boolean one = true;
    }
    if (event.getSource()==btn2){
        boolean two = true;
    }
    if (event.getSource()==btn3){
        boolean three = true;
    }
    repaint();

}
public void paint(Graphics g) {

    super.paint(g);

    g.setColor(Color.black);

    g.drawRect(0, 400, 700, 200);//creating the rectangle
    g.fillRect(0, 400, 700, 200);

    g.setColor(Color.black);
    g.drawRect(645, 0, 55, 155);//creating the rectangle
    g.fillRect(645, 0, 55, 155);

    g.setColor(Color.white);
    g.drawOval(650, 5, 45, 45);//creating the oval
    g.fillOval(650, 5, 45, 45);

    g.setColor(Color.white);
    g.drawOval(650, 55, 45, 45);//creating the oval
    g.fillOval(650, 55, 45, 45);

    g.setColor(Color.white);
    g.drawOval(650, 105, 45, 45);//creating the oval
    g.fillOval(650, 105, 45, 45);
    if (one == true){
        g.setColor(Color.red);
        g.drawOval(650,5,45,45);
    }
    else if (two == true){
        g.setColor(Color.red);
        g.drawOval(650,55,45,45);
    }
    else if (three == true){
        g.setColor(Color.red);
        g.drawOval(650,105,45,45);
    }
    g.drawImage(Hayden, 0, 500, 150, 100, this);//create the image

}


}

isSelected() methods is more related to toggle buttons. In your case inside actionPerformed() none of the conditions will ever return true. Quick and dirty fix could be to check the source of the event, ie:

if (event.getSource() == btn1){
    x = 5;
}

Cleaner would be:

btn1.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        x = 5;
    }
});

Another note, do you custom painting on JPanel or JComponent and then add the component to the content pane of JApplet. See Lesson: Performing Custom Painting for details.

Also, it is recommended to use a layout manager rather than absolute layout. Check out A Visual Guide to Layout Managers for more info on that.

Make sure to go through How to Make Applets tutorial for general guidelines.

EDIT: regarding

cut it down a bit, hopefullynotworse }

Your last edit is incorrect, it does not compile. You're declaring local variable boolean one = true; and trying to use it in another method. Make boolean one a member of the class. Same with two and three variables.

One thing you need to be wary about is variable scope. The booleans one, two, and three have a scope within their respective if statement. If you make these booleans instance variables (create the variables at the top of the class like int x and your buttons) then their scopes are the entire class, and they can be referenced in each method.

JButton btn2;
JButton btn3;
boolean one, two three;

As of right now, these booleans cannot be accessed by anything, including the paint method, that isn't in their respective if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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