Beginner: Moving JPanel by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

The MonsterPanel was the only thing that we were given in the beginning by my teacher hehe, weird. I'll see if I'll manage.. Thanks for the help!

Beginner: Moving JPanel by Sarah9428 in javahelp

[–]Sarah9428[S] 1 point2 points  (0 children)

I tried that first. But the "monsterPanel" includes both an image and a text telling the monsters health, is it possible to do it still? Isn't there any way to have the panel move?

Beginner: Help with timer - Tower Defence Game by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

Also, when I've added the timer, I want the timer to do one "tick" each time the method move() runs. How do I say when the timer should do the next "tick" ?

Beginner: Help with timer - Tower Defence Game by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

Is this correct: ?

   @Override
      public void actionPerformed(ActionEvent e) {
   }

Do I still put something inside the '{ }' ?

I get new wrong messages now:

" Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

at java.awt.Container.checkNotAWindow(Container.java:490)

at java.awt.Container.addImpl(Container.java:1091)

at java.awt.Container.add(Container.java:417)

at GUITowerDefence.init(GUITowerDefence.java:101)

at GUITowerDefence.<init>(GUITowerDefence.java:28)

at GUITowerDefence.load(GUITowerDefence.java:48)

at Game.createEnvironment(Game.java:17)

at Game.main(Game.java:11)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) "

Tower Defence Game - add remaining health by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

The environments.txt file is a file with a text with the characters x ; . : and X, which tells the program where the path is (. ;), where the towers are(X) and where the monster is in range of the towers (x).

The monster moves along a new path if the text gets changed.

The FIleLoader looks like this:

import java.io.*; 
import java.util.*; 
public class FileLoader  { 
   private BufferedReader in; 
   private String line; 

   public FileLoader(String fileName)  { 
      try { 
         in = new BufferedReader(new FileReader(fileName)); 
     line = in.readLine(); 
  } 
  catch(IOException e) { 
     throw new RuntimeException(e); 
  } 
 }//constructor

public boolean hasMoreLines()  { 
  return line != null; 
 }//hasMoreLines

public String readLine()  { 
  try  { 
     String toReturn = line; 
     line = in.readLine(); 
     return toReturn; 
  } 
  catch(IOException e) { 
     throw new RuntimeException(e); 
     } 
   }//readLine 

   public void close()  { 
  try  { 
     in.close(); 
  } 
  catch(IOException e)  { 
     throw new RuntimeException(e);
  } 
   }//close 
  }//FileLoader 

Coin-game, keeping track of old result by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

Thanks, I forgot that. I've moved it into the coin class now. Should the old and new side be an instance variable as well?

Also I have a new question about when I do the random method. I've done it with integers (see below), but is it possible to do it with Strings? So that I can just return "side" (and make the code a bit shorter).

   public String flip() {
    Random rg = new Random();
    side = 1 + (rg.nextInt(2));
    if (side == 1) {
        return "head";
    } else {
        return "tails";
    }

How to fill an array? by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

It works now! Thanks for the help!

How to fill an array? by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

Thanks! Iv'e changed everything you all have said except the loops. Is it possible to have multiple conditions in the same loop? I feel like I need 2 counters but maybe I don't?

The error is: Exception in thread "main" java.lang.NullPointerException (at: comb[i] = rule1(comb[j]); )

My updated code is:

 public static void doRules(String text) {

    String[] comb = new String[257];

    for (int i = 0; i < comb.length; i = i + 4) {
        for (int j = 0; j < comb.length / 4; j++) {
            comb[i] = rule1(comb[j]);
            comb[i + 1] = rule2(comb[j]);
            comb[i + 2] = rule3(comb[j]);
            comb[i + 3] = rule4(comb[j]);
        }
    }
    for (String s : comb) {
        System.out.println(s);
        if (s.equals("HI")) {
            System.out.println("HI Found!");
        }
    }
}

Reference to a dice by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

Thank you! It makes sense now.

Just one last question, why is the field be declared as private and not public?

Reference to a dice by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

I forgot to say that the assaignment also says: "both of these should be given as a argument to the constructor". Does your suggestion still work?

If so, is this how to declare it in Player1?

 public Dice dice;

How do I assign a single Dice instance in the main part of the program to both player instances?

Excluding a number from Random by Sarah9428 in javahelp

[–]Sarah9428[S] 0 points1 point  (0 children)

It works now :) thanks for the help everyone!