swing - Java 'Background' opens to small to see anything -
i making game , game window open size of title. please let me know changes need make "b.setsize(900,885)" work. change or add amount of code if needed.
import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image; import java.awt.rectangle; import java.awt.window; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.awt.event.keylistener; import javax.swing.*; public class background extends jframe implements keylistener, actionlistener{ int platform1x = 300, platform1y = 555, platform2x, platform2y; image image; public boolean isjumping = false; int playerx = 100, playery = 585, velx = 0, vely = 0; public boolean[] keydown = new boolean[4]; private int fallingspeed = 0; private int gravity = 5; private int jumppower = -30; public background(){ addkeylistener(this); setfocusable(true); setfocustraversalkeysenabled(false); keydown[0] = false; keydown[1] = false; keydown[2] = false; keydown[3] = false; } public void paint(graphics g){ if(!isonground()){ fall(); } imageicon = new imageicon("/users/kairotieremorton/documents/java code/kairo , enoch game/src/images/backgroundsquareman.png"); image = i.getimage(); g.drawimage(image, 0, 0, 900, 700, null); g.setcolor(color.blue); g.fillrect(platform1x, platform1y, 120, 20); g.setcolor(color.red); g.fillrect(playerx, playery, 30, 30); try { thread.sleep(40); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } repaint(); playery = playery + vely; playerx = playerx + velx; } public static void main(string[] args){ background b = new background(); b.settitle("game"); b.setsize(900,885); b.setdefaultcloseoperation(jframe.exit_on_close); b.setvisible(true); } public void keypressed(keyevent e) { int key = e.getkeycode(); if(isonground() && key == keyevent.vk_space){ keydown[0] = true; jump(); } //if(key == keyevent.vk_down){ setvely(5); keydown[1] = true;} if(key == keyevent.vk_left) { setvelx(-5); keydown[2] = true;} if(key == keyevent.vk_right) { setvelx(5); keydown[3] = true;} system.out.println(key); system.out.println(playerx); system.out.println(playery); } public void keyreleased(keyevent e) { int key = e.getkeycode(); if(key == keyevent.vk_space){ keydown[0] = false;} //tempobject.setvely(0); //if(key == keyevent.vk_down) keydown[1] = false;//tempobject.setvely(0); if(key == keyevent.vk_left) keydown[2] = false;//tempobject.setvelx(0); if(key == keyevent.vk_right) keydown[3] = false; //tempobject.setvelx(0); //vertical movement if(!keydown[0]) setvely(0); system.out.println(isonground()); //horizontal movement if(!keydown[2] && !keydown[3]) setvelx(0); } public void keytyped(keyevent e) {} public void actionperformed(actionevent e) { //playery = playery + vely; //playerx = playerx + velx; //repaint(); } public boolean isonground(){ if(playery + (30 /2) >= 585) { return true; } return false; } public void fall(){ playery = playery + fallingspeed; fallingspeed = fallingspeed + gravity; } public void jump(){ fallingspeed = jumppower; fall(); } public void setvelx(int velx) { this.velx = velx; } public void setvely(int vely) { this.vely = vely; } public float getvelx(){ return velx; } public float getvely(){ return vely; } public int getplayerx(){ return playerx; } public int getplayery(){ return playery; } public void setplayerx(int playerx) { this.playerx = playerx; } public void setplayery(int playery) { this.playery = playery; } public rectangle getbounds(){ return new rectangle(getplatform1x(), platform1y(), 120, 20); } public int getplatform1x(){ return platform1x; } public int platform1y(){ return platform1y; } public rectangle getboundstop(){ return new rectangle(getplayerx() + 10, getplayery(), 30 - 20, 5); } public rectangle getboundsbottom(){ return new rectangle(getplayerx() + 10, getplayery() + 30 - 5, 30 - 20, 5); } public rectangle getboundsleft(){ return new rectangle(getplayerx(), getplayery()+10, 5, 30 -20); } public rectangle getboundsrigth(){ return new rectangle(getplayerx() + 30 - 5, getplayery()+10, 5, 30 - 20); } }
edit:
a better way achieve same result call b.setpreferredsize(dimension); , pack() frame.
see answer more information jframe.pack() : https://stackoverflow.com/a/22982334/5224040
try in main method:
public static void main(string[] args) { background b = new background(); dimension d = new dimension(900, 885); //create new dimension b.settitle("game"); b.setpreferredsize(d); //set preferredsize; b.setdefaultcloseoperation(jframe.exit_on_close); b.pack(); //pack frame b.setvisible(true); //setting visible }
you can use following methods reposition frame necessary:
b.setlocationrelativeto(null); //center location on screen. b.setlocation(0, 0); //set location explicitly
call either of these methods below b.pack(); reposition frame.
Comments
Post a Comment