it is very simple and easy ... first i get the images, which is a screen shot basically, using java's built in Class Robot and paint it in a JFrame and just scale that image ... i use a screen shot of the screen as that was my requirement you can also create an image from a file ... for that
// remove this portion ... Robot r= new Robot(); Rectangle rect = new Rectangle(0,0,d.width,d.height); img = r.createScreenCapture(rect); // with this... img=Toolkit.getDefaultToolkit().getImage("PATH_TO_IMAGE");my zoom application will look like something like that ... ofcourse it is not the original application :)
left click will trigger zoom-in function and right click will trigger zoom-out function... the complete code of my Zoom application was somthing like this
Source Code
class ZoomFrame extends JFrame implements WindowListener, ComponentListener,MouseListener { Image img; boolean bo = true; static double amount=2; public ZoomFrame(){ setSize(150,150); setVisible(true); setTitle("Zoom Utility - www.tutorialjinni.com "); capture(); addMouseListener(this); addWindowListener(this); addComponentListener(this); } public void capture(){ Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); try { Robot r= new Robot(); Rectangle rect = new Rectangle(0,0,d.width,d.height); img = r.createScreenCapture(rect); /* * You can also put here image like... * img=Toolkit.getDefaultToolkit().getImage("PATH_TO_IMAGE"); */ } catch(Exception e) { System.out.println(e); } } @Override public void paint(Graphics g){ Graphics2D g2=(Graphics2D)g; g2.scale(amount,amount); g2.drawImage(img,0,0,getWidth(),getHeight(),getX(),getY(),getX()+getWidth(),getY()+getHeight(),this); } @Override public void update(Graphics g){ paint(g); } public void windowClosing(WindowEvent we){ this.dispose(); //System.exit(0); } void zoomIn(){ amount=amount+.5; repaint(); } void zoomOut(){ if(amount>1){ amount=amount-.5; } repaint(); } public void windowClosed(WindowEvent we){ //setVisible(false); System.exit(1); } public void windowOpened(WindowEvent we){} public void windowIconified(WindowEvent we){} public void windowDeiconified(WindowEvent we){} public void windowActivated(WindowEvent we){ if (bo == false){ setVisible(false); capture(); bo = true; setVisible(true); } else{ bo = false; } } public void windowDeactivated(WindowEvent we){ } public void componentMoved(ComponentEvent ce){ repaint(); } public void componentHidden(ComponentEvent ce){ } public void componentShown(ComponentEvent ce){} public void componentResized(ComponentEvent ce){} public void mouseClicked(MouseEvent e) { switch(e.getModifiers()){ case InputEvent.BUTTON1_MASK:{ zoomIn(); break; } case InputEvent.BUTTON3_MASK:{ zoomOut(); break; } } } public void mousePressed(MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); } public void mouseReleased(MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); } public void mouseEntered(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); } public void mouseExited(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); } }you can also download a Netbeans Project so can start working immediately ...
1 comments:
Maxxxxxxxxxxxxa
Post a Comment