java swing rectangle_解决swing - Rotate a Java Graphics2D Rectangle?

方法一、使用Graphics import java.awt.*;import java.awt.image.*; import javax.imageio.*;import java.io.*;public static void main(String[]

I have searched everywhere and I just cant find the answer.

How do I rotate a Rectangle in java?

Here is some of my code:

package net.chrypthic.Space;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Space extends JPanel implements ActionListener{

Timer time;

public Space()

{

setVisible(true);

setFocusable(true);

addMouseMotionListener(new ML());

addMouseListener(new ML());

addKeyListener(new AL());

time=new Timer(5, this);

time.start();

}

public void paint(Graphics g)

{

super.paint(g);

Graphics2D g2d = (Graphics2D)g;

g2d.setColor(Color.WHITE);

Rectangle rect2 = new Rectangle(100, 100, 20, 20);

g2d.draw(rect2);

g2d.fill(rect2);

}

public void actionPerformed(ActionEvent ae) {

repaint();

}

public class AL extends KeyAdapter

{

public void keyPressed(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

}

public class ML extends MouseAdapter

{

public void mouseMoved(MouseEvent e) {

}

public void mousePressed(MouseEvent e){

}

}

}

I tried g2d.rotate(100D); but it didnt work. Thanks in advance.

Here's my edited code:

package net.chrypthic.Space;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Space extends JPanel implements ActionListener{

Timer time;

public Space()

{

setVisible(true);

setFocusable(true);

setSize(640, 480);

setBackground(Color.BLACK);

time=new Timer(5, this);

time.start();

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

Rectangle rect1 = new Rectangle(100, 100, 20, 20);

g2d.setColor(Color.WHITE);

g2d.translate(rect1.x+(rect1.width/2), rect1.y+(rect1.height/2));

g2d.rotate(Math.toRadians(90));

g2d.draw(rect1);

g2d.fill(rect1);

}

public void actionPerformed(ActionEvent e)

{

repaint();

}

}

java

swing

rotation

graphics2d

|

this question

edited Jun 16 '15 at 13:02

Gosu 3,413 6 19 32 asked Sep 22 '11 at 15:50

chrypthic 364 1 4 14      this way you are first translating the rect and then rotating it. It's equivalent to: new Rectangle(110,110,20,20);g2d.rotate(Math.toRadians(45)); –

Heisenbug Sep 22 '11 at 17:30

|

4 Answers

4

解决方法

For images you have to use drawImage method of Graphics2D with the relative AffineTransform.

For shape you can rotate Graphics2D itself:

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

g2d.setColor(Color.WHITE);

Rectangle rect2 = new Rectangle(100, 100, 20, 20);

g2d.rotate(Math.toRadians(45));

g2d.draw(rect2);

g2d.fill(rect2);

}

And btw, you should override paintComponent method instead of paint.

Citing JComponent's API:

Invoked by Swing to draw components. Applications should not invoke paint directly, but should instead use the repaint method to schedule the component for redrawing.java应用程序开发中的GUI的API的体系架构如下:   AWT:     是第一个和java同时推出的GUI,他调用本地库,支持鼠标和键盘的事件,有本地窗口接收到事件,转到ja

This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. They're called in the order listed to ensure that children appear on top of component itself. Generally speaking, the component and its children should not paint in the insets area allocated to the border. Subclasses can just override this method, as always. A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.

Remember also than when you perform an affine transformation, like a rotation, the object is implicitly rotated around the axis origin. So if your intent is to rotate it around an arbitrary point, you should before translating it back to the origin, rotate it, and then re-traslating it to the desired point.

|

this answer

edited Sep 22 '11 at 16:18 answered Sep 22 '11 at 15:54

Heisenbug 26.8k 19 89 160      Thanks ill try it, what is better about paintComponent? –

chrypthic Sep 22 '11 at 16:09      K, i tried it. Now my rectangle is wedged in the left corner although I set the X position to 100. Why is this happening? (screenie:

cl.ly/AM8c) –

chrypthic Sep 22 '11 at 16:13      "Remember also than when you perform an affine transformation, like a rotation, the object is implicitly rotated around the axis origin. So if your intent is to rotate it around an arbitrary point, you should before translating it back to the origin, rotate it, and then re-traslating it to the desired point." How'd I do that? –

chrypthic Sep 22 '11 at 16:32      you are drawing a square : new Rectangle(100, 100, 20, 20); It's located at 100,100. If you define your rectangle this way: new Rectangle(-10, -10, 20, 20); then perform the rotation and then g2d.translate(100,100); you should obtain the desired result. –

Heisenbug Sep 22 '11 at 16:35      Thanks. I have translated it, now when I change the X or the Y and my rotation is 90 (its pointless but a test), it moves down instead of right or down. Why? Is there a way around this? –

chrypthic Sep 22 '11 at 16:52

|

show more comments

public void draw(Graphics2D g) {

Graphics2D gg = (Graphics2D) g.create();

gg.rotate(angle, rect.x + rect.width/2, rect.y + rect.height/2);

gg.drawRect(rect.x, rect.y, rect.width, rect.height);

gg.dispose();

gg = (Graphics2D) g.create();

... other stuff

}

Graphics.create() and Graphics.dispose() allow you to save the current transformation parameters (as well as current font, stroke, paint, etc), and to restore them later. It is the equivalent of glPushMatrix() and glPopMatrix() in OpenGL.

You can also apply an inverse rotation once you drew the rectangle to revert the transformation matrix back to its initial state. However, floating point approximations during substractions may lead to a false result.

|

this answer answered Sep 22 '11 at 16:04

Aurélien Ribon 5,532 2 28 46      draw method doesn't exist for JComponent –

Heisenbug Sep 22 '11 at 16:28      Of course, I expect the op to know how to draw on a JPanel, this is just a code snippet :) –

Aurélien Ribon Sep 22 '11 at 17:13      +1 for the

other

rotate(). –

trashgod Sep 22 '11 at 17:59

|

Another way is by using Path2D, with it you can rotate the path only and not the entire graphics object.

Rectangle r = new Rectangle(

x,y ,

width, height);

Path2D.Double path = new Path2D.Double();

path.append(r, false);

AffineTransform t = new AffineTransform();

t.rotate(angle);

path.transform(t);

g2.draw(path);

|

this answer

edited Aug 14 '14 at 8:29 answered May 2 '13 at 4:45

Daniel De León 7,020 2 45 48 1   Path2D is one awesome feature of Java2D I didn't know about... –

Jose Aug 7 '13 at 10:22

|

-1

The only problem with g2d.rotate is that it doesn't rotate it around a specific point. It will mostly mess up where you want your Image and then force you to move the x and y coordinates of the image. I would not use it,expecially for a game. What you should look into is rotating a point in java.

|

this answer answered Feb 28 '13 at 15:43

Devon Ward 1

|

java图像界面开发简单实例 Graphics2D、Rectangle2D、Ellipse2D、Line2D的简单应用,原理为创建相应的图形对象,并设置图形的大小及相关设置,通过Graphics2D对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值