java - Line not appearing on JDesktopPane -
i want draw line between 2 jpanels line not appearing on layeredpane.
this have done please go through it, compilable.do try , correct code. have tried on drawing lines on internal frames way not working jpanels.
package build; import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import javax.swing.jframe; import javax.swing.jlayeredpane; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.lineborder; public class linklayerpane1 { public static void main(string[] args) { new linklayerpane1(); } public linklayerpane1() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception ex) { } catch (instantiationexception ex) { } catch (illegalaccessexception ex) { } catch (unsupportedlookandfeelexception ex) { } jlayeredpane layer = new jlayeredpane(); jpanel red = new jpanel(); red.setbackground(color.red); jpanel blue = new jpanel(); blue.setbackground(color.blue); red.setbounds(50, 50, 50, 50); blue.setbounds(125, 125, 50, 50); layer.add(red); layer.add(blue); point red= new point(); red.x=red.getx()+(red.getwidth()/2); red.y=red.gety()-(red.getheight()/2); point blue= new point(); blue.x=blue.getx()+(blue.getwidth()/2); blue.y=blue.gety()-(blue.getheight()/2); handledrag(red,blue); handledrag(blue,red); layer.add(new linkpane(red, blue), new integer(jlayeredpane.default_layer + 1)); layer.setpreferredsize(new dimension(250, 250)); jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(layer); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class linkpane extends jpanel { private point startpoint; private point endpoint; public linkpane(jpanel panel1, jpanel panel2) { setopaque(false); point p1 = panel1.getlocation(); point p2 = panel2.getlocation(); startpoint = new point(); endpoint = new point(); point = new point(); point = new point(); if (p1.x < p2.x) { from.x = p1.x + (panel1.getwidth() / 2); to.x = p2.x + (panel2.getwidth() / 2); } else { from.x = p2.x + (panel2.getwidth() / 2); to.x = p1.x + (panel1.getwidth() / 2); startpoint.x = p2.x; } if (p1.y < p2.y) { from.y = p1.y + (panel1.getheight()/ 2); to.y = p2.y + (panel2.getheight()/ 2); } else { from.y = p2.y + (panel2.getheight()/ 2); to.y = p1.y + (panel1.getheight()/ 2); } int width = math.max(1, to.x - from.x); int height = math.max(1, to.y - from.y); setlocation(from); setsize(width, height); system.out.println(getbounds()); if (p1.x < p2.x) { startpoint.x = 0; endpoint.x = getwidth(); } else { startpoint.x = getwidth(); endpoint.x = 0; } if (p1.y < p2.y) { startpoint.y = 0; endpoint.y = getheight(); } else { startpoint.y = getheight(); endpoint.y = 0; } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); g2d.setstroke(new basicstroke(3, basicstroke.cap_round, basicstroke.join_round)); g2d.drawline(startpoint.x, startpoint.y, endpoint.x, endpoint.y); g2d.dispose(); } } //dragging public static void handledrag(final jpanel tpanel,final point to) { final jpanel p = tpanel; tpanel.addmousemotionlistener(new mousemotionadapter() { @override public void mousedragged(mouseevent me) { me.translatepoint(me.getcomponent().getlocation().x, me.getcomponent().getlocation().y); //handling panel dragging p.setlocation(me.getx(), me.gety()); //finding new point point p1 = tpanel.getlocation(); point p2=to; point startpoint = new point(); point endpoint = new point(); point = new point(); if (p1.x < p2.x) { from.x = p1.x + (tpanel.getwidth() / 2); to.x = p2.x + (tpanel.getwidth() / 2); } else { from.x = p2.x + (tpanel.getwidth() / 2); to.x = p1.x + (tpanel.getwidth() / 2); startpoint.x = p2.x; } if (p1.y < p2.y) { from.y = p1.y + (tpanel.getheight()/ 2); to.y = p2.y + (tpanel.getheight()/ 2); } else { from.y = p2.y + (tpanel.getheight()/ 2); to.y = p1.y + (tpanel.getheight()/ 2); } int width = math.max(1, to.x - from.x); int height = math.max(1, to.y - from.y); if (p1.x < p2.x) { startpoint.x = 0; endpoint.x = tpanel.getwidth(); } else { startpoint.x = tpanel.getwidth(); endpoint.x = 0; } if (p1.y < p2.y) { startpoint.y = 0; endpoint.y = tpanel.getheight(); } else { startpoint.y = tpanel.getheight(); endpoint.y = 0; } } }); } }
i'm confused. extend class jdesktop
, proceed add jlayedpane
...jdesktop
extends.
without layout manger default, jdesktoppane
added jlayeredpane
size of 0x0..
i'm not sure hoped achieve this.
update example
the problem(s) facing paintcomponent
paint under components, while paint
in theory paint on components, can excluded updates (as repaintmanager
can go direct component needs updated).
while still think using glass pane simplest solution, demonstrated here, take advantage of layer pane adding "connect" pane draws line 1 point , add layeredpane
...
import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import javax.swing.jframe; import javax.swing.jlayeredpane; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.lineborder; public class linklayerpane { public static void main(string[] args) { new linklayerpane(); } public linklayerpane() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception ex) { } catch (instantiationexception ex) { } catch (illegalaccessexception ex) { } catch (unsupportedlookandfeelexception ex) { } jlayeredpane layer = new jlayeredpane(); jpanel red = new jpanel(); red.setbackground(color.red); jpanel blue = new jpanel(); blue.setbackground(color.blue); red.setbounds(50, 50, 50, 50); blue.setbounds(125, 125, 50, 50); layer.add(red); layer.add(blue); layer.add(new linkpane(red, blue), new integer(jlayeredpane.default_layer + 1)); layer.setpreferredsize(new dimension(250, 250)); jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(layer); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class linkpane extends jpanel { private point startpoint; private point endpoint; public linkpane(jpanel panel1, jpanel panel2) { setopaque(false); point p1 = panel1.getlocation(); point p2 = panel2.getlocation(); startpoint = new point(); endpoint = new point(); point = new point(); point = new point(); if (p1.x < p2.x) { from.x = p1.x + (panel1.getwidth() / 2); to.x = p2.x + (panel2.getwidth() / 2); } else { from.x = p2.x + (panel2.getwidth() / 2); to.x = p1.x + (panel1.getwidth() / 2); startpoint.x = p2.x; } if (p1.y < p2.y) { from.y = p1.y + (panel1.getheight()/ 2); to.y = p2.y + (panel2.getheight()/ 2); } else { from.y = p2.y + (panel2.getheight()/ 2); to.y = p1.y + (panel1.getheight()/ 2); } int width = math.max(1, to.x - from.x); int height = math.max(1, to.y - from.y); setlocation(from); setsize(width, height); system.out.println(getbounds()); if (p1.x < p2.x) { startpoint.x = 0; endpoint.x = getwidth(); } else { startpoint.x = getwidth(); endpoint.x = 0; } if (p1.y < p2.y) { startpoint.y = 0; endpoint.y = getheight(); } else { startpoint.y = getheight(); endpoint.y = 0; } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); g2d.setstroke(new basicstroke(3, basicstroke.cap_round, basicstroke.join_round)); g2d.drawline(startpoint.x, startpoint.y, endpoint.x, endpoint.y); g2d.dispose(); } } }
now dragging
remember, mouse events relative component mouselistener
registered to. is, if register mosuelistener
of components within jlayeredpane
, mouse events translated relative there coordinate space. can make life little confusing.
this example makes 2 significant changes.
- it adds
mouse/motionlistener
jlayeredpane
. listener deals figuring out clicked , managing process moves "setup" code
linkpane
updatelinks
method.linkpane
registerscomponentlistener
each of panels linking , updates link when there position or size changes...import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.event.componentadapter; import java.awt.event.componentevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.jframe; import javax.swing.jlayeredpane; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;
public class linklayerpane {
public static void main(string[] args) { new linklayerpane(); } public linklayerpane() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception ex) { } catch (instantiationexception ex) { } catch (illegalaccessexception ex) { } catch (unsupportedlookandfeelexception ex) { } jlayeredpane layer = new jlayeredpane(); jpanel red = new jpanel(); red.setbackground(color.red); jpanel blue = new jpanel(); blue.setbackground(color.blue); red.setbounds(50, 50, 50, 50); blue.setbounds(125, 125, 50, 50); layer.add(red); layer.add(blue); layer.add(new linkpane(red, blue), new integer(jlayeredpane.default_layer + 1)); layer.setpreferredsize(new dimension(250, 250)); mouseadapter ma = new mouseadapter() { private point offset; private component dragcomponent; @override public void mousepressed(mouseevent e) { jlayeredpane layer = (jlayeredpane) e.getcomponent(); dragcomponent = layer.getcomponentat(e.getpoint()); if (dragcomponent != null && !dragcomponent.equals(layer) && !(dragcomponent instanceof linkpane)) { offset = e.getpoint(); offset.x = dragcomponent.getx() - offset.x; offset.y = dragcomponent.gety() - offset.y; } else { dragcomponent = null; } } @override public void mousereleased(mouseevent e) { dragcomponent = null; offset = null; } @override public void mousedragged(mouseevent e) { if (dragcomponent != null) { point dragto = e.getpoint(); dragto.x += offset.x; dragto.y += offset.y; dragcomponent.setlocation(dragto); } } }; layer.addmouselistener(ma); layer.addmousemotionlistener(ma); jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(layer); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class linkpane extends jpanel { private point startpoint; private point endpoint; private jpanel[] links; public linkpane(jpanel panel1, jpanel panel2) { setopaque(false); links = new jpanel[]{panel1, panel2}; componentadapter ca = new componentadapter() { @override public void componentmoved(componentevent e) { updatelinks(); } @override public void componentresized(componentevent e) { updatelinks(); } }; links[0].addcomponentlistener(ca); links[1].addcomponentlistener(ca); updatelinks(); } protected void updatelinks() { point p1 = links[0].getlocation(); point p2 = links[1].getlocation(); startpoint = new point(); endpoint = new point(); point = new point(); point = new point(); if (p1.x < p2.x) { from.x = p1.x + (links[0].getwidth() / 2); to.x = p2.x + (links[1].getwidth() / 2); } else { from.x = p2.x + (links[1].getwidth() / 2); to.x = p1.x + (links[0].getwidth() / 2); startpoint.x = p2.x; } if (p1.y < p2.y) { from.y = p1.y + (links[0].getheight() / 2); to.y = p2.y + (links[1].getheight() / 2); } else { from.y = p2.y + (links[1].getheight() / 2); to.y = p1.y + (links[0].getheight() / 2); } int width = math.max(1, to.x - from.x); int height = math.max(1, to.y - from.y); setlocation(from); setsize(width, height); system.out.println(getbounds()); if (p1.x < p2.x) { startpoint.x = 0; endpoint.x = getwidth(); } else { startpoint.x = getwidth(); endpoint.x = 0; } if (p1.y < p2.y) { startpoint.y = 0; endpoint.y = getheight(); } else { startpoint.y = getheight(); endpoint.y = 0; } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); g2d.setstroke(new basicstroke(3, basicstroke.cap_round, basicstroke.join_round)); g2d.drawline(startpoint.x, startpoint.y, endpoint.x, endpoint.y); g2d.dispose(); } }
}
Comments
Post a Comment