troubleshooting Question

when using a global variable isn't really global - using buttons to rotate a drawing

Avatar of nhay59
nhay59 asked on
13 Comments2 Solutions299 ViewsLast Modified:

I've now added some actions to buttons that I have within a Java2D program. I want to be able to use these buttons to rotate a drawing within a panel by 45 degrees right and left. I've written the code to rotate the drawing as follows,

AffineTransform at = new AffineTransform();
at.translate(size/2.0, size/2.0); //third transform
at.rotate(Math.toRadians(45)); //second transform
at.translate(-size/2.0, -size/2.0); //first transform

This allows the drawing to be rotated by 45 degrees. My question is how do I link this rotation to the buttons so they will rotate the drawing I've already got in the panel.

My current code is as follows,

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.color.*;

public class PacMan2Clicks extends JFrame implements ActionListener,
        MouseMotionListener, MouseListener {
    public static int mode = 0;
    private static JButton rightButton;
    private static JButton leftButton;
    private Image pacmanImage;
    private int pacmanX, pacmanY;
    JPanel panel = new JPanel();
    public PacMan2Clicks() {
        Container contentPane = getContentPane();
        JPanel panel = new JPanel() {
            public void paintComponent(Graphics g) {
        JPanel buttonPanel = new JPanel();
        leftButton = new JButton("Left");
        rightButton = new JButton("Right");
        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
    private void setPacMan(Graphics g) {
        Dimension d = getSize();
        int size = 100;
        Ellipse2D.Double head = new Ellipse2D.Double(0, 0, size, size);
        Ellipse2D.Double eye = new Ellipse2D.Double(size / 2 - 1, size / 5 - 1,
                size / 10, size / 10);
        GeneralPath mouth = new GeneralPath();
        mouth.moveTo(size, size / 4);
        mouth.lineTo(size / 8, size / 2);
        mouth.lineTo(size, size * 3 / 4);
        Area pacman = new Area(head);
        pacman.subtract(new Area(eye));
        pacman.subtract(new Area(mouth));
        Graphics2D g2 = (Graphics2D) g;
        g2.translate(pacmanX, pacmanY);
    protected void clearDisplay() {
    public void mouseDragged(MouseEvent e) {
        pacmanX = e.getX();
        pacmanY = e.getY();
    public void mouseReleased(MouseEvent e) {}
    public void mouseMoved(MouseEvent e){}
    public void mouseClicked(MouseEvent e){
        JPanel panel = new JPanel();
        if (e.getClickCount() == 2) {
            pacmanX = e.getX();
            pacmanY = e.getY();
    public void mouseExited(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mousePressed(MouseEvent e) {}
    public void actionPerformed(ActionEvent event) {
        if (event.getSource().equals(rightButton)) {
        } else if (event.getSource().equals(leftButton)) {
    public static void main(String[] args) {
        PacMan2Clicks frame = new PacMan2Clicks();
        frame.setSize(600, 600);

Any help really appreciated.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros