刚写了个简单的例子,
你看看吧你自己试着改进看看
/*
* TestDraw.java
*
* Created on 2006年11月29日, 上午9:35
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author lbf
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestDraw extends JFrame implements ActionListener{
private JRadioButton jr1,jr2,jr3,jr4;
private int drawWhat=Const.DRAW_LINE;
private int startX,startY,endX,endY;
/** Creates a new instance of TestDraw */
public TestDraw() {
initWindow();
}
private void initWindow(){
JPanel jrPanel=new JPanel();
DrawPanel dp=new DrawPanel();
jrPanel.add(jr1=new JRadioButton("任意线段",true));
jrPanel.add(jr2=new JRadioButton("画直线"));
jrPanel.add(jr3=new JRadioButton("画椭圆"));
jrPanel.add(jr4=new JRadioButton("画矩形"));
ButtonGroup bg=new ButtonGroup();
bg.add(jr1);
bg.add(jr2);
bg.add(jr3);
bg.add(jr4);
jr1.addActionListener(this);
jr2.addActionListener(this);
jr3.addActionListener(this);
jr4.addActionListener(this);
dp.setBorder(BorderFactory.createTitledBorder("画图区"));
jrPanel.setBorder(BorderFactory.createTitledBorder("选择区"));
this.getContentPane().add(jrPanel,BorderLayout.SOUTH);
this.getContentPane().add(dp,BorderLayout.CENTER);
this.setSize(400,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==jr1){
drawWhat=Const.DRAW_LINE;
} else if(ae.getSource()==jr2){
drawWhat=Const.DRAW_LINE_TO;
}else if(ae.getSource()==jr3){
drawWhat=Const.DRAW_OVAL;
}else if(ae.getSource()==jr4){
drawWhat=Const.DRAW_RECT;
}
}
private class DrawPanel extends JPanel implements MouseListener,MouseMotionListener{
public DrawPanel(){
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mouseEntered(MouseEvent me){
}
public void mouseExited(MouseEvent me){
}
public void mousePressed(MouseEvent me){
startX=me.getX();
startY=me.getY();
endX=me.getX();
endY=me.getY();
}
public void mouseClicked(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
endX=me.getX();
endY=me.getY();
repaint();
}
public void mouseMoved(MouseEvent me){
}
public void mouseDragged(MouseEvent me){
if(drawWhat==Const.DRAW_LINE){
startX=endX;
startY=endY;
}
endX=me.getX();
endY=me.getY();
repaint();
}
public void paintComponent(Graphics g){
if(drawWhat==Const.DRAW_LINE){
g.drawLine(startX,startY,endX,endY);
} else if(drawWhat==Const.DRAW_OVAL){
super.paintComponent(g);
g.drawOval(startX,startY,endX-startX,endY-startY);
}else if(drawWhat==Const.DRAW_RECT){
super.paintComponent(g);
g.drawRect(startX,startY,endX-startX,endY-startY);
}else if(drawWhat==Const.DRAW_LINE_TO){
super.paintComponent(g);
g.drawLine(startX,startY,endX,endY);
}
}
}
protected static class Const{
public static final int DRAW_LINE=1;
public static final int DRAW_OVAL=2;
public static final int DRAW_RECT=3;
public static final int DRAW_LINE_TO=4;
}
public static void main(String[] args) {
new TestDraw();
}
}
可惜不是你,陪我到最后