日期:2014-05-20  浏览次数:20693 次

兄弟帮下忙!感激不尽!JAVA2D绘图中怎么实现画的图象被选中?还有实现图象的移动?
JAVA2D绘图中怎么实现画的图象被选中?还有实现图象的移动?这个问题搞到我好痛苦!日思夜想!


------解决方案--------------------
找了一个java的画图程序,希望有帮助;

/*
* 画图程序 来自bc-cn论坛
* java applet 与windows自带的程序类似,功能简单
*
*
*/


import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Test extends JPanel implements Runnable,ActionListener,MouseListener,MouseMotionListener {

private JButton clear,draw,replay,btnColor;//清除,开始画和重放和颜色的按钮
Color choosedColor = Color.BLACK;//选中的颜色
JDialog dialog = null;
private LinkedList <MyShape> save;//保存起来的形状

private int flag=0;//定义一个标志,用它来传递当前是在干什么
private final int DRAW=1;//1代表当前正在画图
private final int REPLAY=2;//2代表当前正在重放

private LinkedList <Pen> ps;//保存点

private volatile boolean go;
/** Creates a new instance of Test */

public Test() {
initOther();
initWindow();
}

private void initOther(){
clear=new JButton( "清除 ");
draw=new JButton( "开始画 ");
replay=new JButton( "重放 ");
btnColor = new JButton( "选取颜色 ");//选颜色

clear.addActionListener(this);
draw.addActionListener(this);
replay.addActionListener(this);

save=new LinkedList <MyShape> ();
ps=new LinkedList <Pen> ();
clear.setEnabled(false);
replay.setEnabled(false);
}
private void initWindow(){
JFrame jf=new JFrame( "画板 Dream ");
JPanel down=new JPanel();
down.add(clear);
down.add(draw);
down.add(replay);
down.add(btnColor);
this.setBorder(BorderFactory.createTitledBorder( "绘画区 "));
jf.add(this,BorderLayout.CENTER);
jf.add(down,BorderLayout.SOUTH);
jf.setSize(500,450);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnColor.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final JColorChooser chooser = new JColorChooser();
dialog = JColorChooser.createDialog(null, "选取颜色 ", false, chooser, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
choosedColor = chooser.getColor();
}
}, null);
dialog.setVisible(true);
}
});
}

protected void paintComponent(Graphics g){
super.paintComponent(g);
if(flag==DRAW){
for(MyShape my:save){
my.drawMe(g);
}