【发布时间】:2015-11-20 08:22:46
【问题描述】:
Soo,我尝试在游戏中编写 hp 界面,宽度 rect 将有 hp*20 和 20 高度。这将是一个带有类和“gifs”的游戏(我的第一个普通游戏:D)。如何用 Tim.hp*20 编写宽度矩形?如果你读到了这篇文章并得到帮助,祝你编程好运!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
public class main {
public static void main(String[] args)
{
myFrame window = new myFrame();
warior Tim = new warior();
Tim.hp=100;
}
}
class myFrame extends JFrame
{
public myFrame()
{
myPanel panel = new myPanel();
Container cont = getContentPane();
cont.add(panel);
setBounds(0,0,1900,1000);
setVisible(true);
}
}
class myPanel extends JPanel
{
private Image gif1;
private Image gif2;
private int x1=850,y1=300;
private int x2=550,y2=300;
private int n=1;
private int n2=1;
public myPanel()
{
setFocusable(true);
Timer nt = new Timer(1000,new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
gif1=ImageIO.read(new File("C:\\Users\\Тима\\Desktop\\"+ n +".solider.png"));
n++;
System.out.println(n);
if (n>=3) n=1;
System.out.println(n);
gif2=ImageIO.read(new File("C:\\Users\\Тима\\Desktop\\"+ n2 +".mutant.png"));
n2++;
System.out.println(n2);
if (n2>=3) n2=1;
System.out.println(n2);
}
catch (Exception exp) {}
repaint();
}
});
nt.start();
}
public void paintComponent(Graphics gr)
{
gr.clearRect(x1, y1, gif1.getWidth(null), gif1.getHeight(null));
gr.drawImage(gif1, x1, y1, null);
gr.clearRect(x2, y2, gif2.getWidth(null), gif2.getHeight(null));
gr.drawImage(gif2, x2, y2, null);
gr.fillRect(10, 800, 20, hp*20); //there is an error
}
}
class warior
{
int hp;
}
【问题讨论】:
-
@Daniel Alder,是的.. 如何用 Tim.hp*20 编写宽度矩形?
-
会发生什么? 1)在错误的地方 2)你有一个运行时错误 3)你有一个编译错误。不要指望 200 人执行你的代码只是为了找出你的代码有什么问题
-
@Daniel Alder,矩形宽度为 2000(Tim.hp=100,矩形宽度为 100*20 之后),但矩形宽度为 20
-
不可能:当我尝试编译代码时,我看到了错误:
p cannot be resolved to a variable。 此代码不会在屏幕上绘制任何内容。
标签: java class scope graphics2d