java - Use JScrollPane in JPanel and zoom a Graphics2D correctly -
i have problem zoom jpanel
. have circle in middle of panel when click message say:
you clicked in circle
and if don't click on circle be:
you're out of circle
the problem tried zoom panel when click message says you're out of panel (but i'm clicking in zoomed circle) jscrollpane
don't work tried hard don't working. please help.
this code:
import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.ellipse2d; /* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ public class zoomproject extends javax.swing.jframe { ellipse2d draggedcircle; ellipse2d circledrawinpanel; public zoomproject() { initcomponents(); circledrawinpanel=new ellipse2d.float(jpanel1.getwidth()/2,jpanel1.getheight()/2, 50, 50); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jslider1 = new javax.swing.jslider(); jscrollpane1 = new javax.swing.jscrollpane(); jpanel1 = new javax.swing.jpanel(){ protected void paintcomponent(graphics g) { graphics2d g2 = (graphics2d) g.create(); super.paintcomponent(g); int w =getwidth(); // real width of canvas int h =getheight(); // real height of canvas // translate used make sure scale centered g2.translate(w/6, h/6); g2.scale((double)jslider1.getvalue(),(double)jslider1.getvalue()); g2.translate(-w/6, -h/6); g2.filloval(w/2,h/2,50,50); } }; setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jslider1.setorientation(javax.swing.jslider.vertical); jslider1.setvalue(1); jslider1.addchangelistener(new javax.swing.event.changelistener() { public void statechanged(javax.swing.event.changeevent evt) { jslider1statechanged(evt); } }); jpanel1.setbackground(new java.awt.color(255, 255, 255)); jpanel1.addmousemotionlistener(new java.awt.event.mousemotionadapter() { public void mousedragged(java.awt.event.mouseevent evt) { jpanel1mousedragged(evt); } }); jpanel1.addmouselistener(new java.awt.event.mouseadapter() { public void mouseclicked(java.awt.event.mouseevent evt) { jpanel1mouseclicked(evt); } }); javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); jpanel1.setlayout(jpanel1layout); jpanel1layout.sethorizontalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 436, short.max_value) ); jpanel1layout.setverticalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 311, short.max_value) ); jscrollpane1.setviewportview(jpanel1); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jscrollpane1, javax.swing.grouplayout.default_size, 455, short.max_value) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jslider1, javax.swing.grouplayout.preferred_size, 27, javax.swing.grouplayout.preferred_size) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) .addcomponent(jscrollpane1, javax.swing.grouplayout.default_size, 311, short.max_value) .addcomponent(jslider1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)) .addcontainergap()) ); pack(); }// </editor-fold> private void jpanel1mouseclicked(java.awt.event.mouseevent evt) { if(circledrawinpanel.contains(evt.getpoint())){ draggedcircle=circledrawinpanel; system.out.println("you click circle now"); } else system.out.println("out of circle"); } private void jpanel1mousedragged(java.awt.event.mouseevent evt) { } private void jslider1statechanged(javax.swing.event.changeevent evt) { jpanel1.setpreferredsize(new dimension(jpanel1.getwidth()*jslider1.getvalue(),jpanel1.getheight()*jslider1.getvalue())); jscrollpane1.updateui(); repaint(); } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(zoomproject.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(zoomproject.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(zoomproject.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(zoomproject.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new zoomproject().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jpanel jpanel1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jslider jslider1; // end of variables declaration }
Comments
Post a Comment