当前位置: 代码迷 >> Java相关 >> 功能实现不了
  详细解决方案

功能实现不了

热度:228   发布时间:2006-11-29 10:08:07.0

刚写了个简单的例子,
你看看吧你自己试着改进看看

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


----------------解决方案--------------------------------------------------------
谢谢,我看一下吧
----------------解决方案--------------------------------------------------------
可是每次画完,再画时,先前画的怎么就没有了?
----------------解决方案--------------------------------------------------------

因为我调用了super.paintComponent方法
否则的话,就会出现一片,很难看的


----------------解决方案--------------------------------------------------------
还是版主厉害,经验丰富!
----------------解决方案--------------------------------------------------------

鼠标拖拽完之后的图怎么保留下来呢?


----------------解决方案--------------------------------------------------------
你可以把它放到JAVA的任何一个数据结构里面去
----------------解决方案--------------------------------------------------------
我是说保留前一个图之后 然后在此基础上继续画图
----------------解决方案--------------------------------------------------------

这就需要你自定义一些类来实现了
你可以定义一个类叫Oval,它可以保存椭圆的一些数据
也可以定义一个类叫Line,它可以保存线的数据
如此这般,就可以了,
然后把它们都存到一个集合里去
画的时候,全部取出来,再调用它们的drawSelf方法,当然这个方法是你先定义好了的
这样就是多态的实现


----------------解决方案--------------------------------------------------------

收到 ! 多谢!


----------------解决方案--------------------------------------------------------
  相关解决方案