bluej - Java Turtle Animation Issue -
i trying make animation 2 rectangles appear , disappear frame when type in: c turtle graphics. problem have not understand how incorporate turtle graphics loop. this, have use do-while loop. suppose have rectangles made move horizontally across screen. have general idea set out, not know how use turtle graphics loop. code not orderly when tried set here.
/** * write description of class animation here. * * @author (author) * @version */ import java.util.scanner; import java.awt.*; class animation { //set conditions turtle start drawing public void prepareturtletodraw(turtle myrtle, color color, int x, int y) { myrtle.hide(); myrtle.penup(); //pick pen avoid leaving trail when moving turtle myrtle.setcolor(color); //set myrtle's color myrtle.moveto(x, y); //move coordinates myrtle.pendown(); //put pen down start drawing }//end of prepareturtletodraw method //draw line public void drawline(turtle myrtle, int x1, int y1, int x2, int y2)//, int width) { myrtle.moveto(x1, y1); //moves coordinate first myrtle.moveto(x2, y2); //then moves coordinate //myrtle.setpenwidth(width); //this adjusts size of lines }//end of drawline method public static void pressc() { string userinput = ""; //declare , initialize string variable char key = ' '; //declare , initialize char variable scanner in = new scanner(system.in); //construct scanner object system.out.println("please press c key watch animation."); //do-while loop wait user enter letter c { userinput = in.next(); //accept 1 token keyboard in.nextline(); //flush buffer key = userinput.charat(0); //picks off first character userinput string variable } while(key != 'c'); //do-while condition statement system.out.println("thank you. may continue"); }//end of main method } public class animationtester { public static void main(string[] args) { //picture pictureobj = new picture(""); //create picture object maze background image, has name , etc. world worldobj = new world(); //create world object draw in //worldobj.setpicture(pictureobj); //set maze background image in world object turtle lertle = new turtle(300, 150, worldobj); //create turtle object drawing animation turt = new animation(); turtle dyrtle = new turtle(150, 150, worldobj); turt.prepareturtletodraw(lertle, color.black, 250, 150); turt.drawline(lertle, 250, 150, 400, 150); turt.drawline(lertle, 400, 150, 400, 250); turt.drawline(lertle, 400, 250, 250, 250); turt.drawline(lertle, 250, 250, 250, 150); turt.prepareturtletodraw(dyrtle, color.red, 150, 150); turt.drawline(dyrtle, 150, 150, 260, 75); turt.drawline(dyrtle, 260, 75, 335, 150); turt.drawline(dyrtle, 335, 150, 225, 225); turt.drawline(dyrtle, 225, 225, 150, 150); system.out.println(worldobj); } }
well, seems can't upload photos took of program because don't have enough reputation. thanks!
figure 1. before pressing c
figure 2. after pressing c
wrote little program you, running on java swing create animations. have 3 rectangles, 2 - red , blue - fading in , out according how seconds have elapsed, , third appearing , disappearing upon pressing c.
think may helpful when dealing animations "game loop." can find implementation of in mainframe.java below. loosely speaking, game loop controls update speed of animations program runs consistently on both slow , fast computers. if game loop not implemented, fast computer may finish animation faster relatively slower computer.
complete program, compile 3 .java files , run main bring game interface.
main.java
import java.lang.reflect.invocationtargetexception; import javax.swing.swingutilities; public class main { public static void main(string[] args) { try { swingutilities.invokeandwait(() -> { mainframe mf = mainframe.getmainframe(); new thread(mf).start(); }); } catch (invocationtargetexception | interruptedexception e) { system.out.println("could not create gui"); } } }
mainframe.java
import java.awt.borderlayout; import javax.swing.jframe; class mainframe extends jframe implements runnable { private static final long serialversionuid = 1l; private displaypanel dp; private boolean isrunning; private double secondsperframe = 1.0 / 60.0; private static mainframe mf; private mainframe() { super("title"); dp = new displaypanel(); add(dp, borderlayout.center); // add display center setdefaultcloseoperation(exit_on_close); pack(); setvisible(true); } /** * static factory * * @return singleton mainframe */ static mainframe getmainframe() { return mf == null ? mf = new mainframe() : mf; } /** * game loop */ @override public void run() { isrunning = true; int frames = 0; double framecounter = 0; double lasttime = system.nanotime() / 1000000000.0; double unprocessedtime = 0; while(isrunning) { boolean render = false; double starttime = system.nanotime() / 1000000000.0; double passedtime = starttime - lasttime; lasttime = starttime; unprocessedtime += passedtime; framecounter += passedtime; while(unprocessedtime > secondsperframe) { render = true; unprocessedtime -= secondsperframe; // update state of rectangles' brightness dp.update(secondsperframe); if(framecounter >= 1.0) { // show fps count. updates every second dp.setfps(frames); frames = 0; framecounter = 0; } } if(render) { // render rectangles dp.render(); frames++; } else { try { thread.sleep(1); } catch (interruptedexception ie) {} } } } }
displaypanel.java
import java.awt.canvas; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.image.bufferstrategy; // create display within window class displaypanel extends canvas implements keylistener { private static final long serialversionuid = 2l; private graphics2d g2; // drawing tool private bufferstrategy strategy; // drawing tool private fadingrectangle[] fadingrectangles; // appears/disappears based on elapsed time private fadingrectangle usercontrolledrectangle; // appears/disappears upon command user private int fps; // used display fps on screen displaypanel() { setpreferredsize(new dimension(800, 600)); addkeylistener(this); setfocusable(true); fadingrectangles = new fadingrectangle[2]; fadingrectangles[0] = new fadingrectangle(150, 250, 100, 100); fadingrectangles[1] = new fadingrectangle(550, 250, 100, 100); usercontrolledrectangle = new fadingrectangle(350, 100, 100, 100); } /** * updates brightness of rectangles * * @param elapsedseconds seconds elapsed since last call method */ void update(double elapsedseconds) { fadingrectangles[0].update(elapsedseconds); fadingrectangles[1].update(elapsedseconds); } /** * draw */ void render() { // prepare drawing tools if (strategy == null || strategy.contentslost()) { createbufferstrategy(2); strategy = getbufferstrategy(); graphics g = strategy.getdrawgraphics(); this.g2 = (graphics2d) g; } // anti-aliasing this.g2.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on); // clear screen drawing background on top this.g2.setcolor(color.black); this.g2.fillrect(0, 0, getwidth(), getheight()); // draw rectangles fadingrectangles[0].draw(new color(255, 0, 0)); fadingrectangles[1].draw(new color(0, 0, 255)); usercontrolledrectangle.draw(color.white); // draw fps count on upper left corner g2.setcolor(color.white); g2.drawstring("fps: " + integer.tostring(fps), 10, 20); // set drawn lines visible if(!strategy.contentslost()) strategy.show(); } /** * @param fps fps drawn when render() called */ void setfps(int fps) { this.fps = fps; } /** * used draw rectangles in display */ private class fadingrectangle { private int x, y, width, height; // location , size of rectangle private double secondspassed; // arbitrary number determines brightness of blue private fadingrectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } /** * called render() * * @param color color of rectangle drawn */ private void draw(color color) { // determine color double fade = math.abs(math.sin(secondspassed)); int red = (int) (color.getred() * fade); int green = (int) (color.getgreen() * fade); int blue = (int) (color.getblue() * fade); g2.setcolor(new color(red, green, blue)); // draw rectangle g2.drawrect(x, y, width, height); } private void update(double elapsedseconds) { secondspassed += elapsedseconds; } } // quick , dirty implementation. should fixed make clearer @override public void keyreleased(keyevent e) { if(e.getkeycode() == keyevent.vk_c) { usercontrolledrectangle.update(math.pi / 2); } } @override public void keypressed(keyevent e) {} @override public void keytyped(keyevent e) {} }
Comments
Post a Comment