现在java做一个简单绘图软件,想实现 对多个Graphics2D对象的组合和取消组合功能,类似于在Word中绘图的组合和取消组合,没有思路如何做,有高手给个思路或者例程,高分相送。
具体可以描述为 在绘图程序同时选中 几个不同的 几何图像对象 要求实现 对他们进行组合和取消组合的操作。目的是对一个组合后的图形对象进行拖拽等操作。
比较急,高手出手相助了。。。
------解决方案--------------------
public class ItemGroup
{
ArrayList <Item> arrayList=new ArrayList <Item> ();
add(Item item)
remove(Item item)
}
class Item
{
ItemGroup group;
move()
{
if(group==null)
//只移动自身
else
//group内的所有Item都移动
}
}
------解决方案--------------------
组合和取消组合的操作
在JDK自带的demo里面有个例子的
拖动的话可以参考楼上的
要记录鼠标点
使用MouseEvent
getX()和getY()方法可以取得当前鼠标点的位置
------解决方案--------------------
/**
@version 1.31 2004-05-04
@author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle( "MouseTest ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
MousePanel panel = new MousePanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
/**
A panel with mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares = new ArrayList <Rectangle2D> ();
current = null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// draw all squares
for (Rectangle2D r : squares)
g2.draw(r);
}
/**
Finds the first square containing a point.
@param p a point
@return the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r : squares)
{
if (r.contains(p)) return r;
}
return null;
}
/**
Adds a square to the collection.
@param p the center of the square
*/
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.
@param s the square to remove
*/
public void remove(Rectangle2D s)
{
if (s == null) return;
if (s == current) current = null;
squares.remove(s);
repaint();