1
我想在純Java中製作我的第一款遊戲。 我使用圖形繪製項目。更具體的g.fillRec()。 我將在稍後處理真實圖像,但現在已足夠好了。 問題是,當我想畫一個地形矩形,我有一個邊框。 這是邊界的圖片:Java上不需要的JPanel邊框在繪製時
周圍的綠色矩形的白色的東西是我的境界... 這是我的代碼:
BagGame.java
package main;
import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class BagGame extends JFrame implements WindowListener {
private static final long serialVersionUID = 1L;
private static BagPanel panel;
public BagGame(){
super("The Bag Collecting");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(this);
Container c = getContentPane();
panel = new BagPanel();
c.add(panel, "Center");
pack();
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
@Override
public void windowActivated(WindowEvent arg0) {
}
@Override
public void windowClosed(WindowEvent arg0) {
}
@Override
public void windowClosing(WindowEvent arg0) {
}
@Override
public void windowDeactivated(WindowEvent arg0) {
}
@Override
public void windowDeiconified(WindowEvent arg0) {
}
@Override
public void windowIconified(WindowEvent arg0) {
}
@Override
public void windowOpened(WindowEvent arg0) {
}
public static void main(String args[]){
new BagGame();
}
}
BagPanel.java
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class BagPanel extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private static final int PWIDTH = 720;
private static final int PHEIGHT = 480;
private static Thread game;
private static volatile boolean running = false;
private static volatile boolean gameOver = false;
private static volatile boolean paused = false;
public static Player player;
public static Terrain terrain;
public static Graphics g;
public static Image gImage;
long lastLoopTime = System.currentTimeMillis();
long delta;
int speed = 2;
private static volatile boolean upPressed = false, downPressed = false, leftPressed = false, rightPressed = false;
public BagPanel() {
setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
setBackground(Color.white);
setFocusable(true);
requestFocus();
waitForTermination();
}
public void addNotify() {
super.addNotify();
startGame();
}
public void waitForTermination() {
addKeyListener(new KeyListener() {
//player.y -= (delta * speed)/10;
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
stopGame();
}
if(keyCode == KeyEvent.VK_W){
upPressed = true;
}
if(keyCode == KeyEvent.VK_S){
downPressed = true;
}
if(keyCode == KeyEvent.VK_A){
leftPressed = true;
}
if(keyCode == KeyEvent.VK_D){
rightPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_W){
upPressed = false;
}
if(keyCode == KeyEvent.VK_S){
downPressed = false;
}
if(keyCode == KeyEvent.VK_A){
leftPressed = false;
}
if(keyCode == KeyEvent.VK_D){
rightPressed = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
public void run() {
running = true;
while (running) {
delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
gameUpdate();
gameRender();
checkMovement();
paintScreen();
try {
Thread.sleep(5);
} catch (Exception e) {
System.out.println("The thread couldn't sleep! Error info: "
+ e);
}
}
System.exit(0);
}
private void checkMovement() {
if(upPressed){
Player.y -= (delta * speed)/10;
}
if(downPressed){
Player.y += (delta * speed)/10;
}
if(leftPressed){
Player.x -= (delta * speed)/10;
}
if(rightPressed){
Player.x += (delta * speed)/10;
}
}
private void paintScreen() {
Graphics g;
try {
g = this.getGraphics();
if ((g != null) && (gImage != null))
g.drawImage(gImage, 0, 0, null);
g.dispose();
}
catch (Exception e)
{ System.out.println("Graphics context error: " + e); }
}
private void gameRender() {
if(gImage == null){
gImage = createImage(PWIDTH, PHEIGHT);
if(gImage == null){
System.out.println("image null after creating it??? Please check the code for any errors!");
} else {
g = gImage.getGraphics();
}
}
if(!paused){
g.setColor(Color.white);
g.fillRect (0, 0, PWIDTH, PHEIGHT);
g.setColor(Color.blue);
if(player != null){
player.draw(g);
}
if(terrain != null){
terrain.draw(g);
}
}
}
private void gameUpdate() {
if(!paused && !gameOver){
}
}
public void startGame() {
if (game == null) {
game = new Thread(this);
if (game == null) {
System.out.println("Couldn't create the thread!");
} else {
System.out.println("Thread created!");
game.start();
}
}
if(player == null){
player = new Player();
if(player == null){
System.out.println("The player was not created!");
} else {
System.out.println("The player is successfully created!");
}
}
if(g == null){
g = this.getGraphics();
if(g == null){
System.out.println("The graphics were not created!");
} else {
System.out.println("The graphics are successfully created!");
}
}
if(terrain == null){
terrain = new Terrain(PWIDTH, PHEIGHT);
if(terrain == null){
System.out.println("Couldn't create the terrain!");
}
}
running = true;
}
public void stopGame() {
running = false;
}
}
Player.java
package main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Player extends Rectangle {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 32;
private static final int HEIGHT = 32;
public static int x = 64;
public static int y = 64;
public Player(){
}
public void draw(Graphics g){
g.setColor(Color.blue);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(Color.white);
}
}
最後Terrain.java
package main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Terrain extends Rectangle {
private static final long serialVersionUID = 1L;
private static int TWIDTH = 0;
private static int THEIGHT = 0;
public Terrain(int width, int height){
TWIDTH = width;
THEIGHT = height;
}
public void draw(Graphics g){
g.setColor(Color.green);
g.fillRect(0, 0, TWIDTH, THEIGHT);
g.setColor(Color.white);
}
}
這是所有的代碼:)任何幫助將是巨大的! 在此先感謝!
非常感謝!它的工作:D –