java - How can I set the image to move at the given speed? -
i have image of man moves in x-axis. want move man corresponding speed of 5 meter/s delta time in nanoseconds , problem. give me idea on how it?
any appreciated...
here's code :
public class board extends canvas { public double meter;//pixel private final java.util.list<sprite> sprites = new arraylist<sprite>(); private final java.util.list<sprite> z_sorted_sprites = new arraylist<sprite>(); private bufferstrategy strategy; int x0_pixel; int y0_pixel; int x1_pixel; int y1_pixel; double x1_world; double y1_world; public board(double meter) { this.setignorerepaint(true); this.meter = meter; init(); addcomponentlistener(new componentadapter() { @override public void componentresized(componentevent e) { render(); } }); } public void init() { humanbeing humanbeing = new humanbeing(this, 2, 2, 0); sprites.add(humanbeing); z_sorted_sprites.add(humanbeing); } @override public void paint(graphics g) { } public void render() { setupstrategy(); x0_pixel = 0; y0_pixel = 0; x1_pixel = getwidth(); y1_pixel = getheight(); x1_world = x1_pixel / meter; y1_world = y1_pixel / meter; graphics2d g2d = (graphics2d) strategy.getdrawgraphics(); g2d.setbackground(color.lightgray); g2d.clearrect(0, 0, x1_pixel, y1_pixel); g2d.setcolor(color.black); (double x = 0; x < x1_world; x++) { (double y = 0; y < y1_world; y++) { int xx = converttopixelx(x); int yy = converttopixely(y); g2d.drawoval(xx, yy, 2, 2); } } (sprite z_sorted_sprite : z_sorted_sprites) { z_sorted_sprite.render(g2d); } g2d.dispose(); strategy.show(); toolkit.getdefaulttoolkit().sync(); } public int converttopixelx(double distance) { return (int) (distance * meter); } public int converttopixely(double y_world) { return (int) (y1_pixel - (y_world * meter)); } public void onzoomupdated(int value) { meter = value; render(); } private void setupstrategy() { if (strategy == null) { this.createbufferstrategy(2); strategy = this.getbufferstrategy(); } } public void start() throws interruptedexception { long previoustime = system.nanotime(); while (true) { long = system.nanotime(); long dt = - previoustime; (sprite sprite : sprites) { sprite.move(0); } render(); thread.sleep(1); previoustime = now; } } }
for human class
public class humanbeing extends sprite implements imageobserver { private java.awt.image humanimage; private final board board; private double x; private double y; private int speed; private java.util.list<sprite> objects = new arraylist<sprite>(); private int cimage; public humanbeing(board board, int x, int y, int speed) { this.board = board; this.x = x; this.y = y; this.speed = speed; url iu = this.getclass().getresource("human.jpg"); imageicon icon = new imageicon(iu); humanimage = icon.getimage(); objects.add(this); } public image getimage() { return humanimage; } @override public void move(long ns) { x += 0.001; } @override public void render(graphics2d g2d) { affinetransform t = g2d.gettransform(); final double humanheight = 1.6;// meter final double humanwidth = 1.8; //meter final double foot_position_x = humanheight / 2; final double foot_position_y = humanwidth; int xx = board.converttopixelx(x - foot_position_x); // find upper-left corner int yy = board.converttopixely(y + foot_position_y); // find upper-left corner g2d.translate(xx, yy); // ratio actual image size double x_expected_pixels = humanheight * board.meter; double y_expected_pixels = humanwidth * board.meter; double w = ((toolkitimage) humanimage).getwidth(); double h = ((toolkitimage) humanimage).getheight(); double x_s = x_expected_pixels / w; double y_s = y_expected_pixels / h; g2d.scale(x_s, y_s); g2d.drawimage(getimage(), 0, 0, this); // upper left corner g2d.setcolor(color.black); g2d.settransform(t); } @override public void moveat(double distance_x, double distance_y) { this.x = distance_x; this.y = distance_y; } @override public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { return false; } }
here's idea architecting solution. i'm going assume have figured out how many pixels image has move every second speed want. let's game or simulation, means 10 pixels every second. have starting location , ending location. know when need move image. use class scheduledthreadpoolexecutor , method schedulewithfixedrate set periodic update of image's position, issuing call draw image once every second, @ updated location. remember call position image delayed briefly swing thread servicing other gui requests. scheduled thread not affected. scheduled thread says swing put image @ position x , time 1.0 seconds. in fact, swing gets touch later @ time 1.1 seconds. don't want delta screw timing. doesn't because schedulewithfixedrate issue next swing call @ correct time 2.0 seconds, not @ 2.1 seconds. second image update @ right spot @ right time (or close enough depending on how busy gui thread is).
Comments
Post a Comment