Search code examples
javaclassscopegraphics2d

Class's Integer use in Rect's width


Soo, I tried to write hp interface in game, width rect will have hp*20 and 20 hight. It'll be a game with classes and "gifs" (my first normal game:D).How to write width rect with Tim.hp*20 ? If you're read this and help, good luck in codding!

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

Solution

  • You obviously have a compile error (hp cannot be resolved to a variable) because you don't know how to access field hp of the foreign object warior (correct spelling: warrior?).

    You create an instance of warior in the main function but don't keep the reference of it. Everything you set on tim will be only accessible on places where you have access to tim. In your code, that's only in main()

    Depending on what's the function of hp and warior, you have the following options:

    1. Declare int hp static and access it using warior.hp (not good style)
    2. Use the singleton pattern for the class warior
    3. Add a parameter warior tim for myFrame(), store it there in an instance field and use it in the function paintComponent()

    In 1. and 2. you will only have one instance of the variable (1.) or the object (2.)

    In 3., you can have several instances of warior but have to keep track of which object you are using. This is the usual use case.

    Example for 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
     }
     }
    

    Btw: according to java naming conventions you should use lowercase for variables (tim instead of Tim) and CamelCase for classes (Warrior instead of warior)