package cn.njupt.Swing;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.JFrame;
@SuppressWarnings("all")
public class MouseComponent extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares ;
private Rectangle2D current ;//the squares containing the mouse cursor
public MouseComponent() {
squares = new ArrayList<>();
current = null ;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public Dimension getPreferredSize() {
return new Dimension(DEFAULT_WIDTH ,DEFAULT_HEIGHT);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// draw all squares
for(Rectangle2D r :squares)
g2.draw(r);
}
// find the first square containing a point
public Rectangle2D find(Point2D p) {
for(Rectangle2D r :squares) {
if(r.contains(p))
return r;
}
return null;
}
//add a square to the collection
public void add(Point2D p) {
double x = p.getX();
double y = p.getY();
current = new Rectangle2D.Double(x-SIDELENGTH/2, y-SIDELENGTH/2,
SIDELENGTH, SIDELENGTH);
squares.add(current);
repaint();
}
// removes a square from the collection
public void remove (Rectangle2D s ) {
if(s==null)
return;
if(s==current)
current =null;
squares.remove(s);
repaint();
}
private class MouseHandler extends MouseAdapter{
public void mousePressed(MouseEvent event) {
//add a new square if the cursor is not inside a square
current = find(event.getPoint());
if(current == null)
add(event.getPoint());
}
public void mouseClicked(MouseEvent event) {
// remove the current square if double clicked
current = find(event.getPoint());
if(current!=null&event.getClickCount()>=2)
remove(current);
}
}
class MouseMotionHandler implements MouseMotionListener{
@Override
public void mouseMoved(MouseEvent event) {
//set the mouse cursor to cross hairs if it is inside
if (find(event.getPoint())==null)
setCursor(Cursor.getDefaultCursor());
else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
@Override
public void mouseDragged(MouseEvent event) {
if(current!=null) {
int x = event.getX();
int y =event.getY();
//drag the current rectangle to center it at (x,y)
current.setFrame(x-SIDELENGTH/2, y-SIDELENGTH/2,
SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.JFrame;
@SuppressWarnings("all")
public class MouseComponent extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares ;
private Rectangle2D current ;//the squares containing the mouse cursor
public MouseComponent() {
squares = new ArrayList<>();
current = null ;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public Dimension getPreferredSize() {
return new Dimension(DEFAULT_WIDTH ,DEFAULT_HEIGHT);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// draw all squares
for(Rectangle2D r :squares)
g2.draw(r);
}
// find the first square containing a point
public Rectangle2D find(Point2D p) {
for(Rectangle2D r :squares) {
if(r.contains(p))
return r;
}
return null;
}
//add a square to the collection
public void add(Point2D p) {
double x = p.getX();
double y = p.getY();
current = new Rectangle2D.Double(x-SIDELENGTH/2, y-SIDELENGTH/2,
SIDELENGTH, SIDELENGTH);
squares.add(current);
repaint();
}
// removes a square from the collection
public void remove (Rectangle2D s ) {
if(s==null)
return;
if(s==current)
current =null;
squares.remove(s);
repaint();
}
private class MouseHandler extends MouseAdapter{
public void mousePressed(MouseEvent event) {
//add a new square if the cursor is not inside a square
current = find(event.getPoint());
if(current == null)
add(event.getPoint());
}
public void mouseClicked(MouseEvent event) {
// remove the current square if double clicked
current = find(event.getPoint());
if(current!=null&event.getClickCount()>=2)
remove(current);
}
}
class MouseMotionHandler implements MouseMotionListener{
@Override
public void mouseMoved(MouseEvent event) {
//set the mouse cursor to cross hairs if it is inside
if (find(event.getPoint())==null)
setCursor(Cursor.getDefaultCursor());
else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
@Override
public void mouseDragged(MouseEvent event) {
if(current!=null) {
int x = event.getX();
int y =event.getY();
//drag the current rectangle to center it at (x,y)
current.setFrame(x-SIDELENGTH/2, y-SIDELENGTH/2,
SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}