J
Jonck
Hi everybody,
I have a small Java app that allows you to move shapes around by clicking and dragging. The dragged shape has a dashed line to distinguish it from the original.
Now here is my question: when you run the code included at the bottom of this post it works just great. However, I would like to use this in a Swing app. So if I change the Mover class from Frame to JFrame and the MoverPanel from Canvas to JPanel I would expect things to work still (except on line 14 where you have to change add(mp) to getContentPane().add(mp)); after all, all method calls that are used here are valid for JPanel as well. But it doesn't work correctly, the dragged shape leaves behind a "trail", cluttering the JPanel very quickly.
Could anyone tell me how to translate this code to use Swing?
Thanks very much, Jonck
<code>
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Mover extends Frame {
public static void main(String[] arg) {
new Mover();
}
Mover() {
super("Mover");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); } } );
MoverPanel mp = new MoverPanel();
add(mp);
pack();
show();
}
class MoverPanel extends Canvas
implements MouseListener, MouseMotionListener {
boolean moving = false;
int moveIndex;
int startx;
int starty;
int endx;
int endy;
Area[] area;
AffineTransform at;
BasicStroke dashes;
MoverPanel() {
addMouseListener(this);
addMouseMotionListener(this);
area = new Area[4];
area[0] = new Area(new Ellipse2D.Float(10,10,40,40));
area[1] = new Area(new Ellipse2D.Float(80,80,20,50));
area[2] = new Area(new Ellipse2D.Float(130,130,60,60));
area[3] = new Area(new Ellipse2D.Float(200,200,50,20));
setSize(300,250);
at = new AffineTransform();
float[] pattern = { 5f,5f };
dashes = new BasicStroke(1f,BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL,1f,pattern,0f);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for(int i=0; i<area.length; i++)
g2.draw(area);
if(moving) {
g2.setTransform(at);
g2.setStroke(dashes);
g2.draw(area[moveIndex]);
}
}
public void mouseMoved(MouseEvent event) {
moving = false;
}
public void mouseDragged(MouseEvent event) {
if(!moving)
return;
endx = event.getX();
endy = event.getY();
at.setToTranslation(endx - startx,endy - starty);
repaint();
}
public void mousePressed(MouseEvent event) {
moving = false;
startx = event.getX();
starty = event.getY();
for(int i=0; i<area.length; i++) {
if(area.contains(startx,starty)) {
moving = true;
moveIndex = i;
at.setToTranslation(0,0);
break;
}
}
}
public void mouseReleased(MouseEvent event) {
if(moving) {
area[moveIndex].transform(at);
moving = false;
repaint();
}
}
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mouseClicked(MouseEvent event) { }
}
}
</code>
I have a small Java app that allows you to move shapes around by clicking and dragging. The dragged shape has a dashed line to distinguish it from the original.
Now here is my question: when you run the code included at the bottom of this post it works just great. However, I would like to use this in a Swing app. So if I change the Mover class from Frame to JFrame and the MoverPanel from Canvas to JPanel I would expect things to work still (except on line 14 where you have to change add(mp) to getContentPane().add(mp)); after all, all method calls that are used here are valid for JPanel as well. But it doesn't work correctly, the dragged shape leaves behind a "trail", cluttering the JPanel very quickly.
Could anyone tell me how to translate this code to use Swing?
Thanks very much, Jonck
<code>
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Mover extends Frame {
public static void main(String[] arg) {
new Mover();
}
Mover() {
super("Mover");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); } } );
MoverPanel mp = new MoverPanel();
add(mp);
pack();
show();
}
class MoverPanel extends Canvas
implements MouseListener, MouseMotionListener {
boolean moving = false;
int moveIndex;
int startx;
int starty;
int endx;
int endy;
Area[] area;
AffineTransform at;
BasicStroke dashes;
MoverPanel() {
addMouseListener(this);
addMouseMotionListener(this);
area = new Area[4];
area[0] = new Area(new Ellipse2D.Float(10,10,40,40));
area[1] = new Area(new Ellipse2D.Float(80,80,20,50));
area[2] = new Area(new Ellipse2D.Float(130,130,60,60));
area[3] = new Area(new Ellipse2D.Float(200,200,50,20));
setSize(300,250);
at = new AffineTransform();
float[] pattern = { 5f,5f };
dashes = new BasicStroke(1f,BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL,1f,pattern,0f);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for(int i=0; i<area.length; i++)
g2.draw(area);
if(moving) {
g2.setTransform(at);
g2.setStroke(dashes);
g2.draw(area[moveIndex]);
}
}
public void mouseMoved(MouseEvent event) {
moving = false;
}
public void mouseDragged(MouseEvent event) {
if(!moving)
return;
endx = event.getX();
endy = event.getY();
at.setToTranslation(endx - startx,endy - starty);
repaint();
}
public void mousePressed(MouseEvent event) {
moving = false;
startx = event.getX();
starty = event.getY();
for(int i=0; i<area.length; i++) {
if(area.contains(startx,starty)) {
moving = true;
moveIndex = i;
at.setToTranslation(0,0);
break;
}
}
}
public void mouseReleased(MouseEvent event) {
if(moving) {
area[moveIndex].transform(at);
moving = false;
repaint();
}
}
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mouseClicked(MouseEvent event) { }
}
}
</code>