java - Rectangle.y issue, persistently rendering at 0 -
i'm trying render rectangle jpanel:
playerrect = new rectangle(100,100,10,10);
problem is, playerrect renders @ 100,0 every time. i've updated eclipse , java, troubleshoot code , played x, y, width, height (although i'm not sure how code affect java.awt.rectangle).
any clue causing this?
package game; import java.awt.color; import java.awt.graphics; import java.awt.image; import java.awt.rectangle; import java.awt.event.mouseevent; import javax.swing.imageicon; public class player { private world world; private rectangle playerrect; private image playerimg; protected int xdirection, ydirection; //block variables private int hoverx, hovery; private boolean hovering; public player(world world){ this.world = world; playerimg = new imageicon("d:/student data/gametest1/gameengine/res/player.png").getimage(); playerrect = new rectangle(100, 100, 10, 10); // ### here's issue ### } private void setxdirection(int d){ xdirection = d; } private void setydirection(int d){ ydirection = d; } public void update(){ move(); checkforcollision(); } private void move(){ playerrect.x += xdirection; playerrect.y =+ ydirection; } private void checkforcollision(){ } //drawing methods public void draw(graphics g){ g.drawimage(playerimg, playerrect.x, playerrect.y, null); } private void drawblackoutline(graphics g){ g.setcolor(color.black); g.drawrect(hoverx,hovery, world.blocks[0].width, world.blocks[0].height); if(hovering){drawblackoutline(g);} } //mouse events public void mousepressed(mouseevent e){ } public void mousereleased(mouseevent e){ } public void mousemoved(mouseevent e){ int x = e.getx(); int y = e.gety(); int px = playerrect.x; int py = playerrect.y; for(int =0; <world.arraynum; i++){ if(world.blocks[i].contains(x,y)){ hovering = true; hoverx = world.blocks[i].x; hovery = world.blocks[i].y; break; }else{hovering = false;} } } public void mousedragged(mouseevent e){ } private class weapon{ public static final int unarmed = 0; public static final int pickaxe = 1; public static final int gun = 2; public int current_weapon; public weapon( int w){ switch(w){ default: system.out.println("no weapon sellected"); break; case unarmed: current_weapon = unarmed; break; case pickaxe: current_weapon = pickaxe; break; case gun: current_weapon = gun; break; } } public void selectweapon( int w){ switch(w){ default: system.out.println("no weapon sellected"); break; case unarmed: current_weapon = unarmed; break; case pickaxe: current_weapon = pickaxe; break; case gun: current_weapon = gun; break; } } public boolean isequipped(int w){ if(w == current_weapon){ return true; } else return false; } } }
here's rectangle drawn jpanel:
package game; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.jpanel; public class gamepanel extends jpanel implements runnable{ private static final long serialversionuid = 1l; //double buffering private image dbimage; private graphics dbg; //jpanel variables static final int gwidth = 900, gheight = 600; static final dimension gamedim = new dimension(gwidth, gheight); //game variables private thread game; private volatile boolean running = false; public int tickcount = 0; private static final int delays_before_yield = 10; private long period = 6*1000000; //ms --> nano //game objects world world; player p1; public gamepanel(){ world = new world(); p1 = new player(world); setpreferredsize(gamedim); setbackground(color.white); setfocusable(true); requestfocus(); //handle key inputs user addkeylistener(new keyadapter(){ @override public void keypressed(keyevent e){ if(e.getkeycode() == keyevent.vk_w){ world.navigatemap(world.pan_up);} if(e.getkeycode() == keyevent.vk_s){ world.navigatemap(world.pan_down);} if(e.getkeycode() == keyevent.vk_a){ world.navigatemap(world.pan_left);} if(e.getkeycode() == keyevent.vk_d){ world.navigatemap(world.pan_right);} } @override public void keyreleased(keyevent e){ world.stopmovemap(); } @override public void keytyped(keyevent e){ } }); addmouselistener(new mouseadapter(){ @override public void mousepressed(mouseevent e){ } @override public void mousereleased(mouseevent e){ } @override public void mouseclicked(mouseevent e){ } }); addmousemotionlistener(new mouseadapter(){ @override public void mousemoved(mouseevent e){ p1.mousemoved(e); } @override public void mousedragged(mouseevent e){ } @override public void mouseentered(mouseevent e){ } @override public void mouseexited(mouseevent e){ } }); } private void startgame(){ if(game == null || !running){ game = new thread(this); game.start(); running = true; } } public void addnotify(){ super.addnotify(); startgame(); } public void stopgame(){ if(running){ running = false; } } public void run() { long lasttime = system.nanotime(); long beforetime, aftertime, diff, sleeptime, oversleeptime = 0; int delays = 0; while(running){ beforetime =system.nanotime(); gameupdate(); gamerender(); paintscreen(); aftertime = system.nanotime(); diff = aftertime - beforetime; sleeptime = (period - diff) - oversleeptime; //if sleep time between 0 , period, sleep if(sleeptime < period && sleeptime > 0){ try { game.sleep(sleeptime/1000000l); oversleeptime = 0; } catch (interruptedexception e) { system.err.println("you done goofed!"); } } //the difference greater period else if(diff>period){ oversleeptime = diff - period; } //accumulate amount of delays, , yield else if(++delays >= delays_before_yield){ game.yield(); } //the loop took less time expected,but need make oversleep time else{oversleeptime = 0;}} } private void gameupdate(){ if(running && game != null){ //update game state world.movemap(); p1.update(); } } private void gamerender(){ if(dbimage == null){ //create buffer dbimage = createimage(gwidth, gheight); if(dbimage == null){ system.err.println("dbimage still null!"); return; }else{ dbg = dbimage.getgraphics(); } } //clear screen dbg.setcolor(color.black); dbg.fillrect(0, 0, gwidth, gheight); //draw game elements draw(dbg); } //##### draw game content in method #####// public void draw(graphics g){ world.draw(g); p1.draw(g); //g.setcolor(color.red); //g.setfont(new font("pr celtic narrow", font.bold, 50)); //string str = "mentalbrink lv. 5"; //g.drawstring(str, 100,100); } private void paintscreen(){ graphics g; try{ g = this.getgraphics(); if(dbimage != null && g != null){ g.drawimage(dbimage, 0, 0, null); } toolkit.getdefaulttoolkit().sync(); //for linux people. g.dispose(); }catch(exception e){ system.err.println(e); } } private void log(string s){ system.out.println(s); } }
Comments
Post a Comment