java - Graphics will not cover entire canvas -
know: canvas displayed properly, due yellow. however, graphics not cover canvas completely, nor law class recognized past end of black area...
so causing happen? , how draw on undrawable section of canvas(yellow part), or should implement graphics way?
edit: ui class creates canvas , buffer, graphics class takes on , starts drawing on them, reason cannot in yellow section, nor law class handles collision red cube , walls of app, regogize yellow area valid place go. through same variables dimensions, used everywhere.
main class
package app; public class main { static final int x = 1024; static final int y = 680; static final int sanic = 10; int fps = 0; int frames = 0; long totaltime = 0; long curtime = system.currenttimemillis(); long lasttime = curtime; static int[] pos; graphics graphics; law physics; static int status; boolean holdstate; public main() { pos = new int[5]; pos[1] = x; pos[2] = y; } public void launch() { // audio sound = new audio(); graphics = new graphics(); physics = new law(); graphics.draw(); // sound.play(); handle(); } public void update() { graphics.cache(); physics.check(); graphics.render(); try { thread.sleep(10); } catch (exception e) { } { graphics.dump(); } } public void handle() { while (!isover()) { if (!ispaused()) { update(); } } } boolean isover() { if (status == 1) { status = 0; return true; } return false; } boolean ispaused() { if (status == 2) { status = 0; if (!holdstate) { holdstate = true; pos[3] = pos[1]; pos[4] = pos[2]; } else if (holdstate) { holdstate = false; pos[1] = pos[3]; pos[2] = pos[4]; } } return holdstate; } public static void main(string[] args) { main game = new main(); game.launch(); } }
ui class
package app; import java.awt.*; import java.awt.image.*; import java.net.url; import javax.swing.*; public class ui extends main { jframe mainwin; canvas wall; url pic; toolkit kit; image img; bufferstrategy buffer; graphics2d shell; graphics2d ball; public ui() { mainwin = new jframe("game"); wall = new canvas(); wall.addkeylistener(new input()); } void draw() { frame(); icon(); canvas(); show(); prep(); } void frame() { mainwin.setdefaultcloseoperation(jframe.exit_on_close); mainwin.setbackground(color.blue); mainwin.setignorerepaint(true); } void icon() { pic = classloader.getsystemresource("res/app.png"); kit = toolkit.getdefaulttoolkit(); img = kit.createimage(pic); mainwin.seticonimage(img); } void canvas() { wall.setbackground(color.yellow); wall.setignorerepaint(true); wall.setbounds(0, 0, x, y); } void show() { mainwin.add(wall); mainwin.pack(); mainwin.setresizable(false); mainwin.setlocationrelativeto(null); mainwin.setvisible(true); } void prep() { wall.createbufferstrategy(2); buffer = wall.getbufferstrategy(); } }
graphics class
package app; import java.awt.*; public class graphics extends ui { public void render() { mask(); player(); fps(); buffer.show(); } void cache() { shell = (graphics2d) buffer.getdrawgraphics(); ball = (graphics2d) buffer.getdrawgraphics(); } void dump() { shell.dispose(); ball.dispose(); } void mask() { shell.setcolor(color.black); shell.fillrect(0, 0, x, y); } void fps() { lasttime = curtime; curtime = system.currenttimemillis(); totaltime += curtime - lasttime; if (totaltime > 1000) { totaltime -= 1000; fps = frames; frames = 0; } frames++; shell.setcolor(color.green); shell.setfont(new font("courier new", font.plain, 12)); shell.drawstring(string.format("fps: %s", fps), 20, 20); } void player() { ball.setcolor(color.red); ball.fillrect(pos[1], pos[2], 32, 32); } }
law class
package app; public class law extends main { public void check() { out(); } void out() { if (pos[1] < 0) { pos[1] = 0; } if (pos[2] < 0) { pos[2] = 0; } if (pos[1] > x - 32) { pos[1] = x - 32; } if (pos[2] > y - 32) { pos[2] = y - 32; } } }
this bug frame#setresizable
. don't ask me why, wants add 10
pixels width , height of frame.
the best solution know call setresizable
before pack
void show() { mainwin.add(wall); mainwin.setresizable(false); // call me first mainwin.pack(); mainwin.setlocationrelativeto(null); mainwin.setvisible(true); }
Comments
Post a Comment