Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wikipedia;
- import wikipedia.presentation.*;
- import wikipedia.domain.*;
- import wikipedia.persistence.*;
- import java.io.*;
- import g13.*;
- import java.awt.Image;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.image.BufferedImage;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import com.mxgraph.model.mxCell;
- import com.mxgraph.swing.mxGraphComponent;
- import com.mxgraph.util.mxCellRenderer;
- import com.mxgraph.view.mxGraph;
- import javax.swing.SwingUtilities;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.BorderFactory;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.*;
- class MyPanel extends JPanel {
- final private static int PANEL_W = 1080;
- final private static int PANEL_H = 720;
- final private static double ZOOM_FACTOR = 0.75;
- private BufferedImage orig_img;
- private int scroll_x;
- private int scroll_y;
- private int scaled_w;
- private int scaled_h;
- private int img_w;
- private int img_h;
- private int img_x;
- private int img_y;
- private int mouse_x;
- private int mouse_y;
- private mxGraph mxg;
- private mxGraphComponent mxgc;
- public MyPanel(OGraph g) {
- mxg = g.toMxGraph();
- mxgc = new mxGraphComponent(mxg);
- orig_img = mxCellRenderer.createBufferedImage(mxg, null, 1, Color.WHITE, true, null);
- final int orig_w = orig_img.getWidth();
- final int orig_h = orig_img.getHeight();
- img_w = orig_w;
- img_h = orig_h;
- img_x = 0;
- img_y = 0;
- scaled_w = img_w;
- scaled_h = img_h;
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- graphShit(e.getX(),e.getY());
- setScrollOrigin(e.getX(),e.getY());
- }
- });
- addMouseMotionListener(new MouseAdapter() {
- public void mouseDragged(MouseEvent e) {
- moveImage(e.getX(),e.getY());
- setScrollOrigin(e.getX(),e.getY());
- }
- public void mouseMoved(MouseEvent e) {
- mouse_x = e.getX();
- mouse_y = e.getY();
- }
- });
- addMouseWheelListener(new MouseAdapter() {
- public void mouseWheelMoved(MouseWheelEvent e) {
- double zoom = ZOOM_FACTOR;
- if (e.getWheelRotation() == -1) zoom = 1/zoom;
- double new_w = scaled_w * zoom;
- double new_h = scaled_h * zoom;
- if (new_w < 300 || new_h < 300) return;
- if (new_w > img_w*3 || new_h > img_h*3) return;
- scaled_w = (int)new_w;
- scaled_h = (int)new_h;
- int zoom_x = mouse_x;
- int zoom_y = mouse_y;
- if (zoom_x > (img_x + img_w)) zoom_x = (img_x + img_w);
- else if (zoom_x < img_x) zoom_x = img_x;
- img_x = (int)((mouse_x - img_x) + img_w*zoom);
- img_y = (int)((mouse_y - img_y) + img_h*zoom);
- repaint();
- }
- });
- }
- private void graphShit(int x, int y) {
- // Transform coordinates depending on the window's size
- int trans_x = (int)((x - img_x) * (img_w/(double)scaled_w));
- int trans_y = (int)((y - img_y) * (img_h/(double)scaled_h));
- Object obj = mxgc.getCellAt(trans_x, trans_y);
- //System.out.println("x: " + x + " y: " + y);
- if (obj != null) {
- mxCell cell = (mxCell)obj;
- System.out.println(cell.getId());
- }
- }
- private void setScrollOrigin(int x, int y) {
- scroll_x = x;
- scroll_y = y;
- }
- private void moveImage(int x, int y) {
- img_x = img_x + (x - scroll_x);
- img_y = img_y + (y - scroll_y);
- repaint();
- }
- public Dimension getPreferredSize() {
- return new Dimension(PANEL_W, PANEL_H);
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(orig_img, img_x, img_y, scaled_w, scaled_h, null);
- }
- }
- /**
- * Main Class
- * @author G13.2
- */
- public class Main
- {
- private static void createAndShowGUI(OGraph g) {
- System.out.println("Created GUI on EDT? "+
- SwingUtilities.isEventDispatchThread());
- JFrame f = new JFrame("Swing Paint Demo");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.add(new MyPanel(g));
- f.pack();
- f.setResizable(false);
- f.setVisible(true);
- }
- /**
- * The main method
- * @param args Arguments of main
- */
- public static void main(String[] args) {
- WP wikipedia = new WP();
- wikipedia.setGraph(GraphIO.loadWP(new File("golden/ENTRADAcodigo.txt")));
- final OGraph g = wikipedia.getGraph();
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI(g);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement