我想实现画图中画直线时的拉线效果。
既在mousePressed()方法中获取初始点,然后在mouseDragged()中不断获取当前点,画线并调用JPanel的repaint()方法,在mouseReleased()方法中得到结束点,并最终画出这条线。
以上拉线的代码都在mouseDragged()方法中完成。但是,当开始拉线后,一旦鼠标不动后(鼠标还没释放),mouseDragged()就停止调用了,似乎这个时候,系统自动调用了repaint(),导致拉出的线不见了,请问这个问题怎么解决,麻烦各位大虾看看

解决方案 »

  1.   

    release的时候调用repaint不就可以了
      

  2.   

    哦,给你个现成的代码吧:
    (Eastsun is me,also^_^)import java.awt.geom.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    /**
    *简单的画图程序
    *@author Eastsun
    *@version .1
    */
    public class DrawPanel extends JFrame{
      LinkedList<Shape> shapeList =new LinkedList<Shape>();
      Shape shape;
      Point start,end;
      final String[] type =new String[]{"Line","Rectangle","Ellipse"};
      JComboBox comboBox =new JComboBox(type);
      public DrawPanel(){
        super("DrawPanel");
        JPanel panel =new JPanel(){
            public void paintComponent(Graphics g){
              super.paintComponent(g);
              Graphics2D g2 =(Graphics2D)g;
              g2.setColor(Color.white);
              g2.fillRect(0,0,getWidth(),getHeight());
              g2.setColor(Color.black);
              for(Shape s:shapeList) g2.draw(s);
              g2.draw(shape);
            }
        };
        panel.addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
              start =e.getPoint();
            }
            public void mouseReleased(MouseEvent e){
              shapeList.add(shape);
            }
        });
        panel.addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
              end =e.getPoint();
              Object select =comboBox.getSelectedItem();
              if(select.equals(type[0])) shape =new Line2D.Float(start,end);
              else{ 
                if(select.equals(type[1])) shape =new Rectangle();
                else                 shape =new Ellipse2D.Float();
                ((RectangularShape)shape).setFrameFromDiagonal(start,end);
              }
              repaint();
            }
        });
        panel.setPreferredSize(new Dimension(320,240));
        add(panel,BorderLayout.NORTH);
        shape =new Rectangle();
        
        add(comboBox,BorderLayout.SOUTH);
        
        pack();
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        
      }
      public static void main(String[] args){
        new DrawPanel();
      }
    }
      

  3.   

    给你个已经实现的代码,LZ参考下package org.jfree;
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.CubicCurve2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.QuadCurve2D;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class CubicCurveMouse extends JFrame {
      DrawingCanvas canvas;  JLabel label = new JLabel("Mouse Location (x, y):  "); 
      JLabel coords = new JLabel("");  public CubicCurveMouse() {
        super();
        Container container = getContentPane();    JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));    panel.add(label);
      //panel.add(label);
        panel.setOpaque(false);    panel.add(coords);    container.add(panel, BorderLayout.SOUTH);    canvas = new DrawingCanvas();
        JPanel jpanel=new JPanel();
        jpanel.add(canvas);
        container.add(jpanel);
        addWindowListener(new WindowEventHandler());      
        pack();
        setSize(300,300);
        setVisible(true);
      }  class WindowEventHandler extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }  public static void main(String arg[]) {  new CubicCurveMouse();
      }  class DrawingCanvas extends Canvas {
          //x1,y1涓鸿捣濮嬬偣鍧愭爣锛寈c1cur, yc1cur绗&#65533;2娆$偣鍑绘椂鐐瑰潗鏍囷紝x4new, y4new绗竴娆℃澗鎵&#65533;
          //鏃剁邯褰曠殑鐐癸紝
        float x1, y1, xc1cur, yc1cur, xc1new, yc1new, xc2cur, yc2cur, xc2new,
            yc2new, x4cur, y4cur, x4new, y4new;    int pressNo = 0;     //鐐瑰嚮榧犳爣娆℃暟    int dragFlag1 = -1;    int dragFlag2 = -1;    boolean clearFlag = false;    float dashes[] = { 5f, 5f };    BasicStroke stroke;    public DrawingCanvas() {
          setBackground(Color.white);
          addMouseListener(new MyMouseListener());
          addMouseMotionListener(new MyMouseListener());
          setSize(400, 400);
          stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_BEVEL, 10f, dashes, 0f);
        }    public void update(Graphics g) {
          paint(g);
        }    public void paint(Graphics g) {
          Graphics2D g2D = (Graphics2D) g;      if (pressNo == 1) {
            g2D.setXORMode(getBackground());
            g2D.setColor(Color.red);
            g2D.setStroke(stroke);        // Erase the currently existing line
          g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur));
            // Draw the new line
            g2D.draw(new Line2D.Float(x1, y1, x4new, y4new));        // Update the currently existing coordinate values
            x4cur = x4new;
            y4cur = y4new;      }else if (pressNo == 2) {
            g2D.setXORMode(getBackground());
            g2D.setColor(Color.black);
            g2D.setStroke(stroke);        if (dragFlag1 != -1) {
              g2D.draw(new QuadCurve2D.Float(x1, y1, xc1cur, yc1cur,
                  x4new, y4new));
            }
            dragFlag1++; // Reset the drag-flag        g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new,
                y4new));        xc1cur = xc1new;
            yc1cur = yc1new;
          }else if (pressNo == 3) {
            g2D.setXORMode(getBackground());
            g2D.setColor(Color.black);        if (dragFlag2 != -1) {
              g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new,
                  xc2cur, yc2cur, x4new, y4new));
            }
            dragFlag2++; // Reset the drag flag
            g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new, xc2new,
                yc2new, x4new, y4new));
            xc2cur = xc2new;
            yc2cur = yc2new;
          }
          if (clearFlag) {
            g2D.setXORMode(getBackground());
            g2D.setColor(Color.black);
            g2D.setStroke(stroke);        g2D.draw(new Line2D.Float(x1, y1, x4new, y4new));
            g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new,
                y4new));
            clearFlag = false;
          }
        }    class MyMouseListener extends MouseAdapter implements MouseMotionListener {
          public void mousePressed(MouseEvent e) {
            if (pressNo == 0) { 
              pressNo++;
              x1 = x4cur = e.getX();
              y1 = y4cur = e.getY();
            } else if (pressNo == 1) {
              pressNo++;
              xc1cur = e.getX();
              yc1cur = e.getY();
            } else if (pressNo == 2) {
              pressNo++;
              xc2cur = e.getX();
              yc2cur = e.getY();
            }
          }
          public void mouseReleased(MouseEvent e) {
            if (pressNo == 1) {
              x4new = e.getX();
              y4new = e.getY();
              canvas.repaint();
            } else if (pressNo == 2) {
              xc1new = e.getX();
              yc1new = e.getY();
              canvas.repaint();
            } else if (pressNo == 3) {
              xc2new = e.getX();
              yc2new = e.getY();
              canvas.repaint();
              pressNo = 0;
              dragFlag1 = -1;
              dragFlag2 = -1;
              clearFlag = true;
            }
          }
          public void mouseDragged(MouseEvent e) {
            if (pressNo == 1) {
              x4new = e.getX();
              y4new = e.getY();
     
            } else if (pressNo == 2) {
              xc1new = e.getX();
              yc1new = e.getY();        } else if (pressNo == 3) {
              xc2new = e.getX();
              yc2new = e.getY();        }
            String string = "(" + Integer.toString(e.getX()) + ", "
            + Integer.toString(e.getY()) + ")";
            coords.setText(string);
            canvas.repaint();
          }      public void mouseMoved(MouseEvent e) {
            String string = "(" + Integer.toString(e.getX()) + ", "
                + Integer.toString(e.getY()) + ")";
            coords.setText(string);
          }
        }
      }
    }
      

  4.   

    时刻记住当前位置与初始位置,在paint函数中画就好了