I have a picture with 2 buttons, and I want to move it through these buttones
the problem is I don`t know how to make the actionListener for the other button to move to the other side,
pictures are in the attachments
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
public class Transformation2D extends JFrame implements ActionListener {
MyDrawingPanel drawingPanel = new MyDrawingPanel();
MyDrawingPanel2 drawingPanel2 = new MyDrawingPanel2();
JButton goLeft = new JButton("<--");
JButton goRight = new JButton("-->");
int previousX;
int previousY;
Image stick1;
Image stick2;
boolean movementFlag = false;
public Transformation2D(){
super("Walking Stickfigure");
setSize(600,200);
previousX = 250;
previousY = 0;
setBackground(Color.WHITE);
setLayout(new BorderLayout());
add(drawingPanel,BorderLayout.CENTER);
add(drawingPanel2,BorderLayout.SOUTH);
drawingPanel2.add(goLeft);
drawingPanel2.add(goRight);
stick1 = null;
stick2 = null;
try{
stick1 = ImageIO.read(new File("sw1.png"));
stick2 = ImageIO.read(new File("sw2.png"));
}
catch(IOException ex){
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
goLeft.addActionListener(this);
goRight.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == goLeft){
drawingPanel.repaint();
}
else if(ae.getSource() == goRight){
drawingPanel2.repaint();
}
}
public static void main(String[] args) {
new Transformation2D();
}
class MyDrawingPanel extends JPanel{
public MyDrawingPanel(){
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g){
g.clearRect(previousX,previousY,stick1.getWidth(this),stick1.getHeight(this));
g.drawLine(0,134,600,134);
previousX -= 3;
if(movementFlag){
g.drawImage(stick1, previousX, previousY, this);
movementFlag = false;
}
else{
g.drawImage(stick2, previousX, previousY, this);
movementFlag = true;
}
}
}
class MyDrawingPanel2 extends JPanel{
public MyDrawingPanel2(){
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g){
g.clearRect(previousX,previousY,stick1.getWidth(this),stick1.getHeight(this));
g.drawLine(0,134,600,134);
previousX += 3;
if(movementFlag){
g.drawImage(stick1, previousX, previousY, this);
movementFlag = false;
}
else{
g.drawImage(stick2, previousX, previousY, this);
movementFlag = true;
}
}
}
}