java - Having some trouble understanding JViewPorts -


i have been trying understand jviewports , tried work one. have been using gentleman's class, extends jviewport class.

if want set viewport in middle of 6480*4860 object implementing jpanel class, how come code gives me blank view, if, upon debugging,i have confirmation class implementing jpanel using has been added view? mixed in calls different methods or in comprehension of coordinates used , meaning? , 1 more : calling

    v.setopaque(true); 

set viewport opaque or set children too? guess know answer 1 if figure out how use viewports.

public myprogram(){      ...      mycustomjpanel = new mycustonjpanel();     mycustomjpanel.setbackground(color.white);     grabbableviewport v = new grabbableviewport();     v.setviewsize(new dimension(720,540));     v.setviewposition(new point(720,540));     v.setview(mycustomjpanel);     v.setviewposition(new point(720,540));     v.setlocation(43, 5);     appropriateparentpanel.add(v); } 

here class trying use. takes more space short question, formatted text should please more plain text! if not mistaken, though, problem should not come here.

// copyright (c) 2006 - 2008, markus strauch. // rights reserved. //  // redistribution , use in source , binary forms, or without // modification, permitted provided following conditions met: //  // * redistributions of source code must retain above copyright notice,  // list of conditions , following disclaimer. // * redistributions in binary form must reproduce above copyright notice,  // list of conditions , following disclaimer in documentation  // and/or other materials provided distribution. //  // software provided copyright holders , contributors "as is"  // , express or implied warranties, including, not limited to,  // implied warranties of merchantability , fitness particular purpose  // disclaimed. in no event shall copyright owner or contributors  // liable direct, indirect, incidental, special, exemplary, or  // consequential damages (including, not limited to, procurement of  // substitute goods or services; loss of use, data, or profits; or business  // interruption) caused , on theory of liability, whether in  // contract, strict liability, or tort (including negligence or otherwise)  // arising in way out of use of software, if advised of  // possibility of such damage.  package net.sf.sdedit.ui.components;  import java.awt.component; import java.awt.cursor; import java.awt.image; import java.awt.point; import java.awt.rectangle; import java.awt.toolkit; import java.awt.event.mouseevent;  import javax.swing.imageicon; import javax.swing.jcomponent; import javax.swing.jviewport; import javax.swing.event.mouseinputlistener;  /**  * <tt>grabbableviewport</tt> <tt>jviewport</tt> scrolls  * view when mouse dragged. while mouse being dragged, cursor  * set &quot;grabbing hand&quot;, in applications such acrobat  * reader.  *   * @author markus strauch  *   */ public class grabbableviewport extends jviewport implements mouseinputlistener {      private static cursor hand = new cursor(cursor.move_cursor);      private static cursor dflt = new cursor(cursor.default_cursor);      public static void sethandcursoricon(imageicon icon) {         image grabbinghand = icon.getimage();         hand = toolkit.getdefaulttoolkit().createcustomcursor(grabbinghand,                 new point(0, 0), "hand cursor");     }      /**      * constructor.      */     public grabbableviewport () {         super ();     }      private rectangle rect;       private point point;          private jcomponent view;          public void setview(component view) {         super.setview(view);         if (this.view != view) {             if (this.view != null) {                 this.view.removemouselistener(this);                 this.view.removemousemotionlistener(this);             }             if (view != null) {                 view.addmouselistener(this);                 view.addmousemotionlistener(this);             }             this.view = (jcomponent) view;         }     }      public void mouseclicked(mouseevent e) {     }      public void mouseentered(mouseevent e) {     }      public void mouseexited(mouseevent e) {     }      public void mousepressed(mouseevent e) {         view.setcursor(hand);         //((component) e.getsource()).setcursor(hand);         rect = getviewrect();         point = screenlocation(e);     }      private point screenlocation(mouseevent e) {         point root = view.getlocationonscreen();         point mouse = e.getpoint();         if (rect != null && !rect.contains(mouse)) {             return null;         }         point screenpoint = new point(root.x + mouse.x, root.y + mouse.y);         return screenpoint;     }      public void mousereleased(mouseevent e) {         view.setcursor(dflt);         scrollto(screenlocation(e));         clear();     }      public void mousedragged(mouseevent e) {         scrollto(screenlocation(e));     }      public void mousemoved(mouseevent e) {     }      private void scrollto(point newpoint) {         if (point != null && newpoint != null && rect != null) {             int deltax = point.x - newpoint.x;             int deltay = point.y - newpoint.y;             rect.x = rect.x + deltax;             rect.y = rect.y + deltay;             ((jcomponent) getview()).scrollrecttovisible(rect);                 point = newpoint;         }     }              private void clear() {                 rect = null;             point = null;         }     } 

i give sscce think should simple enough, although can piece example if ask.

because grabbableviewport extends jviewport, i'd guess you're supposed use in jscrollpane, shown here. if not, example may construct sscce using scrollpanepaint.mypanel.

mycustomjpanel = new mycustonjpanel(); grabbableviewport v = new grabbableviewport(); v.setview(mycustomjpanel); jscrollpane scrollpane = new jscrollpane(); scrollpane.setviewport(v); enclosingcontainer.add(scrollpane); 

see related example leverages existing jscrollpane actions.

using setopaque(true) promise paint every pixel, discussed here.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -