drawing in swing Java -
i'm making game "who millionaire".
this panel, let user choose 1 of options such as: calling friend, asking audience, etc.
but have problem, options ellipses, drawn in class help_option extends jcomponent. when test class help_option individually, works fine. when add help_option object game panel, sub-panel in frame, displays line on panel, doesn't draw ellipse.
this code:
note: jframe, don't copy whole method initialize(jframe a) cos it's quite long , don't think error comes there.
/******helper panel**********/ jpanel help_area_container = new jpanel(); help_area_container.setborder(borderfactory.createlineborder(color.blue, 3)); help_area_container.setlayout(new gridlayout(4,0)); jpanel voting_container = new jpanel(); jpanel calling_container = new jpanel(); jpanel half_container = new jpanel(); jpanel take_container = new jpanel(); jpanel[] all_help_container = new jpanel[]{voting_container, calling_container, half_container, take_container}; for(int = 0; < all_help_container.length; i++){ all_help_container[i].setborder(borderfactory.createlineborder(color.red)); all_help_container[i].setpreferredsize(new dimension(350, help_area_container.getpreferredsize().height/4)); } for(int j = 0; j < all_help_container.length; j++){ help_area_container.add(all_help_container[j]); } help_option voting_option = new help_option(all_help_container[0].getpreferredsize().width, all_help_container[0].getpreferredsize().height); voting_option.setpreferredsize(new dimension(all_help_container[0].getpreferredsize().width, all_help_container[0].getpreferredsize().height)); all_help_container[0].add(voting_option); a.add(help_area_container, borderlayout.east); /*****************************/ this help_option class:
class help_option extends jcomponent implements mousemotionlistener{ private static int x, y; private ellipse2d ellipse; private color c = color.black; public help_option(int x, int y){ help_option.x = x; help_option.y = y; ellipse = new ellipse2d.double(0, 0, x, y); this.addmousemotionlistener(this); } public void paintcomponent(graphics g){ super.paintcomponent(g); graphics2d g2d = (graphics2d) g; g2d.setcolor(color.blue); g2d.draw(ellipse); g2d.setcolor(c); g2d.fill(ellipse); g2d.setcolor(color.red); g2d.setfont(new font("timesroman", font.bold, 20)); g2d.drawstring("here am", 250, 100); } public void setcolor(color c){ this.c = c; } @override public void mousedragged(mouseevent e) { } @override public void mousemoved(mouseevent e) { if(ellipse.contains(e.getx(), e.gety())){ setcolor(color.green); repaint(); }else{ setcolor(color.black); repaint(); } } } and class used test help_option class:
public class extends jframe{ public static void main(string [] agrs){ h = new help(); h.setdefaultcloseoperation(jframe.exit_on_close); h.init(); } public void init(){ this.setlayout(new flowlayout()); this.setsize(2000, 1000); jpanel = new jpanel(); a.setpreferredsize(new dimension((int)a.getsize().width/3, (int)a.getsize().height/2)); a.setborder(borderfactory.createlineborder(color.yellow, 3)); help_option k = new help_option(a.getpreferredsize().width, a.getpreferredsize().height/2); k.setpreferredsize(new dimension(a.getpreferredsize().width, a.getpreferredsize().height)); a.add(k); this.add(a); this.setvisible(true); } } edit
this link classes, please take @ them. error described above.
it haven't set values first couple of jpanels , returning preferred sizes of 0. dimensions of 0,0
so should add values jpanels there.
public static void main(string[] args) { jpanel help_area_container = new jpanel(); help_area_container.setborder(borderfactory.createlineborder( color.blue, 3)); help_area_container.setlayout(new gridlayout(4, 0)); //should have set sizes below jpanel voting_container = new jpanel(); //voting_container.setsize(50,50); jpanel calling_container = new jpanel(); jpanel half_container = new jpanel(); jpanel take_container = new jpanel(); jpanel[] all_help_container = new jpanel[] { voting_container, calling_container, half_container, take_container }; (int = 0; < all_help_container.length; i++) { all_help_container[i].setborder(borderfactory .createlineborder(color.red)); all_help_container[i].setpreferredsize(new dimension(350, help_area_container.getpreferredsize().height / 4)); } (int = 0; < all_help_container.length; i++) { system.out.println(all_help_container[i].getsize()); } } // can change size all_help_container[0].setsize(50, 50); system.out.println("----"); (int = 0; < all_help_container.length; i++) { system.out.println(all_help_container[i].getsize()); } } hopefully sizing of gui. have adjust different panels suit needs. i'm guessing panels contain other stuff in them.
you may want create custom jpanels each of these, can check if right size.
public class votingpanel extends jpanel { public votingpanel(){ //all variables such size , color schemes } }
Comments
Post a Comment