swing - Drawing erases after while loop in Java paint method -
i'm working on little program draw stacking cups using paint method controlled nested loops. have working, except when outer while loops finishes, drawing erases.
the program consists of 2 while loops. first incrementing row of drawing , second draws cups in row.
watching program in debug mode, after final execution of outer while loop, outer while statement evaluated (while (baselength > 0) false, program goes line int counter2 = 0, drawing disappears , program exits.
i tried building loops instead of while, , same effect. once outer loop evaluated false, drawing disappears.
it seems paint(g) method don't quite understand that's causing drawing erase. ideas?
import javax.swing.jframe; import java.awt.graphics; import java.awt.color; public class cups1 extends jframe { /* * declaring instance variables. startx , starty represent top left coordinates * of first cup block in bottom row. cupwidth , cupheight represent width * , height of each row. */ int startx, starty, cupwidth, cupheight; int baselength; //number of cups in bottom row int cupspacing; //spacing between adjacent cups //constructor calls constructor jframe class, argument window title public cups1() { super("example"); startx = 100; starty = 300; cupwidth = 25; cupheight = 40; baselength = 7; cupspacing = 6; } //paint method fills pattern bottom top public void paint(graphics g) { //call paint method of jframe super.paint(g); //startx , starty variables set original point of reference //drawx , drawy local variables each cup instance int drawy = starty, drawx = startx; int counter1 = 0; while (baselength > 0) { int counter2 = 0; //control number of cups in level while (baselength > counter2) { //make odd levels red , levels blue alternate colors if (baselength % 2 == 0) { g.setcolor(color.blue); } else { g.setcolor(color.red); } //draw cup shapes g.fillrect(drawx, drawy, cupwidth, cupheight); drawx = drawx + cupwidth + cupspacing; counter2++; } //decrement base length reduce number of cups on next level baselength--; counter1++; //shift x 1/2 of total value of cupwidth , cupspacing drawx = startx + (((cupwidth + cupspacing)/2) * counter1); //raise height of next level 1 cup height drawy = starty - (cupheight * counter1); } } public static void main(string[] args) { //create application object 550 x 550 size , visibility = true cups1 cup = new cups1(); cup.setsize(550,550); cup.setvisible(true); //close application clicking "x" cup.setdefaultcloseoperation(jframe.exit_on_close); } }
swing components continually being repainted whenever swing determines components should painted.
the first thing component when painted clear own background , painting redone.
therefore variables need reset starting value (within painting method) loops can execute correctly. example, in case "baselength" variable wound need set "7" in painting method, otherwise first time method called set 0, therefore there never need painting again.
also, custom painting done overriding paintcomponent(...)
method of jpanel (or jcomponent). add panel frame. read section swing tutorial on custom painting more information , working examples.
Comments
Post a Comment