【问题标题】:Class's Integer use in Rect's width类在 Rect 宽度中的 Integer 使用
【发布时间】: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


【解决方案1】:

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

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

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

  1. 声明int hp static 并使用warior.hp 访问它(不好的风格)
  2. 使用singleton patternwarior
  3. myFrame()添加参数warior tim,将其存储在实例字段中并在函数paintComponent()中使用

在 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),对类使用 CamelCase(Warrior 而不是warior

【讨论】:

  • 你能举个例子吗?
猜你喜欢
  • 2018-04-14
  • 2013-09-25
  • 2014-04-01
  • 2018-02-26
  • 2012-05-22
  • 2017-06-03
  • 1970-01-01
  • 2019-11-25
  • 2020-03-21
相关资源
最近更新 更多