How may I rotate rect in the Panel?
I have class extends JPanel. I implemented method paintComponent(). In this method I draw small rectangle. In the Panel I implemented either methods of interfaces MouseListener,MouseMotionListener and KeyListener. I may dragg this rectangle and it work fine but I would like to rotate this rectangle too. I write in paintComponent() code:
if(rot){
AffineTransform af=AffineTransform.getRotateInstance(Math.PI/4);
g2.transform(af);
}
In keyPressed() method implemented in Panel I wrote:
rot=true;
repaint();
Please help.
Did it work? We don't know what else you are doing, or what the rest of your code looks like so it is hard to help.Create a Short,Self Contained, Compilable and Executable, Example Program and post it here.
Thanks for answer.
It is piece of my code:
class Panel1 extends JPanel implements MouseListener, MouseMotionListener,KeyListener{
Rectangle rect = new Rectangle(0, 0, 80, 30);
Rectangle area;
boolean rot=false,first=true;
Graphics2D g2;
...
public void mouseClicked(MouseEvent e){
rot=true;
repaint();
}
...
public void paintComponent(Graphics g){
super.paintComponent(g);
g2 = (Graphics2D)g;
if(first) {
area=new Rectangle(WIDTH,HEIGHT);
rect.setLocation(0,0);
first=false;
}
g2.setColor(Color.orange);
g2.fillRect(0, 0, WIDTH, HEIGHT);
g2.setColor(Color.red);
g2.fill(rect);
g2.setColor(Color.blue);
g2.draw(rect);
if(obrot) {
AffineTransform af=AffineTransform.getRotateInstance(Math.PI/4);
g2.transform(af);
rot=false;
}
}
If I click mouse on this rect my all Panel is rotate 45 deeges but not rect.
You are getting closer but this still doesn't compile.
It is my all code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
public class MyProgram extends JApplet {
Panel1 d;
public void init(){
setLayout(new BorderLayout());
d = new Panel1();
d.setPreferredSize(new Dimension(500,400));
d.setBackground(Color.white);
Border krawedz=BorderFactory.createLineBorder(Color.black);
d.setBorder(krawedz);
add(d,BorderLayout.CENTER);
}
public static void main(String s[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new MyProgram();
f.add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550,480));
f.show();
}
}
class Panel1 extends JPanel implements MouseListener, MouseMotionListener,KeyListener{
Rectangle rect = new Rectangle(0, 0, 100, 50);
final int WIDTH=500;
final int HEIGHT=400;
int x,y;
boolean first= true;
Rectangle area;
boolean pressOut = false;
boolean rot=false;
Graphics2D g2;
public Panel1(){
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);
}
public void mousePressed(MouseEvent e){
x = rect.x - e.getX();
y = rect.y - e.getY();
if ( rect.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else { pressOut = true; }
}
public void mouseDragged(MouseEvent e){
if ( !pressOut ) {
updateLocation(e);
}
}
public void mouseReleased(MouseEvent e){
if ( rect.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {pressOut = false; }
}
public void mouseMoved(MouseEvent e){
if ( rect.contains(e.getX(), e.getY()) ) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void mouseClicked(MouseEvent e){
rot=true;
repaint();
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void updateLocation(MouseEvent e){
rect.setLocation(x + e.getX(),y + e.getY());
repaint();
}
public void keyTyped(KeyEvent kev) { }
public void keyPressed(KeyEvent kev) {
if(kev.getKeyCode()==KeyEvent.VK_1){
rot=true;
repaint();}
}
public void keyReleased(KeyEvent kev) {}
public void paintComponent(Graphics g){
super.paintComponent(g);
g2 = (Graphics2D)g;
if ( first ) {
area = new Rectangle(WIDTH, HEIGHT);
rect.setLocation(0,0);
first = false;
}
g2.setColor(Color.orange);
g2.fillRect(0, 0, WIDTH, HEIGHT);
g2.setColor(Color.red);
g2.fill(rect);
g2.setColor(Color.blue);
g2.draw(rect);
if(rot) {
AffineTransform af=AffineTransform.getRotateInstance(Math.PI/4);
g2.transform(af);
rot=false;
}
}
}
Ok, I will try it out. But one thing that jumped out at me is:f.show();Do not use Deprecated method.In this case do this instead:f.setVisible(true);
Alright, your rotating doesn't happen because you draw the rectangle and then do this:
if (rot) {
AffineTransform af = AffineTransform.getRotateInstance(Math.PI / 4);
g2.transform(af);
rot = false;
}
This need to be the otherway around. Rotate before you do the drawling that you want rotated.
Also, you create a frame in a main method but the class also is an applet. Do you want this to be an applet or a Swing app?
Thanks.this programm will be applet.Thanks for advice for setVisible() method.I don't know how I must implemented this rotation of rectangle.
> I don't know how I must implemented this rotation of> rectangle.What are you asking?You are rotating it now. It just you rotate after you draw. Move the rotating code to happen before the drawling code.
Thank you.Yes, rectangle is rotate, but with orange Panel.I would like to rotate only red rectangle.How may I do it?
Well, then instead of:RotateDraw Orange Draw RedYou will need to reorder your code toDraw OrangeRotateDraw Red
Thanks.Yes, the red rectangle is rotating.But I have another problem.When I rotate the red rectangle, it go away me, don't rotate around its center.And another problem: after rotate rect I don't drag this red rect.
// <applet code="MP" width="500" height="400"></applet>
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.border.*;
public class MP extends JApplet {
public void init() {
ChildPanel childPanel = new ChildPanel();
Border krawedz=BorderFactory.createLineBorder(Color.black);
childPanel.setBorder(krawedz);
setLayout(new BorderLayout());
add(childPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new MP();
f.add("Center", applet);
applet.init();
f.pack();
f.setVisible(true);
}
}
class ChildPanel extends JPanel {
Shape shape = new Rectangle(0, 0, 100, 50);
final int WIDTH=500;
final int HEIGHT=400;
Rectangle area = new Rectangle(0, 0, WIDTH, HEIGHT);
boolean pressOut = false;
public ChildPanel() {
// Instead of calling fill(area) in paintComponent
// you can set the background here and call
// super.paintComponent(g) in paintComponent and
// eliminate the setColor/fill(area) calls there.
// Otherwise, this next line is unnecessary.
//setBackground(Color.orange);
addMouseMotionListener(mouseInput);
addMouseListener(mouseInput);
registerKeyStrokes();
setFocusable(true);
}
protected void paintComponent(Graphics g) {
// This call to super fills in the background of this JPanel.
// If you are going to call g2.fill(area), you don't need it.
//super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.orange);
g2.fill(area);
g2.setColor(Color.red);
g2.fill(shape);
g2.setColor(Color.blue);
g2.draw(shape);
}
public Dimension getPreferredSize() {
return area.getSize();
}
/** Encapsulate your MouseEvent code. */
private MouseInputAdapter mouseInput = new MouseInputAdapter() {
Point start;
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if ( shape.contains(p) ) {
start = p;
} else {
pressOut = true;
}
}
public void mouseDragged(MouseEvent e) {
if ( !pressOut ) {
updateLocation(e);
}
}
public void mouseReleased(MouseEvent e) {
if ( shape.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
pressOut = false;
}
}
public void mouseMoved(MouseEvent e) {
if ( shape.contains(e.getX(), e.getY()) ) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void updateLocation(MouseEvent e) {
Point p = e.getPoint();
int x = p.x - start.x;
int y = p.y - start.y;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
shape = at.createTransformedShape(shape);
start = p;
repaint();
}
};
/**
* This approach is preferred over using a KeyListener
* which can have focus issues in j2se 1.4+
* See Swing tutorial for more.
* Press the "1" key to rotate red rectangle.
*/
private void registerKeyStrokes() {
KeyStroke keyStroke = KeyStroke.getKeyStroke("1");
getInputMap().put(keyStroke, "ROTATE");
getActionMap().put("ROTATE", rotate);
}
private Action rotate = new AbstractAction() {
double thetaInc = Math.PI/4;
public void actionPerformed(ActionEvent e) {
Rectangle2D r = shape.getBounds2D();
double cx = r.getCenterX();
double cy = r.getCenterY();
AffineTransform af=AffineTransform.getRotateInstance(thetaInc, cx, cy);
shape = af.createTransformedShape(shape);
repaint();
}
};
}
Oh, yes. Thank You very, very much. It work very fine!crwood and zadok thanks for help!It it very good way for rotate of rect.