<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
2 import java.awt. * ;
3 import javax.swing. * ;
4 import java.awt.event. * ;
5 import javax.swing.event. * ;
6
7 public class Main
8 {
9 public static void main (String args[])
10 {
11 JFrame f = new JFrame ();
12 f.setLayout ( null );
13 NEWImagepanel pan = new NEWImagepanel ( " /1.jpg " );
14 pan.setPreferredSize ( new Dimension ( 800 , 800 ));
15 JScrollPane jspan = new JScrollPane (pan);
16 jspan.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
17 jspan.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_NEVER);
18 jspan.setBounds ( 0 , 0 , 400 , 400 );
19 f.setSize ( 400 , 500 );
20 f.add ( " Center " ,jspan);
21 f.setVisible ( true );
22 f.setDefaultCloseOperation (f.EXIT_ON_CLOSE);
23 }
24 }
25 class NEWImagepanel extends JPanel implements MouseListener ,MouseInputListener
26 {
27 private Image bgimg;
28 int anchorX = 0 ;
29 int anchorY = 0 ;
30
31 public NEWImagepanel ()
32 {
33 super ();
34 }
35 // 参数所指图片文件的路径
36 public NEWImagepanel (String path)
37 {
38
39 super ();
40
41 MediaTracker tracker = new MediaTracker ( this );
42 bgimg = new ImageIcon (getClass ().getResource (path)).getImage ();
43 tracker.addImage (bgimg, 0 );
44 try
45 {
46 tracker.waitForAll ();
47 }
48 catch (Exception e)
49 {
50 e.printStackTrace ();
51 }
52 addMouseMotionListener ( this );
53 addMouseListener ( this );
54
55 }
56 public void paintComponent (Graphics g)
57 {
58 if (bgimg == null )
59 {
60 super .paintComponent (g);
61 }
62 g.drawImage (bgimg, 0 , 0 , this .getWidth (), this .getHeight (), this );
63 // g.drawImage (bgimg ,0,0,null);
64 }
65
66 public void mouseClicked (MouseEvent e)
67 {
68 }
69
70 public void mousePressed (MouseEvent e)
71 {
72 anchorX = e.getX ();
73 anchorY = e.getY ();
74 try
75 {
76 setCursor (Cursor.getPredefinedCursor (Cursor.MOVE_CURSOR));
77 }
78 catch (Exception ex)
79 {
80 System.out.println (ex.toString ());
81 }
82 }
83
84 public void mouseReleased (MouseEvent e)
85 {
86 setCursor (Cursor.getDefaultCursor ());
87 }
88
89 public void mouseEntered (MouseEvent e)
90 {
91 }
92
93 public void mouseExited (MouseEvent e)
94 {
95 }
96
97 public void mouseDragged (MouseEvent e)
98 {
99 int x = e.getX ();
100 int y = e.getY ();
101 Container parent = getParent ();
102 if (parent instanceof JViewport)
103 {
104 JViewport viewport = (JViewport)parent;
105 Point p = viewport.getViewPosition ();
106 p.translate (anchorX - x ,anchorY - y);
107 p.x = Math.max (p.x, 0 );
108 p.y = Math.max (p.y, 0 );
109 p.x = Math.min (p.x,getWidth () - viewport.getWidth ());
110 p.y = Math.min (p.y , getHeight () - viewport.getHeight ());
111 viewport.setViewPosition (p);
112 }
113 }
114
115 public void mouseMoved (MouseEvent e)
116 {
117 }
118
119 }
120
2 import java.awt. * ;
3 import javax.swing. * ;
4 import java.awt.event. * ;
5 import javax.swing.event. * ;
6
7 public class Main
8 {
9 public static void main (String args[])
10 {
11 JFrame f = new JFrame ();
12 f.setLayout ( null );
13 NEWImagepanel pan = new NEWImagepanel ( " /1.jpg " );
14 pan.setPreferredSize ( new Dimension ( 800 , 800 ));
15 JScrollPane jspan = new JScrollPane (pan);
16 jspan.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
17 jspan.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_NEVER);
18 jspan.setBounds ( 0 , 0 , 400 , 400 );
19 f.setSize ( 400 , 500 );
20 f.add ( " Center " ,jspan);
21 f.setVisible ( true );
22 f.setDefaultCloseOperation (f.EXIT_ON_CLOSE);
23 }
24 }
25 class NEWImagepanel extends JPanel implements MouseListener ,MouseInputListener
26 {
27 private Image bgimg;
28 int anchorX = 0 ;
29 int anchorY = 0 ;
30
31 public NEWImagepanel ()
32 {
33 super ();
34 }
35 // 参数所指图片文件的路径
36 public NEWImagepanel (String path)
37 {
38
39 super ();
40
41 MediaTracker tracker = new MediaTracker ( this );
42 bgimg = new ImageIcon (getClass ().getResource (path)).getImage ();
43 tracker.addImage (bgimg, 0 );
44 try
45 {
46 tracker.waitForAll ();
47 }
48 catch (Exception e)
49 {
50 e.printStackTrace ();
51 }
52 addMouseMotionListener ( this );
53 addMouseListener ( this );
54
55 }
56 public void paintComponent (Graphics g)
57 {
58 if (bgimg == null )
59 {
60 super .paintComponent (g);
61 }
62 g.drawImage (bgimg, 0 , 0 , this .getWidth (), this .getHeight (), this );
63 // g.drawImage (bgimg ,0,0,null);
64 }
65
66 public void mouseClicked (MouseEvent e)
67 {
68 }
69
70 public void mousePressed (MouseEvent e)
71 {
72 anchorX = e.getX ();
73 anchorY = e.getY ();
74 try
75 {
76 setCursor (Cursor.getPredefinedCursor (Cursor.MOVE_CURSOR));
77 }
78 catch (Exception ex)
79 {
80 System.out.println (ex.toString ());
81 }
82 }
83
84 public void mouseReleased (MouseEvent e)
85 {
86 setCursor (Cursor.getDefaultCursor ());
87 }
88
89 public void mouseEntered (MouseEvent e)
90 {
91 }
92
93 public void mouseExited (MouseEvent e)
94 {
95 }
96
97 public void mouseDragged (MouseEvent e)
98 {
99 int x = e.getX ();
100 int y = e.getY ();
101 Container parent = getParent ();
102 if (parent instanceof JViewport)
103 {
104 JViewport viewport = (JViewport)parent;
105 Point p = viewport.getViewPosition ();
106 p.translate (anchorX - x ,anchorY - y);
107 p.x = Math.max (p.x, 0 );
108 p.y = Math.max (p.y, 0 );
109 p.x = Math.min (p.x,getWidth () - viewport.getWidth ());
110 p.y = Math.min (p.y , getHeight () - viewport.getHeight ());
111 viewport.setViewPosition (p);
112 }
113 }
114
115 public void mouseMoved (MouseEvent e)
116 {
117 }
118
119 }
120