Skip to content
Advertisement

How can I rotate multiple graphics shapes together in java without changing their position on the frame or their position relative to eachother?

I have used several java.awt.Rectangle, java.awt.Polygon, and java.awt.geom.Ellipse2D shapes together and I want to rotate them with eachother and I also want to have them keep their location on the JFrame. When I use g2D.rotate(Math.toRadians(rotation)), the shapes move on the JFrame and they are no longer close together. How can I make it so that all of the shapes keep their position relative to eachother and their position on the JFrame? Thank you in advance.

Advertisement

Answer

If you want to be able to rotate the shapes together and keep their position on the JFrame I would recommend that you use the form of g2D.rotate that uses x and y coordinates to rotate with. You should make a standard x and y coordinate to draw each shapes position from so that when you use these standard x and y coordinates to rotate from all of the shapes will rotate together. It would probably look something like this:

//  imports
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;  
import javax.swing.JComponent;

public class GraphicsRotate extends JComponent implements ActionListener {

    //  JFrame and Container
    JFrame frame = new JFrame("Graphics Rotate");
    Container container = frame.getContentPane();
    public int standardX = 100;
    public int standardY = 100;
    public int rotation = 0;

    public static void main(String[] args) {
        GraphicsRotate graphicsRotate = new GraphicsRotate();
        graphicsRotate.setup();

    }

    public void setup() {
        container.setBackground(Color.BLACK);
        container.add(this);

        KeyListener kl = new KeyListener() {
            public void keyPressed(KeyEvent e) {
               int code = e.getKeyCode();
               if (code == KeyEvent.VK_LEFT) {
                   rotation -= 10;
                   repaint();
               }
               if (code == KeyEvent.VK_RIGHT) {
                   rotation += 10;
                   repaint();
               }
            }

            public void keyReleased(KeyEvent e) {

            }

            public void keyTyped(KeyEvent e) {

            }
        };

        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addKeyListener(kl);
        frame.setFocusable(true);
        frame.requestFocus();
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.GREEN);
        g2.rotate(Math.toRadians(rotation), standardX, standardY);
        Rectangle rect = new Rectangle(standardX + 10, standardY + 10, 5, 
5);
        Ellipse2D ellipse = new Ellipse2D.Double(standardX + 13, standardY 
+ 5, 10, 10);
        g2.fill(ellipse);
        g2.fill(rect);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    } 

}

This could be implemented on a much larger scale, and if you wanted the shapes to rotate around the center of the larger shape that you have made with them, then you could do the necessary calculations to figure out the center and do something like g2.rotate(Math.toRadians(rotation), standardX + middleX, standardY + middleY).

I hope that this answered your question.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement