Java Buttons disappear when using paint()
- Get link
- X
- Other Apps
i'm trying create window contain jbuttons , jtextfields. in addition, have paint() function draw functions. however, appears jtextfields , 1 jbuttons disappear. if remove paint function, buttons , textfields visible. putting setvisible in paint function doesn't work , leaving paint blank doesn't draw jbuttons , jtextfields appear.
the possible reason why 1 of jbuttons because added button before worked on paint function.code:import javax.swing.*; import java.awt.event.*; import java.awt.geom.line2d; import java.awt.*; public class graph_function extends jframe implements actionlistener, windowlistener{ control control; drawing_function draw = null; jpanel row1 = new jpanel(); jbutton buttondraw = new jbutton("draw"); jbutton buttontable = new jbutton("table"); jpanel row2 = new jpanel(); jbutton redraw = new jbutton("redraw margins"); jtextfield sizemarginx = new jtextfield(3); jtextfield sizemarginy = new jtextfield(3); boolean redrawingmargin=false; int marginx = 40; int marginy = 40; int currentfunction = 0; public graph_function(control control) { super("graph"); this.control = control; draw = new drawing_function(this); setsize(400,500); setdefaultcloseoperation(jframe.dispose_on_close); setresizable(false); gridlayout layout = new gridlayout(12,1,1,1); setlayout(layout); flowlayout layout1 = new flowlayout(); row1.setlayout(layout1); row1.add(buttondraw); row1.add(buttontable); add(row1); flowlayout layout2 = new flowlayout(); row2.setlayout(layout2); row2.add(sizemarginx); row2.add(sizemarginy); row2.add(redraw); add(row2); redraw.addactionlistener(this); buttondraw.addactionlistener(this); addwindowlistener(this); setvisible(false); } public void paint(graphics g) { //declaring nessecary objects dimension size = new dimension(400,300); point startingrectangle = new point(0,150); rectangle window = new rectangle(startingrectangle,size); point origin = new point(window.width/2,(window.height/2) +150); //gives info system.out.println(window.x + " " + window.y + " " + (window.width+window.x) + " " + (window.height+window.y)); system.out.println("the origin " + origin.x + " " + origin.y); //draws g.clearrect(window.x, window.y, window.width, window.height); g.drawrect(window.x, window.y, window.width, window.height); //draws border g.drawline(window.width/2,150, window.width/2, window.height+150); g.drawline(0, (window.height/2)+150, 400, (window.height/2)+150); drawmargins(); } void drawmargins() { graphics g = getgraphics(); boolean goingup=true; for(int x = 200;;) { g.drawline(x, 295, x, 305); if(goingup==true) { x+=marginx; } else { x-=marginx; } if(x>=400) { x=200; goingup=false; } if(x<=0) { break; } } goingup=true; for(int y = 300;;) { g.drawline(195, y, 205, y); if(goingup==true) { y+=marginy; } else { y-=marginy; } if(y>=450) { y=300; goingup=false; } if(y<=150) { break; } } } public void actionperformed(actionevent event) { object current = event.getsource(); if(current.equals(redraw)) { marginx = integer.parseint(sizemarginx.gettext()); marginy = integer.parseint(sizemarginy.gettext()); graphics g = getgraphics(); paint(g); } if(current.equals(buttondraw)) { setvisible(false); draw.setvisible(true); } } /*void drawaline() { draw.dline.lineequation(); line2d.double line = new line2d.double(); line.setline(0, draw.dline.firsty, 400, draw.dline.secondy); graphics g = getgraphics(); graphics2d g2d = (graphics2d)g; g2d.draw(line); system.out.println(0 + "," + draw.dline.firsty + " " + 400 + ","+ draw.dline.secondy); }*/ @override public void windowactivated(windowevent arg0) { } @override public void windowclosed(windowevent arg0) { } @override public void windowclosing(windowevent arg0) { setvisible(false); control.gui.setvisible(true); } @override public void windowdeactivated(windowevent arg0) { // todo auto-generated method stub } @override public void windowdeiconified(windowevent arg0) { // todo auto-generated method stub } @override public void windowiconified(windowevent arg0) { // todo auto-generated method stub } @override public void windowopened(windowevent arg0) { // todo auto-generated method stub } }
the design , implementation wrong.
paint jcomponent, don't override paint(). override paintcomponent().
also, class structure weird have no idea how make work, i'm going call wrong. jframe should not responsible painting graph component.
there should graphcomponent class, jpanel subclass, , alone responsible painting graph. add graphcomponent instance jframe, e.g. @ center position of borderlayout. couple action-events buttons, through listener (which can jframe, more graphcomponent), jbutton actions go directly action triggered.
shouldn't need override paint() or paintcomponent() in jframe @ all. override should paintcomponent() in graphcomponent.
call "margins" axes, axis labels drawn. recommend putting in graphcomponent, separate objects or methods draw axis labels on graphics passed paintcomponent(). example, graphcomponent.paintcomponent() be:
code:this.drawxaxis(g); this.drawyaxis(g); this.drawgraph(g);
here's quick summary of how custom painting should implemented:
http://download.oracle.com/javase/tutorial/uiswing/painting/closer.html
suggest reading entire tutorial there, , looking other tutorials walk through how make custom subclasses drawing graphs.
1 of reasons posted links existing java graphing components @ how things, , pattern code same way. suggest looking @ existing examples, @ least see methods override.
edit
there other implementation errors, such here:
the red-hilited code wrong. should never call paint() directly, , not jcomponent.code:public void actionperformed(actionevent event) { object current = event.getsource(); if(current.equals(redraw)) { marginx = integer.parseint(sizemarginx.gettext()); marginy = integer.parseint(sizemarginy.gettext()); [color="red"] graphics g = getgraphics(); paint(g); [/color] }
painting event-driven. call 1 of repaint() methods of component needs repainting (there several repaint() forms different args, simplest has no args). awt calls paint() when decides to, using graphics determines.
Forums Macs Mac Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
- Get link
- X
- Other Apps
Comments
Post a Comment