Zooming In On a Java Graphic

tler

New Member
I am making a graph of a sound speed profile with java graphics. I have the static graph drawn (the one that pops up when you run the program), but I am trying to implement it such that if the user clicks on either the x or y axis, then it will zoom in so you can look at the sound speed profile more closely. I don't have the sound speed profile in the graph yet (I already know how to do that, I'm just saving the effort of drawing it until I have the zoom feature figured out). Does anyone have any ideas as to how to make this work? I have seen people trying to use the Affine Transform for similar tasks, but I am not sure that's the right thing to do or if I'm even doing this correctly. The particular code to look at is the paint(), zoomIn(), and mouseClicked() method. Ideas would be much apprecaited!\[code\]public class SoundSpeedProfile extends JPanel implements MouseListener, ActionListener { private String title; private String subTitle; private JFrame frame; private Graphics g; . . . /**Draws the sound speed profile and surrounding graphics * @param Graphics g - graphics object */ public void paint(Graphics g){ this.g = g; super.paint(g); //the super knows how to draw "standard" components like squares, rectangles, circles, etc g.setColor(Color.DARK_GRAY); //1) Set up the graph the sound speed profile lives in //X-Axis for Speeds g.drawLine(100, 150, 450, 150);//the graphics display has 0,0 in the upper left corner versus the lower left corner int i = 120; int k = 1460; while (i<440){ g.drawString("|", i, 155); g.drawString("" + k + "", i-2, 140); k = k + 20; i = i + 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 from input array g.drawString("Depth", 10, 180); //y-axis label g.drawString("(" + depUnits + ")", 07, 200); //Units label--taken from input array //((Graphics2D)g).scale(20, 20); } /**Creates and shows the GUI drawing of the sound speed profile in a 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 the sound speed profile graphic and set a 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 the user clicked on the x-axis if ( 160<y && y<180 && 100<x && x<450){ System.out.println("About to zoom in on the x-axis"); zoomIn(x, y); //System.out.println("zooming in on the x-axis"); } //If the user clicked on the y-axis if (90<x && x<110 && 150 <y && y<500){ //System.out.println("zooming in on the y-axis"); } } public void zoomIn(int x, int y){ AffineTransform old = ((Graphics2D) g).getTransform(); for (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); } }\[/code\]
 
Top