Class's Integer use in Rect's width

Class's Integer use in Rect's width

Soo,我试过在游戏中写hp界面,width rect会有hp*20和20 hight。这将是一个 类 和 "gifs" 的游戏(我的第一个正常 game:D)。如何用 Tim.hp*20 写 width rect ?如果您阅读本文并有所帮助,祝您编码顺利!

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;
}

你显然有一个编译错误(hp cannot be resolved to a variable),因为你不知道如何访问外来对象warior(正确拼写:warrior?)的字段hp

您在 main 函数中创建了 warior 的实例,但不保留它的引用。您在 tim 上设置的所有内容都只能在您有权访问 tim 的地方访问。在您的代码中,那是 only in main()

根据hpwarior的功能,您有以下选择:

  1. 声明 int hp 静态并使用 warior.hp 访问它(风格不佳)
  2. singleton pattern用于classwarior
  3. myFrame()添加参数warior tim,将其存储在实例字段中并在函数paintComponent()
  4. 中使用

在 1. 和 2. 中,您将只有一个变量 (1.) 或对象 (2.) 的实例

在 3. 中,您可以有多个 warior 实例,但必须跟踪您正在使用的对象。这是通常的用例。

3 的示例:

--- orig    2015-11-20 14:23:05.221578051 +0100
+++ mod 2015-11-20 14:24:46.691333378 +0100
@@ -12,19 +12,20 @@

 public static void main(String[] args) 
 {
-    myFrame window = new myFrame();
-
     warior Tim = new warior();
     Tim.hp=100;
+
+    myFrame window = new myFrame(Tim);
+
 }

 }

 class myFrame extends JFrame
 {
-public myFrame()
+public myFrame(warior tim)
 {
-    myPanel panel = new myPanel();
+    myPanel panel = new myPanel(tim);
     Container cont = getContentPane();
     cont.add(panel);
     setBounds(0,0,1900,1000);
@@ -42,9 +43,12 @@
 private int x2=550,y2=300;
 private int n=1;
 private int n2=1;
+private final warior tim;

-public myPanel()
+public myPanel(warior tim)
 {
+    this.tim = tim;
+
     setFocusable(true);

         Timer nt = new Timer(1000,new ActionListener()
@@ -78,7 +82,7 @@
     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
+    gr.fillRect(10, 800, 20, tim.hp*20); //there is an error
 }
 }

顺便说一句:根据 java naming conventions,您应该对变量使用小写字母(tim 而不是 Tim),对 classes 使用 CamelCase(Warrior 而不是warior)