Reputation: 33
I've been stuck at this for a couple of days now (first time working with multiple ActionListener
, so bear with me).
I have two buttons, with each an actionlistener for moving a drawing to either the left or the right.
Yet either the actionlisteners do not seem to work properly, or the actionperformed does not work.
Suggestions are much appreciated, I've tried switching them to Action
as was suggested elsewhere on this forum, but that didn't work out either.
package h03verplaatsbarebal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Paneel extends JPanel implements ActionListener{
//declare objects
private JButton knopLinks; // moves ball to left
private JButton knopRechts; // moves ball to right
//constants
private int horizontalePlaats; // variabele voor horizontale plaats
private int VERPLAATSING; // constante voor verplaatsing
/*create panel with 2 buttons (to left, to right) and a ball*/
public Paneel() {
//create objects
knopLinks = new JButton ("Naar links");
knopLinks.addActionListener(this);
knopRechts = new JButton ("Naar rechts");
knopRechts.addActionListener(this);
//Tooltips
knopLinks.setToolTipText("Klik hier om de bal naar links te bewegen");
knopRechts.setToolTipText("Klik hier om de bal naar rechts te bewegen");
//add to window
add(knopLinks);
add(knopRechts);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int midden = getWidth() / 2; // halfway screen
int balDiameter = 50;
int ovaalDiameter = 25;
horizontalePlaats = midden;
//draw line
g.setColor(Color.GREEN);
g.drawLine(30, getHeight() - 30, getWidth() -30, getHeight() - 30); //lijn
//draw ball
g.setColor(Color.ORANGE);
g.fillOval(horizontalePlaats - balDiameter, getHeight() - 130, 100, 100); // oranje bal
g.setColor(Color.BLACK);
g.drawOval(horizontalePlaats - balDiameter, getHeight() - 130, 100, 100); //lijn van bal
g.setColor(Color.BLACK);
g.drawOval(horizontalePlaats - ovaalDiameter, getHeight() - 130, 50, 100); // binnen lijnen
}
/*clicking buttons*/
public void actionPerformed(ActionEvent e) {
VERPLAATSING = 15;
if (e.getSource() == knopLinks){ //move to left
horizontalePlaats = horizontalePlaats - VERPLAATSING;
}
else { //move to right
horizontalePlaats = horizontalePlaats + VERPLAATSING;
}
repaint(); // paint again
}
}
Upvotes: 0
Views: 43
Reputation: 600
You are overriding horizontalePlaats in your paint.
int midden = getWidth() / 2; // halfway screen
int balDiameter = 50;
int ovaalDiameter = 25;
horizontalePlaats = midden;
I think your action listener is fine, but you need to only initialize horizontalePlaats to the middle.
You can move this
int midden = getWidth() / 2; // halfway screen
horizontalePlaats = midden;
to your Paneel constructor.
Upvotes: 0
Reputation: 17534
Your action listener should work, but what it does is only modifying the value of horizontalePlaats
.
The problem is that horizontalePlaats
gets overwritten by the value of midden
in paintComponent
, so you never see the result of the performed actions.
horizontalePlaats = midden;
Upvotes: 1