java - Troubles with simple animation on image -
im trying make image (alien.png) move around screen randomly , once hits walls comes back. im have trouble can't find way upload image , make bounce around. have far i'm getting lot of errors
package animationdemo; import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import javax.swing.jpanel; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class animationdemo extends jframe { public animationdemo() { image alien; alien = toolkit.getdefaulttoolkit().getimage("alien.png"); timer timer = new timer(50, this); timer.start(); } public static void main(string[] args) { animationdemo frame = new animationdemo(); frame.settitle("animationdemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(300, 300); frame.setvisible(true); } } class movingmessagepanel extends jpanel implements actionlistener { public int xcoordinate = 20; public int ycoordinate = 20; public int xdir=5; public int ydir=5; public void actionperformed(actionevent e) { repaint(); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); if (xcoordinate > getwidth()) xdir*=-1; if (ycoordinate > getheight()) ydir*=-1; if (xcoordinate <0) xdir*=-1; if (ycoordinate <0) ydir*=-1; xcoordinate += xdir; ycoordinate += ydir; g.drawimage(alien,xcoordinate,ycoordinate,this); } }
heres of errors
animationdemo.java:18: error: cannot find symbol alien = toolkit.getdefaulttoolkit().getimage("alien.png"); ^ symbol: variable toolkit location: class animationdemo animationdemo.java:19: error: incompatible types: animationdemo cannot converted actionlistener timer timer = new timer(50, this); ^ animationdemo.java:52: error: cannot find symbol g.drawimage(alien,xcoordinate,ycoordinate,this); ^ symbol: variable alien location: class movingmessagepanel note: messages have been simplified; recompile -xdiags:verbose full output 3 errors
i'm not sure why can't find toolkit thought imported i'm not sure why not recognizing alien png on g.drawimage
the errors self explanatory:
animationdemo.java:18: error: cannot find symbol alien = toolkit.getdefaulttoolkit().getimage("alien.png"); ^ symbol: variable toolkit location: class animationdemo
you're capitalizing toolkit wrong. have precise , careful avoid these errors.
animationdemo.java:19: error: incompatible types: animationdemo cannot converted actionlistener timer timer = new timer(50, this); ^
animationdemo class not implement actionlistener, , can't use such.
animationdemo.java:52: error: cannot find symbol g.drawimage(alien,xcoordinate,ycoordinate,this); ^ symbol: variable alien location: class movingmessagepanel
the alien variable not visible in program because declared in constructor or method , not in class.
Comments
Post a Comment