我只是試圖製作一個大學的遊戲,這是一個玩家用鍵盤導航以通過迷宮的球。唯一困擾的是我的背景是一個迷宮的圖像,當我添加「球」時,當我註釋掉油漆和油漆組件時,重新繪製背景(虛化顏色,沒有圖像),迷宮背景又回來了,但當然沒有球。作爲背景和繪畫的組成部分
我是一個新來的java,我已經搜索了這個,並且無法看到我已經擁有的代碼的良好靈魂。它是不透明的?是不透明的?......即使是正確的方向也不透明?
一旦我得到這個...我完全瞭解使用碰撞檢測來檢測與迷宮「牆」
試圖弄清楚這一點,是對Java的碰撞,可以使一個成年男子哭泣。
main.java
public class Main{
public static void main (String[] args){
//System.out.println("Start of the Game");
GameFrame game1 = new GameFrame();
}
JavaGame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.io.File;
import java.io.IOException;
public class GameFrame extends JFrame{
private int frameWidth = 240, frameHeight =315;
int x, y;
private Image dbImage;
private Graphics dbg;
//Below is the constructor
public GameFrame(){
super("OperationMaze!");
addKeyListener(new AL());
setResizable(false);
//setTitle("OperationMaze!2");
//setSize(250, 302);
setSize(frameWidth, frameHeight); //Set height and width
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Because of opening file, there needs to be a try catch just incase there is an error opening.
//Plus java won't let you unless you try catch.
try{
setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Documents and Settings/brando/My Documents/NetBeansProjects/GameFrame/src/maze.jpg")))));
} catch (IOException e) {
e.printStackTrace();
}
//Show the frame
setVisible(true);
//setOpaque(false);
y = 35;
x = 15;
}
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g) {
g.fillOval(x, y, 15, 15);
//g.setOpaque(false);
repaint();
}
// private void setOpaque(boolean b) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT) {
if(x <= 5)
x = 5;
else
x += -5;
}
if(keyCode == e.VK_RIGHT) {
if(x >= 230)
x = 230;
else
x += +5;
}
if(keyCode == e.VK_UP) {
if(y <= 23)
y=23;
else
y += -5;
}
if(keyCode == e.VK_DOWN) {
if(y >= 277)
y = 277;
else
y += +5;
}
}
}
}