Zooming In On a Java Graphic -


i making graph of sound speed profile java graphics. have static graph drawn (the 1 pops when run program), trying implement such if user clicks on either x or y axis, zoom in can @ sound speed profile more closely. don't have sound speed profile in graph yet (i know how that, i'm saving effort of drawing until have zoom feature figured out). have ideas how make work? have seen people trying use affine transform similar tasks, not sure that's right thing or if i'm doing correctly. particular code @ paint(), zoomin(), , mouseclicked() method. ideas apprecaited!

public class soundspeedprofile extends jpanel implements mouselistener, actionlistener {      private string title;     private string subtitle;     private jframe frame;     private graphics g;     .         .         .       /**draws sound speed profile , surrounding graphics      * @param graphics g - graphics object      */         public void paint(graphics g){             this.g = g;             super.paint(g); //the super knows how draw "standard" components squares, rectangles, circles, etc             g.setcolor(color.dark_gray);             //1) set graph sound speed profile lives in             //x-axis speeds             g.drawline(100, 150, 450, 150);//the graphics display has 0,0 in upper left corner versus lower left corner             int = 120;             int k = 1460;             while (i<440){                 g.drawstring("|", i, 155);                 g.drawstring("" + k + "", i-2, 140);                 k = k + 20;                 = + 60;             }             //y-axis             g.drawline(100, 500, 100, 150);             k= 7000;             int j = 500;                while (j>160){                 g.drawstring("" + k, 60, j);                 g.drawstring("--", 94, j);                 k = k - 1000;                 j = j - 50;             }              font f1 = new font("serif", 4, 15);             g.setfont(f1);             g.drawstring(this.title, 200,30);//graph title             g.drawstring(this.subtitle, 225, 50);             font f2 = new font("serif", 2, 15);             g.setfont(f2);             g.drawstring("sound speed ", 200, 110);//x-axis label             g.drawstring("(" + spdunits + ")", 290, 110); //units label--taken input array             g.drawstring("depth", 10, 180); //y-axis label             g.drawstring("(" + depunits + ")", 07, 200); //units label--taken input array             //((graphics2d)g).scale(20, 20);            }         /**creates , shows gui drawing of sound speed profile in jframe          */         private void createandshowgui(){             frame = new jframe("sound speed profile");             canvas = new canvas();             frame.add(canvas);             frame.addmouselistener(this);             frame.setbackground(color.cyan);             frame.setsize(600,800);             frame.setdefaultcloseoperation(jframe.exit_on_close);             this.setlayout(new borderlayout());             frame.add(this, borderlayout.center);//add sound speed profile graphic , set border layout             //frame.pack();             frame.setvisible(true);         }          /**          * runs test cases          * @param args          */         public static void main (string [] args){                  ssp.settitle("sound speed profile 1");                 ssp.setsubtitle("june 1, 2012");                 ssp.createandshowgui();                  ssp.repaint(); //necessary?          }          @override         public void actionperformed(actionevent arg0) {             // todo auto-generated method stub          }          @override         public void mouseclicked(mouseevent arg0) {             int x = arg0.getx();             //system.out.println("x: " + x);             int xmin = x - 50;             int xmax = x + 50;             int y = arg0.gety();             //system.out.println("y: " + y);             int ymin = y-50;             int ymax = y + 50;             //if user clicked on x-axis             if ( 160<y &&  y<180 && 100<x && x<450){                 system.out.println("about zoom in on x-axis");                 zoomin(x, y);                  //system.out.println("zooming in on x-axis");             }              //if user clicked on y-axis             if (90<x && x<110 && 150 <y && y<500){                 //system.out.println("zooming in on y-axis");             }           }          public void zoomin(int x, int y){             affinetransform old = ((graphics2d) g).gettransform();             (double zoom = 1; zoom >=0.1; zoom=-0.1){                   affinetransform tr2 =affinetransform.gettranslateinstance(-x, -y);                     affinetransform  tr= affinetransform.getscaleinstance(zoom,zoom);                     tr.concatenate(tr2); tr2=tr;                     tr =affinetransform.gettranslateinstance(x, y);                     tr.concatenate(tr2); tr2=tr;                     tr= new affinetransform(old);                     tr.concatenate(tr2);  tr2=tr;                     ((graphics2d)g).settransform(tr2);                     ((graphics2d)g).drawrect(x, y, 10, 10);                     ((graphics2d)g).settransform(old);                }         } 


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -