I
Ian Stanley
Hi,
I have a simple program below where the user can draw rectangles on a panel.
The choice to either rotate or scale any single rectangle should result in
the appropriate action.(after selecting a shape).
However, the selected rectangle is transformed(rotated or scaled) but it
appears to be drawn translated as well.
I am guessing that it has somethig to do with transforming the coordinate
system for that shape.
Is there a way to fix it so that the selected shape is rotated or scaled at
its current location?
Thanking you in advance
Ian.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class SwingDraw
{
public static void main(String[] args)
{
Draw D = new Draw() ;
}
}
interface Constants {
final int RECTANGLE = 1, ROTATE = 2, SCALE = 3;
}
class Draw extends JFrame
implements Constants, ActionListener{
JPanel bp = new JPanel() ;
DrawJPanel djp ;
Container content = getContentPane() ;
JButton rectButton, rotateButton, quitButton, scaleButton ;
Draw() {
super() ;
content.setLayout(new BorderLayout()) ;
djp = new DrawJPanel() ;
bp.add(rectButton = new JButton("Rectangle")) ;
bp.add(rotateButton = new JButton("rotate")) ;
bp.add(scaleButton = new JButton("scale")) ;
bp.add(quitButton = new JButton("Quit")) ;
quitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent
e) {
System.exit(0) ;
}
}) ;
rectButton.addActionListener(this) ;
rotateButton.addActionListener(this) ;
scaleButton.addActionListener(this) ;
content.add(bp, BorderLayout.NORTH) ;
content.add(djp, BorderLayout.CENTER) ;
setSize(750, 500) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand() ;
switch (cmd.charAt(0)) {
case 'R':
djp.setMode(RECTANGLE) ;
break ;
case 'r':
djp.setMode(ROTATE) ;
break ;
case 's':
djp.setMode(SCALE) ;
break ;
}
}
}
class DrawJPanel extends JPanel
implements Constants
{
Vector v = new Vector();
Shape current_shape = null;
int drawingMode;
public DrawJPanel()
{
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
// check if we hit an existing shape
current_shape = locateRect(x,y);
// if ( current_shape==null)
// {
if (drawingMode == RECTANGLE){
Rectangle2D.Float e = new
Rectangle2D.Float(x, y, 80, 50);
v.add(e);
repaint();
}
else if (drawingMode == ROTATE &&
current_shape != null ){
rotateShape(x,y);
repaint();
}
else if (drawingMode == SCALE &&
current_shape != null ){
scaleShape(x,y);
repaint();
}
}
}
);
}
void setMode(int mode) {
drawingMode = mode ;
}
public Shape locateRect(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) )
return s;
}
return null;
}
public Shape rotateShape(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) ){
AffineTransform tr =
AffineTransform.getRotateInstance(Math.toRadians(45));
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(s);
repaint();
}
}
return null;
}
public Shape scaleShape(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) ){
AffineTransform tr =
AffineTransform.getScaleInstance(0.7,0.7);
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(s);
repaint();
}
}
return null;
}
public void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
for(int i=0; i<v.size(); i++)
g.draw((Shape)v.elementAt(i));
}
}
I have a simple program below where the user can draw rectangles on a panel.
The choice to either rotate or scale any single rectangle should result in
the appropriate action.(after selecting a shape).
However, the selected rectangle is transformed(rotated or scaled) but it
appears to be drawn translated as well.
I am guessing that it has somethig to do with transforming the coordinate
system for that shape.
Is there a way to fix it so that the selected shape is rotated or scaled at
its current location?
Thanking you in advance
Ian.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class SwingDraw
{
public static void main(String[] args)
{
Draw D = new Draw() ;
}
}
interface Constants {
final int RECTANGLE = 1, ROTATE = 2, SCALE = 3;
}
class Draw extends JFrame
implements Constants, ActionListener{
JPanel bp = new JPanel() ;
DrawJPanel djp ;
Container content = getContentPane() ;
JButton rectButton, rotateButton, quitButton, scaleButton ;
Draw() {
super() ;
content.setLayout(new BorderLayout()) ;
djp = new DrawJPanel() ;
bp.add(rectButton = new JButton("Rectangle")) ;
bp.add(rotateButton = new JButton("rotate")) ;
bp.add(scaleButton = new JButton("scale")) ;
bp.add(quitButton = new JButton("Quit")) ;
quitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent
e) {
System.exit(0) ;
}
}) ;
rectButton.addActionListener(this) ;
rotateButton.addActionListener(this) ;
scaleButton.addActionListener(this) ;
content.add(bp, BorderLayout.NORTH) ;
content.add(djp, BorderLayout.CENTER) ;
setSize(750, 500) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand() ;
switch (cmd.charAt(0)) {
case 'R':
djp.setMode(RECTANGLE) ;
break ;
case 'r':
djp.setMode(ROTATE) ;
break ;
case 's':
djp.setMode(SCALE) ;
break ;
}
}
}
class DrawJPanel extends JPanel
implements Constants
{
Vector v = new Vector();
Shape current_shape = null;
int drawingMode;
public DrawJPanel()
{
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
// check if we hit an existing shape
current_shape = locateRect(x,y);
// if ( current_shape==null)
// {
if (drawingMode == RECTANGLE){
Rectangle2D.Float e = new
Rectangle2D.Float(x, y, 80, 50);
v.add(e);
repaint();
}
else if (drawingMode == ROTATE &&
current_shape != null ){
rotateShape(x,y);
repaint();
}
else if (drawingMode == SCALE &&
current_shape != null ){
scaleShape(x,y);
repaint();
}
}
}
);
}
void setMode(int mode) {
drawingMode = mode ;
}
public Shape locateRect(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) )
return s;
}
return null;
}
public Shape rotateShape(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) ){
AffineTransform tr =
AffineTransform.getRotateInstance(Math.toRadians(45));
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(s);
repaint();
}
}
return null;
}
public Shape scaleShape(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) ){
AffineTransform tr =
AffineTransform.getScaleInstance(0.7,0.7);
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(s);
repaint();
}
}
return null;
}
public void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
for(int i=0; i<v.size(); i++)
g.draw((Shape)v.elementAt(i));
}
}