java - how to display buttons with paint method in code? -
i have code in java
`
public class okno extends jframe{ pixel p; private int x_zac, y_zac, x_kon, y_kon; //zaciatoca koncova pozicia ciary private int x_kruh, y_kruh; //suradnice kruhu int poc_ciar=0; // premenna na pocitanie poctu vytvorenych ciar int cokreslim=0; // pomocna premenna na urcovanie kreslenia ciar=0 alebo kruhov=1 canvas can = new canvas(); jbutton kruh = new jbutton("kruh"); jbutton ciara = new jbutton("ciara"); gridbagconstraints c = new gridbagconstraints(); kreslenie_vypocty vypocty = new kreslenie_vypocty(); okno(pixel p1){ setsize(800, 700); setdefaultcloseoperation(jframe.exit_on_close); setlayout(new gridbaglayout()); setresizable(false); c.gridx = 0; c.gridy = 0; c.insets= new insets(10, 10, 10, 10); add(kruh,c); c.gridx = 1; c.gridy = 0; c.insets= new insets(10, 10, 10, 10); add(ciara,c); c.gridx = 2; c.gridy = 0; add(can,c); can.setsize(500,700); can.setbackground(color.gray); can.addmouselistener(new mouselistener() { public void drawcenteredcircle(graphics2d g, int x, int y, int r) { x = x-(r/2); y = y-(r/2); g.filloval(x,y,r,r); } public void mouseclicked(mouseevent e) { } public void mouseexited(mouseevent e) { } public void mouseentered(mouseevent e) { } public void mousepressed(mouseevent e) { p.c.add(new ciara()); x_zac = e.getx(); y_zac = e.gety(); } public void mousereleased(mouseevent e) { x_kon = e.getx(); y_kon = e.gety(); repaint(); vypocty.vypocet_bodov(x_zac, y_zac, x_kon, y_kon, p, poc_ciar); poc_ciar++; } }); //can.setvisible(true); p=p1; kruh.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { cokreslim=1; } }); ciara.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { cokreslim=0; } }); } public void paint(graphics g){ g=can.getgraphics(); g.setcolor(color.red); if (cokreslim==0) g.drawline(x_zac,y_zac,x_kon,y_kon); else g.drawoval(x_zac-(50/2),y_zac-(50/2),50,50);}}` when have paint method in code, buttons not displayed. when remove it, everithing fine.
screens:with paint method 
without paint method 
..... when click on canvas, 1 button displayed
thanks
well, first of, should call super.paint(g)...this important, part of it's job paint child components...
having said that...you should avoid overriding paint of top level containers jframe, instead, should create custom component extends jcomponent (like jpanel example) , override it's paintcomponent method instead.
you should avoid painting components contain other components, unless you're planning on painting background image of kind, example.
instead, separate containers, you're painting doesn't end under other components...unless thats you're aming for, should carry on...
take closer @ performing custom painting , painting in awt , swing more details
Comments
Post a Comment