java - Custom JComponent not showing up on JLayeredPane -
i'm trying create chess interface in java , i'm using jlayeredpane
put pieces on top of chessboard image. problem pieces not added layered pane.
this code dragimage
class (every piece going instance of class can drag , drop on chessboard).
class dragimage extends jcomponent implements mousemotionlistener { private static final long serialversionuid = 1l; int imagewidth = 52, imageheight = 52; int imagex, imagey; image img; public dragimage(image i) { img = i; repaint(); addmousemotionlistener(this); } public void mousedragged(mouseevent e) { imagex = e.getx(); imagey = e.gety(); repaint(); } public void mousemoved(mouseevent e) { } public void paint(graphics g) { graphics2d g2 = (graphics2d) g; g2.drawimage(img, imagex, imagey, this); } }
and code jpanel
.
the image addresses correct aren't showing on panel.
class mypanel extends jpanel { private jlayeredpane layeredpane; private jlabel imagecontainer = new jlabel(); private imageicon img; mypanel() { super(); img = new imageicon("chessboard.png"); this.imagecontainer.seticon(img); this.layeredpane = new jlayeredpane(); layeredpane.setpreferredsize(new dimension(416, 416)); layeredpane.add(imagecontainer, 50); imagecontainer.setbounds(0, 0, img.geticonwidth(), img.geticonheight()); int pozx = 52, pozy = 52; (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { if(tabla[i][j]!="--"){ dragimage img = new dragimage(toolkit.getdefaulttoolkit().getimage("pieces\\" + tabla[i][j] + ".png")); //system.out.println("pieces\\" + tabla[i][j] + ".png"); img.setbounds(pozx,pozy,img.imagewidth,img.imageheight); layeredpane.add(img,100); //this doesn't work } pozx+=52; } pozy+=52; pozx=52; } this.add(layeredpane); } }
...
frame = new jframe(); frame.setvisible(true); frame.setsize(800, 600); frame.setdefaultcloseoperation(jframe.exit_on_close); panel = new mypanel(); frame.add(panel); frame.pack();
first use layeredpane attach 2 panels 1 image , panel components , second panel must palete layer components appear..
Comments
Post a Comment