java - Panel not shown for 2D Animation -
i trying create 2d animation in java of moving line on panel(a line moving 1 point in panel). hope possible. here's code used.
private void movingline(int length) throws interruptedexception { for(int = 0; + length < width; i++){ for(int j = 0; j + length < height; j++){ eraseline(); drawline(color.cyan, i, j, i+length, j+length); erase = true; } } } private void eraseline() { if(erase){ fillcanvas(color.blue); } }
on running code, panel doesn't show up.
here's code draw line.
public void drawline(color c, int x1, int y1, int x2, int y2) { int pix = c.getrgb(); int dx = x2 - x1; int dy = y2 - y1; canvas.setrgb(x1, y1, pix); if (dx != 0) { float m = (float) dy / (float) dx; float b = y1 - m*x1; dx = (x2 > x1) ? 1 : -1; while (x1 != x2) { x1 += dx; y1 = math.round(m*x1 + b); canvas.setrgb(x1, y1, pix); } } repaint(); }
on running code panel doesn't show moving line. appreciated.
i think biggest problem you're trying change appearance of gui (i'm guessing) thread that's not event dispatching thread.
the solution wrap activity (specifically, calls eraseline
, drawline
) in runnable
, call runnable using swingutilities.invokeandwait()
.
edit: java's graphics components don't let manipulate canvas yourself. components drawing, , when called on paint themselves. directly drawing on canvas, if work, work badly because you'd interfering component does.
rather go lot more explanation, i've gone , implemented think "proper" way this.
the coding's commented necessary, hope gives ideas. more background, read official tutorials on swing , graphics.
Comments
Post a Comment