java - Making a glassPane fit with the dimensions of a JPanel -


stackoverflow!

i'm working on project, in regards making navigation system, , we're in beginning stages, working drawing map , navigating itself. have function, should able draw square on top of current data having been drawn already. want avoid having repaint of data roads on again, each mousedragged action event. this, found using glass pane optimal choice.

the problem recides dimensions of glasspane , coordinates reads. when click on spot on our jpanel (which use draw on), knows how , zoom in properly, when draw square, being drawn specific amount of pixels above mouse's position. seems due fact glass pane dimension not correspond jpanel dimensions. glass pane seems cover menubar, whereas jpanel not read clicks occur on menubar.

i want somehow fit glasspane jpanel, , i've tried few different things. 1 thing tried, draw on jrootpane instead of jpanel, doesn't seem possible. tried retrieve "myglasspane" class jpanel (which has own class), , call it's repaint method, that's issue originated from. lately, i've retrieved jrootpane jpanel uses or part of, , retrieved glasspane of jrootpane, seems menubar, added jframe, part of jrootpane, meaning glasspane covers menubar well.

here simplified code, covers issue, not mouse-listening nor data-drawing aspects. show off fact it's annoying rectangle being drawn on top of menubar.

import java.awt.*; import java.awt.event.itemevent; import java.awt.event.itemlistener; import javax.swing.*;  public class testforrootpanestuff {     jframe frame = new jframe();     jrootpane root;     jpanel panel = new jpanel();     jlabel label = new jlabel("hello there!");      public void rootpanepls()     {         frame.add(panel);         panel.add(label);          root = panel.getrootpane();          jmenubar menubar = new jmenubar();         jmenu prettymenu = new jmenu("pretty");         menubar.add(prettymenu);         frame.setjmenubar(menubar);          myglasspane gp = new myglasspane();         root.setglasspane(gp);          gp.setvisible(true);         gp.setenabled(true);         gp.repaint();           frame.setlocationrelativeto(null);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setpreferredsize(new dimension(250, 250));          frame.pack();         frame.setvisible(true);     }      public static void main(string[] args) {         testforrootpanestuff derp = new testforrootpanestuff();         derp.rootpanepls();     }      private class myglasspane extends jcomponent implements itemlistener     {         //react change button clicks.         @override         public void itemstatechanged(itemevent e) {             setvisible(e.getstatechange() == itemevent.selected);         }          //draw square zoomtosquare         @override         protected void paintcomponent(graphics g) {             g.setcolor(color.blue);             g.drawrect(10, 10, 100, 100);         }     } } 

and corresponding screenshot, result of running code. (i can't post direct images, due lack of activity on here)

i hope explaination makes @ least bit of sense. i'm aware explaination perhaps bit in-depth resolving issue, felt necessary invelop guys little bit more situation, , reason needing solution this.

tl;dr: need way make glasspane (retrieved/set somewhere) fit dimensions of specific jpanel, can receive same mouse event coordinates both jpanel , glasspane. see code above visualization of issue. there smart way go this? setting rootpane glasspane final idea...

thanks in advance, if has insight bring!

kirluu~

not asnwer longer comment, guessing based on wrong/uncomplete code posted here

  1. use jlabel instead of jcomponent, jcomponent hasn't layoutmanager in api, sure jlabel too, container simple contianer, transparent, in paintcomponent can (semi) transparent

  2. your code incompleted why there itemlistener, don't put jcomponents jcomponent contianer, use jpanel instead, or mentioned opaque jlabel easiests of ways glasspane, 1st code line should super.paintcomponent in paintcomponent, dispose() custom painting placed/laid in glasspane

  3. jlabel can has easilly copy coordinates jpanel, use componentlistener added jpanel, delaing (350-600) events componentlistener (one event per 1 pixel) using swing timer, edit if resize continue call timer.restart() </edit, if resize ended swing action/actionlistener fire setbounds jlabel (whatever) placed in glasspane

  4. search simple repaintmanager (just covering jlabels bounds in glasspane) animation in glasspane


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -