一个小程序,所画图比较大,要实现拖动功能,可总是有问题,请高手指教.import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.Point;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class Draw_image extends JFrame {
  private JPanel jpanel = new JPanel();
  public static int draw_x = 0, draw_y = 0;
  private Point last=new Point();
  boolean isdraging=false;  Draw_image() {
    this.setSize(1024, 768);
    this.setVisible(true);
    jpanel.setSize(new Dimension(86400,1200));
    this.getContentPane().add(jpanel, BorderLayout.CENTER);
    jpanel.setDoubleBuffered(true);
      setCursor(Cursor.HAND_CURSOR);
      jpanel.addMouseListener(new MouseAdapter(){
//      Point last=new Point();
        public void mousePressed(MouseEvent e){
          last.x=e.getPoint().x;
          last.y=e.getPoint().y;
          isdraging=true;
        }
        public void mouseReleased(MouseEvent e){
          isdraging=false;
        }
    });
    jpanel.addMouseMotionListener(new MouseMotionAdapter(){
      public void mouseDragged(MouseEvent e){
        Point drag=new Point();
        drag=e.getPoint();
        draw_x=drag.x-last.x;
        draw_y=drag.y-last.y;
        last=drag;
        if(isdraging){
          jpanel.setLocation(draw_x,draw_y);
          repaint();
      }
      }
    });
  }  public void update(Graphics2D g) {    paint(g);
  }  public void paint(Graphics g) {
      Graphics og=jpanel.getGraphics();
//    super.paint(og);
    try {
  Graphics2D og2 = (Graphics2D) og;
  og2.setPaint(Color.blue);
og2.drawLine(50,100,2000,100);
og2.drawLine(50,400,2000,400);
og2.drawLine(80,100,200,400);
og2.drawLine(1200,100,1800,400);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }  public static void main(String[] args) {
    new Draw_image();
  }
}

解决方案 »

  1.   

    1.jpanel有setAutoscrolls方法,你为什么还自己写
    public void setAutoscrolls(boolean autoscrolls)Sets the autoscrolls property. If true mouse dragged events will be synthetically generated when the mouse is dragged outside of the component's bounds and mouse motion has paused (while the button continues to be held down). The synthetic events make it appear that the drag gesture has resumed in the direction established when the component's boundary was crossed. Components that support autoscrolling must handle mouseDragged events by calling scrollRectToVisible with a rectangle that contains the mouse event's location. All of the Swing components that support item selection and are typically displayed in a JScrollPane (JTable, JList, JTree, JTextArea, and JEditorPane) already handle mouse dragged events in this way. To enable autoscrolling in any other component, add a mouse motion listener that calls scrollRectToVisible. For example, given a JPanel, myPanel: 
     MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
         public void mouseDragged(MouseEvent e) {
            Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
            ((JPanel)e.getSource()).scrollRectToVisible(r);
        }
     };
     myPanel.addMouseMotionListener(doScrollRectToVisible);
     The default value of the autoScrolls property is false
    2.public void update(Graphics2D g) {    paint(g);
      }中传进的是Graphics2D,而public void paint(Graphics g) {
    3.drawLine中的位置可以改为用变量控制相对位置
    希望对你会有帮助