i working on game.i want add images applet not appear there until resize or minimize applet.i not know whether image observer wrong or not. images in right path.their in build , class folder correct spelling. wrong code? appreciated.
public class game extends japplet implements keylistener, runnable { screendir sd; image ball; image flower; public void init() { sd = new screendir(this); ball = getimage(getcodebase(), "ball.jpg"); ballb = new ball(50, 50, 30, 40, color.red, ball, sd); sd.add(ball); flower = getimage(getcodebase(), "flower.gif"); //class flower defined class ball flowerf = new flower(60, 100, 30, 30, color.green, flower, sd); sd.add(flower); } public void paint(graphics g) { sd.draw(g); } //these moving ball @override public void keytyped(keyevent e) { } @override public void keypressed(keyevent e) { } @override public void keyreleased(keyevent e) { } @override public void run() { while (true) { repaint(); try { thread.sleep(15); } catch (interruptedexception ex) {}}} public class screendir { arraylist<object> olist; japplet parent; public screendir(japplet parent) { this.parent = parent; olist = new arraylist<>(); } public void add(object o) { olist.add(o); } image img; graphics og; img = parent.createimage(parent.getwidth(), parent.getheight()); og = offimg.getgraphics(); for(int = 0;i<olist.size ();i++){ olist.get(i).draw(offg); } g.drawimage (img,0, 0, parent); } public class ball extends object { screendir sd; public ball(int x, int y, int w, int h, color c, image pi, screendir sd) { { this.sd = sd; } public void draw(graphics g) { g.drawimage(pi, x, y, w, h, null); } public abstract class object { int x, y, w, h; color c; image pi; public object(int x, int y, int w, int h, color c, image pi) { this.x = x; this.y = y; this.w = w; this.h = h; this.c = c; this.pi = pi; } public abstract void draw(graphics g) { }
applets load images asynchronously, images not loaded before applet goes paint. every java component worth mentioning implements imageobserver
means updates on image loading. fix problem, change this:
g.drawimage(pi, x, y, w, h, null);
to this:
g.drawimage(pi, x, y, w, h, this);
update
i mistakenly thought drawimage
method part of japplet
(which imageobserver
). might change method declaration & painting line to:
public void draw(graphics g, imageobserver io) { g.drawimage(pi, x, y, w, h, io); }
then call it, change:
sd.draw(g);
to:
sd.draw(g, this);
Comments
Post a Comment