Getting a custom JPanel to show custom JComponents (Java Swing) -



Getting a custom JPanel to show custom JComponents (Java Swing) -

i @ loss 1 guys. trying create custom led display show 7 bars arranged 8. got single custom jcomponent (bar) show within of jframe, cannot bars show within of custom panel creating. here code constructor methods , paint methods in classes main method using test these classes.

the custom jcomponent:

public class bar extends jcomponent { // instance variables - replace illustration below own private static boolean litup = false; private static boolean vertical = false; private static boolean rotated = false; private static boolean rotclockwise = false; private static int positionx; private static int positiony; public bar(boolean lit, boolean vert, int posx, int posy) { litup = lit; vertical = vert; positionx = posx; positiony = posy; repaint(); system.out.println("the bar beingness initialized"); } public void paintcomponent(graphics g) { system.out.println("bar: paint component beingness called"); super.paintcomponent(g); graphics2d g2d = (graphics2d)g; if(vertical == true) { if(litup == true) { g2d.setcolor(color.red); } else { g2d.setcolor(color.black); } g2d.drawrect(positionx , positiony, 10, 30); g2d.fillrect(positionx , positiony, 10, 30); system.out.println("bar: fillrect beingness called vertical bar"); if(rotated == true) { if(rotclockwise == true) { g2d.rotate(0.3398); } else { g2d.rotate(-0.3398); } } } else{ system.out.println("bar: fillrect beingness called horizontal bar"); if(litup == true) { g2d.setcolor(color.red); } else { g2d.setcolor(color.black); } g2d.drawrect(positionx,positiony, 30, 10); g2d.fillrect(positionx,positiony, 30, 10); } } }

the custom jpanel:

public class led extends jpanel { // instance variables - replace illustration below own //private static bar[] bars = new bar[7]; //private static int xpos; //private static int ypos; private bar barzero; private bar barone; private bar bartwo; private bar barthree; private bar barfour; private bar barfive; private bar barsix; /** * constructor objects of class led */ public led() { barzero = new bar(false, false, 0, 0); this.add(barzero); // barzero.setdirection(false); barone = new bar(false, true, 0, 11); this.add(barone); //barone.setdirection(true); bartwo = new bar(false, true, 20, 11); this.add(bartwo); //bartwo.setdirection(true); barthree = new bar(false, false, 0, 42); this.add(barthree); //barthree.setdirection(false); barfour = new bar(false, true, 0, 53); this.add(barfour); //barfour.setdirection(true); barfive = new bar(false, true, 20, 53); this.add(barfive); //barfive.setdirection(true); barsix = new bar(false, false, 0, 64); this.add(barsix); //barsix.setdirection(false); system.out.println("the led class beingness accessed"); repaint(); } @ override public void paintcomponent(graphics g) { system.out.println("led: paintcomponent beingness called"); //barone.paintcomponent(g); system.out.println("led: barzero beingness painted| " + barzero.orientation() + "| " + barzero.coordx()); system.out.println("led: barone beingness painted| " + barone.orientation() + "| " + barone.coordx()); //bartwo.paintcomponent(g); system.out.println("led: bartwo beingness painted| " + bartwo.orientation() + "| " + bartwo.coordx()); //barthree.paintcomponent(g); system.out.println("led: barthree beingness painted| " + barthree.orientation() + "| " + barthree.coordx()); //barfour.paintcomponent(g); system.out.println("led: barfour beingness painted| " + barfour.orientation() + "| " + barfour.coordx()); //barfive.paintcomponent(g); system.out.println("led: barfive beingness painted| " + barfive.orientation() + "| " + barfive.coordx()); //barsix.paintcomponent(g); system.out.println("led: barsix beingness painted| " + barsix.orientation() + "| " + barsix.coordx()); super.paintcomponent(g); } }

and tester method:

public class drawrect { public static void main(string[] a) { jframe window = new jframe(); led led = new led() window.setdefaultcloseoperation(jframe.exit_on_close); window.setbounds(30, 30, 300, 300); window.getcontentpane().add(led); window.setvisible(true); } }

i can initialize bar , have display in frame, cannot led (panel) show bars in it. also, here strings printed out testing component. none of bars adding have values set: horizontal , have x positions set 0. not 1 give up, programme making me want alter major.

there couple of issues need consider here:

each bar object needs own variables maintain track of state. means fellow member variables in bar should not static.

a custom component needs override getpreferredsize() in order tell parent container how much space custom component wants take.

the parent container responsible determining position of components contains. (more appropriately, responsibility of layoutmanager.) means inappropriate have x , y positions in custom component.

in paintcomponent(graphics g), allowed draw within area owned component. coordinates relative top left corner of component, not relative top left of container holds it. means need draw rectangle covering total component:

g.drawrectangle(0, 0, getwidth(), getheight());

you can cut down of complexity of code using array or list store bar objects within custom panel. example, able paint each bar in simple loop.

java swing jpanel jcomponent

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -