首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java Graphics2D绘图谋求 -组合/取消组合- 思路

2011-12-10 
Java Graphics2D绘图寻求 -组合/取消组合- 思路!现在java做一个简单绘图软件,想实现对多个Graphics2D对象

Java Graphics2D绘图寻求 -组合/取消组合- 思路!
现在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();


}


private static final int SIDELENGTH = 10;
private ArrayList <Rectangle2D> squares;
private Rectangle2D current;
// the square containing the mouse cursor

private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
// add a new square if the cursor isn 't 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);
}
}

private class MouseMotionHandler
implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
// set the mouse cursor to cross hairs if it is inside
// a rectangle

if (find(event.getPoint()) == null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}

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();
}
}
}
}

这是Core Java 卷I 第8章 第4节关于鼠标事件的例子, 虽然没有关于如何组合/取消, 但演示了如何对绘制矩形, 删除矩形和拖动矩形. 不知是否对你有所帮助...

我想了一下, 这里应该可以应用Composite模式. 让好几个图形的组合作为一个整体, 也实现Shape接口, 而这个Composite组合本身包含一个ArrayList <Shape> , 里面存放的是所有其它的图形. 实现了Shape你就可以对它进行绘制, 判断一个点是否在这个图形组合间等.

现在我们宿舍要停电了, 晚上给你好好想想, 明天再来看看有没有好的解决方案...
[解决办法]
package com.andnnl.graphics;

public interface MyGraphics {
public void draw();
}


/*
* 创建日期 2007-2-2
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package com.andnnl.graphics;

import java.awt.Graphics;


/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Line implements MyGraphics {


public void draw() {
System.out.println( "line ");

}


}


/*
* 创建日期 2007-2-2
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package com.andnnl.graphics;

import java.util.ArrayList;
import java.util.Iterator;


public class MultGrihics implements MyGraphics{

private ArrayList <MyGraphics> list=new ArrayList <MyGraphics> ();

public void draw() {
for(Iterator it=list.iterator();it.hasNext();){
MyGraphics mg=(MyGraphics) it.next();
mg.draw();
}
}

public void addGraphics(MyGraphics mg){
list.add(mg);
}

public void removeGraphics(MyGraphics mg) {
list.remove(mg);
}
}


/*
* 创建日期 2007-2-2
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/


package com.andnnl.graphics;

/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Rect implements MyGraphics {

/* (非 Javadoc)
* @see com.andnnl.graphics.MyGraphics#draw()
*/
public void draw() {
System.out.println( "rect ");
}


}


/*
* 创建日期 2007-2-2
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package com.andnnl.graphics;

/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Test {

public static void main(String[] args) {
MyGraphics mg1=new Line();
MyGraphics mg2=new Rect();

MultGrihics mgs=new MultGrihics();
mgs.addGraphics(mg1);
mgs.addGraphics(mg2);
mgs.draw();
}
}

热点排行