Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Point;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.event.MouseWheelEvent;
  10. import java.awt.event.MouseWheelListener;
  11. import java.awt.geom.AffineTransform;
  12.  
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16.  
  17.  
  18. public class MyAppFrame extends JFrame {
  19.  
  20. private JScrollPane scroll;
  21. private JPanel nullLayoutPanel;
  22. private JPanel subPanel;
  23. private double zoomFactor;
  24. private static Point pressed;
  25.  
  26. public MyAppFrame(){
  27. setSize(200, 200);
  28. setLocation(50, 50);
  29. zoomFactor = 1.0;
  30.  
  31. nullLayoutPanel = new JPanel(){
  32. private void zoom() {
  33. //the zoom logic should perform in here
  34.  
  35.  
  36. }
  37.  
  38. //when you try it with the paint method you'll see my problem
  39. //move the rect by dragging it around then
  40. //scroll out or in and try to move the rect
  41. //you'll see that the position of the logical rect doesn't fit with the painted one
  42. @Override
  43. public void paint(Graphics g){
  44. Graphics2D g2d = (Graphics2D) g;
  45. AffineTransform old = g2d.getTransform();
  46. g2d.scale(zoomFactor, zoomFactor);
  47. super.paint(g);
  48. //old.translate(zoomFactor, zoomFactor);
  49. g2d.setTransform(old);
  50. }
  51.  
  52. };
  53. nullLayoutPanel.setPreferredSize(new Dimension(400,400));
  54. nullLayoutPanel.setLayout(null);
  55. nullLayoutPanel.addMouseWheelListener(new MouseWheelListener() {
  56.  
  57. @Override
  58. public void mouseWheelMoved(MouseWheelEvent e) {
  59. if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL){
  60. if((.1 * e.getWheelRotation()) > 0 && zoomFactor < 2.0){
  61. zoomFactor += (.1 * e.getWheelRotation());
  62. } else if((.1 * e.getWheelRotation()) < 0 && zoomFactor > 0.1){
  63. zoomFactor += (.1 * e.getWheelRotation());
  64. }
  65. zoomFactor = Math.round(zoomFactor*10.0)/10.0;
  66. nullLayoutPanel.repaint();
  67. }
  68. }
  69. });
  70.  
  71.  
  72. scroll = new JScrollPane(nullLayoutPanel);
  73. scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  74. scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  75. getContentPane().add(scroll);
  76.  
  77. subPanel = new JPanel(){
  78. @Override
  79. public void paint(Graphics g){
  80. g.setColor(Color.BLUE);
  81. g.fillRect(0, 0, this.getWidth(), this.getHeight());
  82. }
  83. };
  84.  
  85. subPanel.addMouseListener(new MouseListener() {
  86.  
  87. @Override
  88. public void mouseReleased(MouseEvent e) {
  89. // TODO Auto-generated method stub
  90.  
  91. }
  92.  
  93. @Override
  94. public void mousePressed(MouseEvent e) {
  95. pressed = e.getPoint();
  96. }
  97.  
  98. @Override
  99. public void mouseExited(MouseEvent e) {
  100. // TODO Auto-generated method stub
  101.  
  102. }
  103.  
  104. @Override
  105. public void mouseEntered(MouseEvent e) {
  106. // TODO Auto-generated method stub
  107.  
  108. }
  109.  
  110. @Override
  111. public void mouseClicked(MouseEvent e) {
  112. // TODO Auto-generated method stub
  113.  
  114. }
  115. });
  116. subPanel.addMouseMotionListener(new MouseMotionListener() {
  117.  
  118. @Override
  119. public void mouseMoved(MouseEvent e) {
  120. // TODO Auto-generated method stub
  121.  
  122. }
  123.  
  124. @Override
  125. public void mouseDragged(MouseEvent e) {
  126. Point pPoint;
  127. pPoint = subPanel.getLocation();
  128. int x = (int)(pPoint.x - pressed.getX()+e.getX());
  129. int y = (int)(pPoint.y - pressed.getY()+e.getY());
  130. if(x>0&&y>0)
  131. subPanel.setLocation(x,y);
  132. }
  133. });
  134.  
  135. subPanel.setBounds(10, 10, 10, 10);
  136. nullLayoutPanel.add(subPanel);
  137.  
  138.  
  139. setVisible(true);
  140.  
  141. }
  142.  
  143. /**
  144. * @param args
  145. */
  146. public static void main(String[] args) {
  147. new MyAppFrame();
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement