SOS

can anyone help me sort out the problem i have. i create a square and want to drag it to somewhere according to position of mouse. it works, but not what i want to. Here is the code, please have a look at it.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

import java.util.*;

public class myMouseTest{

public static void main(String[] args){

myMouseFrame mf=new myMouseFrame();

mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mf.setVisible(true);

}

}

class myMouseFrame extends JFrame{

public myMouseFrame(){

setTitle("My testing of mouse event");

setSize(400,500);

myMousePanel mp=new myMousePanel();

add(mp);

}

}

class myMousePanel extends JPanel{

private Rectangle2D temp;

private ArrayList<Rectangle2D> squares;

public myMousePanel(){

squares=new ArrayList<Rectangle2D>();

temp=null;

addMouseListener(new myMouseListener());

addMouseMotionListener(new myMouseMotionListener());

}

private void remove(Rectangle2D t){

squares.remove(t);

repaint();

}

private Rectangle2D find(Point2D t){

for(Rectangle2D s:squares)

if(s.contains(t))

return s;

return null;

}

private void add(Point2D t){

double x=t.getX();

double y=t.getY();

temp=new Rectangle2D.Double(

x-10/2,

y-10/2,

10,

10);

squares.add(temp);

repaint();

}

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D draw=(Graphics2D) g;

for(Rectangle2D t:squares)

draw.draw(t);

}

private class myMouseListener extends MouseAdapter{

public void mousePressed(MouseEvent e){

if((temp=find(e.getPoint()))==null)

add(e.getPoint());

}

public void mouseClicked(MouseEvent e){

if((temp=find(e.getPoint()))!=null && (e.getClickCount()==2))

remove(temp);

}

}

private class myMouseMotionListener implements MouseMotionListener{

public void mouseDragged(MouseEvent e){

if((temp=find(e.getPoint()))!=null){

temp.setFrame(e.getX(),e.getY(),10,10);

}

repaint();

}

public void mouseMoved(MouseEvent e){

if(find(e.getPoint())==null)

setCursor(Cursor.getDefaultCursor());

else

setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

}

}

}

[2559 byte] By [terry261a] at [2007-11-27 10:10:09]
# 1
Hi,you sure you in the right forum?
bonito_qqa at 2007-7-13 0:46:52 > top of Java-index,Security,Cryptography...