swing - Java: mouseDragged and moving around in a graphical interface -


newbie programmer here.

i'm making program renders user-inputted equations in cartesian coordinate system. @ moment i'm having issues letting user move view around freely in coordinate. mousedragged user can drag view around bit, once user releases mouse , tries move view again origin snaps current position of mouse cursor. best way let user move around freely? in advance!

here's code drawing area.

import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.rectangle; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.geom.line2d; import java.awt.geom.point2d; import javax.swing.jpanel; public class drawingarea extends jpanel implements mousemotionlistener {  private final int x_panel = 350; // width of panel private final int y_panel = 400; // height of panel private int div_x; // width of 1 square private int div_y; // height of 1 square  private int real_y; private int real_x; private point origin; // origin of coordinate private point temp; // temporary point  private static int y = 0; private static int x = 0;  drawingarea() {     setbackground(color.white);      real_x = x_panel;     real_y = y_panel;      setdivisiondefault();     setorigin(new point((real_x / 2), (real_y / 2)));      setsize(x_panel, y_panel);     addmousemotionlistener(this);  }  drawingarea(point origin, point destination) {      this.origin = origin;      this.destination = destination;      panel = new jpanel();      panel.setsize(destination.x, destination.y);      panel.setlocation(origin);      this.panel.setbackground(color.red);      panel.setlayout(null);   }  @override public void paintcomponent(graphics g) {      super.paintcomponent(g);      graphics2d line = (graphics2d) g;       temp = new point(origin.x, origin.y);      line.setcolor(color.red);     drawhelplines(line);      line.setcolor(color.blue);     draworigin(line);      line.setcolor(color.green);     (int = 0; < 100; i++) { // test line         //temp = this.suora();          temp.x++;         temp.y++;          line.drawline(temp.x, temp.y, temp.x, temp.y);      }    }  public void setorigin(point p) {     origin = p;   }    public void draworigin(graphics2d line) {     line.drawline(origin.x, 0, origin.x, y_panel);     line.drawline(0, origin.y, x_panel, origin.y); }  public void drawhelplines(graphics2d line) {      int xhelp= origin.x;     int yhelp= origin.y;     (int = 0; < 20; i++) {         xhelp+= div_x;         line.drawline(xhelp, 0, xhelp, y_panel);     }     xhelp= origin.x;     (int = 0; < 20; i++) {         xhelp-= div_x;          line.drawline(xhelp, 0, xhelp, y_panel);     }      (int = 0; < 20; i++) {         yhelp-= div_y;         line.drawline(0, yhelp,x_panel, yhelp);     }     yhelp= origin.y;     (int = 0; < 20; i++) {         yhelp+= div_y;         line.drawline(0, yhelp, x_panel, yhelp);     }  }  public void setdivisiondefault() {     div_x = 20;     div_y = 20;  }  @override public void mousedragged(mouseevent e) {       //point temp_point = new point(mouse_x,mouse_y);     point coords = new point(e.getx(), e.gety());        setorigin(coords);      repaint();  }  @override public void mousemoved(mouseevent e) { } } 

based on example, following program allows user drag axes' intersection arbitrary point, origin, starts @ center of panel.

iamge

import java.awt.cursor; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.point; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import javax.swing.jframe; import javax.swing.jpanel;  /**  * @see https://stackoverflow.com/a/15576413/230513  * @see https://stackoverflow.com/a/5312702/230513  */ public class mousedragtest extends jpanel {      private static final string title = "drag me!";     private static final int w = 640;     private static final int h = 480;     private point origin = new point(w / 2, h / 2);     private point mousept;      public mousedragtest() {         this.setcursor(cursor.getpredefinedcursor(cursor.hand_cursor));         this.addmouselistener(new mouseadapter() {             @override             public void mousepressed(mouseevent e) {                 mousept = e.getpoint();                 repaint();             }         });         this.addmousemotionlistener(new mousemotionadapter() {             @override             public void mousedragged(mouseevent e) {                 int dx = e.getx() - mousept.x;                 int dy = e.gety() - mousept.y;                 origin.setlocation(origin.x + dx, origin.y + dy);                 mousept = e.getpoint();                 repaint();             }         });     }      @override     public dimension getpreferredsize() {         return new dimension(w, h);     }      @override     public void paintcomponent(graphics g) {         super.paintcomponent(g);         g.drawline(0, origin.y, getwidth(), origin.y);         g.drawline(origin.x, 0, origin.x, getheight());     }      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 jframe f = new jframe(title);                 f.add(new mousedragtest());                 f.setdefaultcloseoperation(jframe.exit_on_close);                 f.pack();                 f.setlocationrelativeto(null);                 f.setvisible(true);             }         });     } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -