Friday, 15 May 2015

java - JPanel Location and Size changing weird -



java - JPanel Location and Size changing weird -

i'm programme little version of "pong". i've got problem jpanels. size of shields changing pretty weird 10 100. , there no method changes size of it! shields jump upper middle of jpanel. aren't expect that. seen on 2 screenshots @ ent of post. (puu.sh)

sorry if i'm missing obvious, said, i'm training.

main psvm()

public class main { public static void main(string[] args) { new field(); } }

field (frame game in it)

import java.awt.color; import java.awt.dimension; import java.awt.toolkit; import java.awt.event.componentadapter; import java.awt.event.componentevent; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jframe; public class field extends jframe { private room room = new room(); public field() { this.setsize((int) (toolkit.getdefaulttoolkit().getscreensize().getwidth() / 1.5), (int) (toolkit.getdefaulttoolkit().getscreensize().getheight() / 1.5)); this.setlayout(null); this.setlocation((int) ((toolkit.getdefaulttoolkit().getscreensize().getwidth() - (toolkit.getdefaulttoolkit().getscreensize().getwidth() / 1.5)) / 2), (int) ((toolkit.getdefaulttoolkit().getscreensize().getheight() - (toolkit.getdefaulttoolkit().getscreensize().getheight() / 1.5)) / 2)); this.getcontentpane().setbackground(color.gray); this.setresizable(true); this.setenabled(true); this.setminimumsize(new dimension(1000, 455)); if (getheight() < 455) { this.setsize(getwidth(), 455); } else if (getwidth() < 1000) { this.setsize(1000, getheight()); } this.setdefaultcloseoperation(jframe.exit_on_close); this.add(room); this.setvisible(true); addmouselistener(this); this.addcomponentlistener(new componentadapter() { public void componentresized(componentevent evt) { room.updatelocation(getsize()); } }); } }

room

import javax.swing.*; import java.awt.*; public class room extends jpanel { private shield shieldl = new shield(0); // left side private shield shieldr = new shield(1); // right side public room() { this.setbackground(new color(122, 197, 205)); this.add(shieldl); this.add(shieldr); } public void updatelocation(dimension d) { this.setsize((int) d.getwidth(), (int) (d.getheight() - (d.getheight() / 5))); this.setlocation(0, (int) (d.getheight() / 10)); shieldl.updatelocation(getsize()); shieldr.updatelocation(getsize()); this.setvisible(true); } }

shield

import javax.swing.*; import java.awt.*; public class shield extends jpanel { private int side = 0; public shield(int s) { //0 = left | 1 = right side = s; this.setsize(10, 100); this.setbackground(color.white); } public void updatelocation(dimension d) { //bug found here system.out.println(getsize()); //bug seems not created here? int xpos; if (side == 0) { xpos = (int) (d.getwidth() / 8); } else { xpos = (int) (d.getwidth() / 8 * 7 - this.getwidth() / 2); } this.setlocation(xpos, (int) (d.getheight() / 2 - (getheight() / 2))); this.setvisible(true); } }

they end this:, switching location @ randome framesize changings.

http://puu.sh/ceye0/ef15ee1748.png

http://puu.sh/ceyfn/122174db48.png

thanks! julian :)

in shield, alter line

setsize(10, 100);

to

setpreferredsize(new dimension(10, 100));

do not utilize setsize methods, called internally.

edit:

try these 2 classes:

public class field extends jframe { private room room = new room(); public field() { //mock panel jpanel filler = new jpanel(); filler.setopaque(false); getcontentpane().setbackground(color.gray); getcontentpane().add(filler, borderlayout.page_start); getcontentpane().add(room); setdefaultcloseoperation(jframe.exit_on_close); setminimumsize(new dimension(1000, 455)); setlocationrelativeto(null); pack(); room.init(); setvisible(true); } } public class room extends jpanel { private static int leftwidth = 100; private static int leftheight = 100; private static int leftx; private static int lefty; private static int rightwidth = 100; private static int rightheight = 100; private static int rightx; private static int righty; public room() { setbackground(new color(122, 197, 205)); } void init() { leftx = getwidth() / 8; lefty = getheight() / 2 - leftheight / 2; rightx = getwidth() * 7 / 8; righty = getheight() / 2 - rightheight / 2; } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; g2d.setcolor(color.white); g2d.fillrect(leftx, lefty, leftwidth, leftheight); g2d.fillrect(rightx, righty, rightwidth, rightheight); } }

java location jpanel size pong

No comments:

Post a Comment