Advertisement
xerpi

magia 2D

May 20th, 2015
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. package wikipedia;
  2.  
  3. import wikipedia.presentation.*;
  4. import wikipedia.domain.*;
  5. import wikipedia.persistence.*;
  6. import java.io.*;
  7. import g13.*;
  8. import java.awt.Image;
  9. import java.awt.BorderLayout;
  10. import java.awt.Color;
  11. import java.awt.Dimension;
  12. import java.awt.Point;
  13. import java.awt.Toolkit;
  14. import java.awt.event.MouseAdapter;
  15. import java.awt.event.MouseEvent;
  16. import java.awt.image.BufferedImage;
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.border.EmptyBorder;
  22. import com.mxgraph.model.mxCell;
  23. import com.mxgraph.swing.mxGraphComponent;
  24. import com.mxgraph.util.mxCellRenderer;
  25. import com.mxgraph.view.mxGraph;
  26. import javax.swing.SwingUtilities;
  27. import javax.swing.JFrame;
  28. import javax.swing.JPanel;
  29. import javax.swing.BorderFactory;
  30. import java.awt.Color;
  31. import java.awt.Dimension;
  32. import java.awt.Graphics;
  33. import java.awt.event.*;
  34.  
  35.  
  36. class MyPanel extends JPanel {
  37.  
  38.     final private static int PANEL_W = 1080;
  39.     final private static int PANEL_H = 720;
  40.     final private static double ZOOM_FACTOR = 0.75;
  41.  
  42.     private BufferedImage orig_img;
  43.     private int scroll_x;
  44.     private int scroll_y;
  45.     private int scaled_w;
  46.     private int scaled_h;  
  47.     private int img_w;
  48.     private int img_h;
  49.     private int img_x;
  50.     private int img_y;
  51.     private int mouse_x;
  52.     private int mouse_y;
  53.     private mxGraph mxg;
  54.     private mxGraphComponent mxgc;
  55.  
  56.     public MyPanel(OGraph g) {
  57.  
  58.         mxg = g.toMxGraph();
  59.         mxgc = new mxGraphComponent(mxg);
  60.  
  61.         orig_img = mxCellRenderer.createBufferedImage(mxg, null, 1, Color.WHITE, true, null);
  62.         final int orig_w = orig_img.getWidth();
  63.         final int orig_h = orig_img.getHeight();
  64.  
  65.         img_w = orig_w;
  66.         img_h = orig_h;
  67.         img_x = 0;
  68.         img_y = 0;
  69.         scaled_w = img_w;
  70.         scaled_h = img_h;
  71.  
  72.         addMouseListener(new MouseAdapter() {
  73.             public void mousePressed(MouseEvent e) {
  74.                 graphShit(e.getX(),e.getY());
  75.                 setScrollOrigin(e.getX(),e.getY());
  76.             }      
  77.         });
  78.  
  79.         addMouseMotionListener(new MouseAdapter() {
  80.             public void mouseDragged(MouseEvent e) {
  81.                 moveImage(e.getX(),e.getY());
  82.                 setScrollOrigin(e.getX(),e.getY());
  83.             }
  84.             public void mouseMoved(MouseEvent e) {
  85.                 mouse_x = e.getX();
  86.                 mouse_y = e.getY();
  87.             }  
  88.         });
  89.  
  90.         addMouseWheelListener(new MouseAdapter() {
  91.             public void mouseWheelMoved(MouseWheelEvent e) {
  92.                 double zoom = ZOOM_FACTOR;
  93.                 if (e.getWheelRotation() == -1) zoom = 1/zoom;
  94.                
  95.                 double new_w = scaled_w * zoom;
  96.                 double new_h = scaled_h * zoom;
  97.  
  98.                 if (new_w < 300 || new_h < 300) return;
  99.                 if (new_w > img_w*3 || new_h > img_h*3) return;
  100.  
  101.                 scaled_w = (int)new_w;
  102.                 scaled_h = (int)new_h;
  103.  
  104.                 int zoom_x = mouse_x;
  105.                 int zoom_y = mouse_y;
  106.                 if (zoom_x > (img_x + img_w)) zoom_x = (img_x + img_w);
  107.                 else if (zoom_x < img_x) zoom_x = img_x;
  108.  
  109.                 img_x = (int)((mouse_x - img_x) + img_w*zoom);
  110.                 img_y = (int)((mouse_y - img_y) + img_h*zoom);
  111.  
  112.                 repaint();
  113.             }
  114.            
  115.         });
  116.     }
  117.  
  118.     private void graphShit(int x, int y) {
  119.         // Transform coordinates depending on the window's size
  120.         int trans_x = (int)((x - img_x) * (img_w/(double)scaled_w));
  121.         int trans_y = (int)((y - img_y) * (img_h/(double)scaled_h));
  122.  
  123.         Object obj = mxgc.getCellAt(trans_x, trans_y);
  124.         //System.out.println("x: " + x + "  y: " + y);
  125.         if (obj != null) {
  126.             mxCell cell = (mxCell)obj;
  127.             System.out.println(cell.getId());
  128.         }
  129.     }
  130.  
  131.     private void setScrollOrigin(int x, int y) {
  132.         scroll_x = x;
  133.         scroll_y = y;
  134.     }
  135.  
  136.     private void moveImage(int x, int y) {
  137.         img_x = img_x + (x - scroll_x);
  138.         img_y = img_y + (y - scroll_y);
  139.         repaint();
  140.     }
  141.  
  142.  
  143.     public Dimension getPreferredSize() {
  144.         return new Dimension(PANEL_W, PANEL_H);
  145.     }
  146.  
  147.     protected void paintComponent(Graphics g) {
  148.         super.paintComponent(g);      
  149.         g.drawImage(orig_img, img_x, img_y, scaled_w, scaled_h, null);
  150.     }  
  151. }
  152.  
  153. /**
  154.  * Main Class
  155.  * @author G13.2
  156.  */
  157. public class Main
  158. {
  159.     private static void createAndShowGUI(OGraph g) {
  160.         System.out.println("Created GUI on EDT? "+
  161.         SwingUtilities.isEventDispatchThread());
  162.         JFrame f = new JFrame("Swing Paint Demo");
  163.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  164.         f.add(new MyPanel(g));
  165.         f.pack();
  166.         f.setResizable(false);
  167.         f.setVisible(true);
  168.     }
  169.  
  170.     /**
  171.     * The main method
  172.     * @param args Arguments of main
  173.     */
  174.     public static void main(String[] args) {
  175.  
  176.         WP wikipedia = new WP();
  177.         wikipedia.setGraph(GraphIO.loadWP(new File("golden/ENTRADAcodigo.txt")));
  178.         final OGraph g = wikipedia.getGraph();
  179.  
  180.         SwingUtilities.invokeLater(new Runnable() {
  181.             public void run() {
  182.                 createAndShowGUI(g);
  183.             }
  184.         });
  185.        
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement