试图制作一个简单的游戏,但结果是空白的灰色屏幕 [英] Trying to make a simple game but thr result is blank grey screen
本文介绍了试图制作一个简单的游戏,但结果是空白的灰色屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计们,
正如我在标题中提到的那样,我正试图从公共导游那里制作一个简单的游戏。
i不知道为什么我只能得到是一个空白的灰色屏幕。
希望得到你的一些指导!
这里是我的代码:
main
Hey guys,
as i mentioned in the title i'm trying to make a simple game from a public guide.
i dont know why but all i can get is a blank grey screen.
hope to get some guidance from you!
here is my code:
main
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
public class Frame extends JFrame{
public static String title = "Tower Defence ";
public static Dimension size = new Dimension(700,550);
public Frame(){//main frame
setTitle(title);
setSize(size);
setResizable(false);
setBackground(null);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init(){
setLayout(new GridLayout(1, 1, 0, 0));//set a window inside our main frame
Screen screen = new Screen();
add(screen); //******update*******
setVisible(true);
}
public static void main(String[] args){
Frame frame = new Frame();
}
}
sub:
sub's:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class Screen extends JPanel implements Runnable {
public Thread t1 = new Thread(this);
public static boolean isFirst = true;
public static int myWitdh,myHeight;
public static Room room;
public Screen(){
t1.start();
}
public void paintComponenr(Graphics g){
if(isFirst){
define();
isFirst = false;
}
g.clearRect(0, 0, getWidth(), getHeight());
room.draw(g);//drawing the room
}
private void define() {
room = new Room();
}
public void run(){
while(true){
if(!isFirst){
room.physic();
}
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {}
}
}
}
import java.awt.Graphics;
public class Room {
public int worldWidth = 10;
public int worldheight = 6;
public int blockSize = 32;
public Block[][]block;
public Room(){
define();
}
private void define() {
block = new Block[worldWidth][worldheight];
for( int y=0 ; y<block.length ; y++ ){
for( int x=0 ; x<block[0].length ; x++ ){
block[y][x] = new Block( x*blockSize, y*blockSize, blockSize, blockSize, 0, 0);
}
}
}
public void physic(){
}
public void draw(Graphics g){
for( int y=0 ; y<block.length ; y++ ){
for( int x=0 ; x<block[0].length ; x++ ){
block[y][x].draw(g);
}
}
}
}
import java.awt.Graphics;
import java.awt.Rectangle;
public class Block extends Rectangle {
public int groundID;
public int airID;
public Block(int x, int y, int w, int h,int ground ,int air){
setBounds(x,y,w,h);
this.groundID = ground;
this.airID = air;
}
public void draw(Graphics g){
g.drawRect(x, y, width,height );
}
}
推荐答案
对我感到羞耻,问题发生在Screen Class,instedpublic void paintComponenr (图形g)
它应该有public void paintComponen t (图形g)
对不起所有的麻烦
Shame on me, the problem was in Screen Class, insted "public void paintComponenr(Graphics g)"
it should have "public void paintComponent(Graphics g)"
sorry for all the trouble
这篇关于试图制作一个简单的游戏,但结果是空白的灰色屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文