我已經一起使用了幾個 java.awt.Rectangle、java.awt.Polygon 和 java.awt.geom.Ellipse2D 形狀,我想彼此旋轉它們,我還想讓它們保持在 JFrame 上的位置。當我使用 g2D.rotate(Math.toRadians(rotation)) 時,形狀在 JFrame 上移動,它們不再靠得很近。我怎樣才能使所有形狀保持它們相對于彼此的位置以及它們在 JFrame 上的位置?先感謝您。
uj5u.com熱心網友回復:
如果您希望能夠一起旋轉形狀并保持它們在 JFrame 上的位置,我建議您使用 g2D.rotate 的形式,它使用 x 和 y 坐標進行旋轉。您應該制作一個標準的 x 和 y 坐標來繪制每個形狀的位置,以便當您使用這些標準的 x 和 y 坐標從所有形狀旋轉時將一起旋轉。它可能看起來像這樣:
// 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) {
}
}
這可以在更大的范圍內實作,如果您希望形狀圍繞您使用它們制作的較大形狀的中心旋轉,那么您可以進行必要的計算以找出中心并執行類似 g2.2 的操作。旋轉(Math.toRadians(旋轉),標準 X 中間 X,標準 Y 中間 Y)。
我希望這回答了你的問題。
uj5u.com熱心網友回復:
記住這一點非常重要,轉變是復合的。所以你不能簡單地Graphics為每個物件的背景關系應用一個新的旋轉,相反,你需要重置轉換之間的狀態

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public final class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Shape> shapes = new ArrayList<>(4);
private double angle = 0;
public TestPane() {
shapes.add(new Rectangle2D.Double(50, 50, 100, 100));
shapes.add(new Rectangle2D.Double(250, 50, 100, 100));
shapes.add(new Rectangle2D.Double(50, 250, 100, 100));
shapes.add(new Rectangle2D.Double(250, 250, 100, 100));
Timer timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
angle = 1;
repaint();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape shape : shapes) {
Graphics2D g2d = (Graphics2D) g.create();
Rectangle bounds = shape.getBounds();
int midx = bounds.x (bounds.width / 2);
int midy = bounds.y (bounds.height / 2);
g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angle), midx, midy));
g2d.draw(shape);
g2d.dispose();
}
}
}
}
您也可以直接轉換形狀,例如...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Shape shape : shapes) {
Rectangle bounds = shape.getBounds();
int midx = bounds.x (bounds.width / 2);
int midy = bounds.y (bounds.height / 2);
Path2D.Double rotatedShape = new Path2D.Double(shape, AffineTransform.getRotateInstance(Math.toRadians(angle), midx, midy));
g2d.draw(rotatedShape);
}
g2d.dispose();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/466366.html
