Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Solved on: 2010-04-18 04:09:18
Quote
You should leave your original post after your problem is solved so it can help out other people who may be having the same problem...

Oooops, sorry. I'm new to using forums still learning. I'll keep that in mind for next time
2  Game Development / Newbie & Debugging Questions / Re: Rare Black Screen && Save/Load Actors Problem on: 2010-04-16 04:57:48
Thanks h3ckboy, that's better.

Simulator Source Code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
package simulator;

import java.awt.Color;
import java.awt.Graphics2D;

import util.InputGUI;
import util.InputGUI.ExhaustedInputAttempts;
import view.View;

import actors.Actor;
import army.Army;

/**
 * Binds the model and view components of the <i>Lord of the Rings Battlefield Simulator</i>; also implements the launch of main().
 *
 * @author Rex Woollard
 * @see Army
 * @see View
 */

public class Simulator implements Runnable{
   // Instance variables for full Simulator
  /** Reference to one of the two <i>Army</i> objects that comprise the model. */
   private Army armyForcesOfLight;
   /** Reference to one of the two <i>Army</i> objects that comprise the model. */
   private Army armyForcesOfDarkness;
   /** Reference to a type of JFrame, where the <i>Actor</i> objects will be displayed. */
   private View view;

   /** Entry point to program execution of full simulator. */
   public static void main(String[] args) {
      Simulator simulator = new Simulator(); // creates Simulator object; invokes constructor which builds two Army objects and GUI window / panel for battlefield display
     simulator.manageSimulationEnvironment();
   }

   public Simulator() {
      armyForcesOfLight = new Army("Blue", Color.green);
      armyForcesOfDarkness = new Army("Red", Color.RED);
      view = new View(1616, 938);
      view.setSimulator(this);
   } // Simulator constructor

   /** Main controlling menu to setup the battlefield, then launch the simulation. */
   public void manageSimulationEnvironment() {
      try {
         int nMenuChoice;
         do {
            nMenuChoice = menuGUI();
            switch (nMenuChoice) {
            case 1:
               armyForcesOfLight.createActors(view);
               break; // Reference to View object is sent to help define the boundaries of X,Y values in Point2D coordinates
           case 2:
               armyForcesOfLight = armyForcesOfLight.loadFromDisk();
               view.repaint();
               break;
            case 3:
               armyForcesOfLight.saveToDisk();
               break;
            case 4:
               armyForcesOfLight.editOneActor();
               break;
            case 5:
               armyForcesOfLight.display(System.out);
               break;

            case 6:
               armyForcesOfDarkness.createActors(view);
               break; // Reference to View object is sent to help define the boundaries of X,Y values in Point2D coordinates
           case 7:
               armyForcesOfDarkness = armyForcesOfDarkness.loadFromDisk();
               break;
            case 8:
               armyForcesOfDarkness.saveToDisk();
               break;
            case 9:
               armyForcesOfLight.editOneActor();
               break;
            case 10:
               armyForcesOfDarkness.display(System.out);
               break;

            case 11:
               new Thread(this).start();
               break;
            }
            view.repaint();
         } while (nMenuChoice != 0);
      } catch (ExhaustedInputAttempts e) {
         InputGUI.showErrorGUI(e.getMessage() + "\nExiting program.");
      }
      view.dispose(); // kills view and all associated resources (including implied thread)
  } // end manageSimulationEnvironment()

   /** Displays menu in a GUI modal dialog box, and waits for user response. */
   public int menuGUI() throws ExhaustedInputAttempts {
      return InputGUI.getIntGUI("Lord of the Rings -- Battlefield Simulator\n\n" + " 1. Forces of Light: Create New Set of Actors\n" + " 2. Forces of Light: Load from Disk\n"
            + " 3. Forces of Light: Save to Disk\n" + " 4. Forces of Light: Edit One Actor\n" + " 5. Forces of Light: Display\n\n" +

            " 6. Forces of Darkness: Create New Set of Actors\n" + " 7. Forces of Darkness: Load from Disk\n" + " 8. Forces of Darkness: Save to Disk\n" + " 9. Forces of Light: Edit One Actor\n"
            + "10. Forces of Darkness: Display\n\n" +

            "11. Run Simulation\n\n" +

            " 0. Quit", 0, 11);
   } // end void menuGUI()

   /** Implements primary simulation loop where <i>Actor</i> objects will move around on the screen (and eventually do battle). */
   public synchronized void run() {
      while (armyForcesOfLight.getNumActors() > 0 && armyForcesOfDarkness.getNumActors() > 0) {
         // Note: The following algorithm is a simple mechanism of allowing
        // for round-robin movement, that is, move
        // one Actor object in armyForcesOfLight then one Actor object in
        // armyForcesOfDarkness on each iteration.

         for (int i = 0; i < armyForcesOfLight.getNumActors() && armyForcesOfDarkness.getNumActors() != 0; i++) {
            {
               Actor aActorToMove = armyForcesOfLight.getActiveActor();
               Actor aTargetActor = armyForcesOfDarkness.findNearestActor(aActorToMove);
               aActorToMove.move(aTargetActor);
               aActorToMove.combat(aTargetActor);
               armyForcesOfDarkness.removeActor();
               view.ensureLocationIsWithinBounds(aActorToMove);
            }
            armyForcesOfDarkness.removeActor();
         }

         for (int i = 0; i < armyForcesOfDarkness.getNumActors() && armyForcesOfLight.getNumActors() != 0; ++i) {
            {
               Actor aActorToMove = armyForcesOfDarkness.getActiveActor();
               Actor aTargetActor = armyForcesOfLight.findNearestActor(aActorToMove);
               aActorToMove.move(aTargetActor);
               aActorToMove.combat(aTargetActor);
               armyForcesOfLight.removeActor();
               view.ensureLocationIsWithinBounds(aActorToMove);
            }
            armyForcesOfLight.removeActor();
         }
         view.repaint();
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   } // end void runSimulation()

   /**
    * Method called by the JPanel.paintComponent method.
    *
    * @see View
    */

   public void paint(Graphics2D g2) {
      armyForcesOfLight.paint(g2);
      armyForcesOfDarkness.paint(g2);
   } // end void paint()

} // end class Simulator


ActorFactory Source Code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
package actors;

import java.awt.geom.Point2D;

import util.InputGUI;
import util.SingletonRandom;
import util.InputGUI.ExhaustedInputAttempts;

import army.Army;

/**
 * class ActorFactory
 *
 * @author Rex Woollard
 * @see Actor
 * @see Army
 */

public class ActorFactory {
   public static int getNumActorTypes() {
      return 2;
   }

   /**
    * Factory method to build new <i>Actor</i> objects of selected type
    *
    * @param sName
    *
    * @return reference to derived <i>Actor</i> type object
    * @throws ExhaustedInputAttempts
    */

   public static Actor createNewActor(boolean bAutomatic, Point2D p2dLocation, String sName) throws ExhaustedInputAttempts {
      Actor newActor;
      if (bAutomatic)
         newActor = createNewActor(SingletonRandom.getRandomIntEvenDistribution(0, getNumActorTypes() - 1), p2dLocation, sName);
      else
         newActor = createNewActor(InputGUI.getIntGUI("Actor Type (0:Hobbit 1:Wizard): ", 0, getNumActorTypes() - 1), p2dLocation, sName);
      return newActor;
   }

   /**
    * Factory method to build new <i>Actor</i> objects of selected type
    *
    * @param nActorType
    *          identifies the specific derived Actor type to create
    * @param p2dLocation
    *          reference to prebuilt Point2D object with coordinate information
    * @return reference to derived Actor type
    */

   public static Actor createNewActor(int nActorType, Point2D p2dLocation, String sName) {
      switch (nActorType) {
      case 0:
         return new Hobbit(p2dLocation, sName);
      case 1:
         return new Wizard(p2dLocation, sName);
      default:
         return null; // should throw exception (later when you learn how)
     }
   }
}
3  Game Development / Newbie & Debugging Questions / Rare Black Screen && Save/Load Actors Problem on: 2010-04-15 22:05:35
Rare Black Screen: When you create 2 teams and run simulation then add additional actors while the simulation is running, there's a rare glitch where the screen will go black.

Save/Load Actors Problem(In the Army Class): When you create a team then save and load, it will not display the actors on the JPanel even when you create additional actors although when you run simulation they are still fighting just you can't see them.

Screenshot(Save/Load Actors Problem):
http://i41.tinypic.com/2ltq446.jpg

Full Simulator Source Code:
http://www.mediafire.com/file/qkmtngnwgh2/Simulator.zip

Army Source Code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
package army;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import util.InputGUI;
import util.InputGUI.ExhaustedInputAttempts;
import view.View;

import actors.Actor;
import actors.ActorFactory;

/**
 * <i>Army</i> class will manage a collection of <i>Actor</i> objects.
 *
 * @author Rex Woollard
 */

public class Army implements Serializable {
   /**
    *
    */

   private static final long serialVersionUID = 1L;
   /** Reference to the <i>String</i> holding the name of the <i>Army</i> */
   private String sName;
   /** Reference to the <i>Color</i> object; used to identify <i>Actor</i> objects associated with this <i>Army</i> */
   @SuppressWarnings("unused")
   private Color cArmyColor;
   /** Collection to hold references to <i>Actor</i> objects. */
   private List<Actor> lActors;
   private int nNextActorToMove;

   public Army(String sName, Color cArmyColor) {
      this.sName = sName;
      this.cArmyColor = cArmyColor;
      lActors = new ArrayList<Actor>();
      nNextActorToMove = 0;
   } // end Army constructor

   /** Probes the collection to find the current number of <i>Actor</i> objects */
   public int getNumActors() {
      return lActors.size();
   }

   /** Returns a reference to the <i>String</i> object storing the <i>Army</i> name */
   public String getName() {
      return sName;
   }

   /**
    * On each call, adds more <i>Actor</i> objects to collection.
    *
    * @param view
    *          used to set the boundaries on x,y values in Point2D coordinate objects.
    * @throws ExhaustedInputAttempts
    */

   public synchronized void createActors(View view) throws ExhaustedInputAttempts {
      boolean bIsAutomatic = true;
      int nNumActors = InputGUI.getIntGUI(sName + ": Number of Actors to Add:", 1, 10000);

      for (int n = 0; n < nNumActors; ++n)
         lActors.add(ActorFactory.createNewActor(bIsAutomatic, view.getRandomLocation(getName()), getName()));
      view.repaint();
   } // end void createActors()

   /**
    * Displays text-oriented values of all <i>Actor</i> objects in the <i>Army</i> collection.
    *
    * @param psOutputStream
    *          could receive <i>System.out</i> or an explicitly opened file stream on disk.
    */

   public synchronized void display(PrintStream psOutputStream) {
      for (Actor a : lActors)
         psOutputStream.println(a);
   } // end void display()

   /**
    * Method called by the <i>paintComponent</i> method in <i>View.Panel</i>.
    *
    * @param g2
    *          reference to Graphics2D object that contains device context information
    * @return
    */

   public synchronized void paint(Graphics2D g2) {
   Graphics g = (Graphics) g2;
       for (Actor aCurrentActorToDisplay : lActors) {
         aCurrentActorToDisplay.getDisplayActor(g);
       }
   } // end void paint()

   /** Allows user to edit the values in a single <i>Actor</i> object, using an <i>int</i> value as the selection index */
   public synchronized void editOneActor() {
      try {
         lActors.get(InputGUI.getIntGUI("Edit Actor", 0, lActors.size() - 1)).setGUI();
         // Preceding line could be expanded to the following three lines. They do the same thing.
        // int nIndex = get(InputGUI.getIntGUI("Edit Actor", 0, lActors.size()-1));
        // Actor aActorToEdit = lActors.get(nIndex);
        // aActorToEdit.setGUI();
     } catch (InputGUI.ExhaustedInputAttempts e) {
         InputGUI.showErrorGUI(e.getMessage());
      }
   } // end editOneActor()

   public synchronized Actor getActiveActor() {
      if (nNextActorToMove >= lActors.size())
         nNextActorToMove = 0;

      return lActors.get(nNextActorToMove++);
   }

   public synchronized Actor findNearestActor(Actor aActorToMove) {
      int nTargetActor = 0;

      for (Actor a : lActors) {
         if (aActorToMove.getLocation().distance(a.getLocation()) < aActorToMove.getLocation().distance(lActors.get(nTargetActor).getLocation())) {
            nTargetActor = lActors.indexOf(a);
         }
      }
      return lActors.get(nTargetActor);
   }

   public synchronized void saveToDisk() {
      try {
         ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(getName() + ".ser"));

         // Magic starts here ******************************************************************
        // Entire Student object is written to disk; all the related structural information is embedded in file
        out.writeObject(this);
         // Magic ends here ********************************************************************

         out.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public synchronized Army loadFromDisk() {
      Army army = null;
      // Using a try block in case there is a file I/O error
     try {
         ObjectInputStream in = new ObjectInputStream(new FileInputStream(getName() + ".ser"));

         // Magic starts here ******************************************************************
        // Student object is read from disk with all the structural information restored from file
        army = (Army) in.readObject();
         // Magic ends here ********************************************************************

         in.close();
      } catch (Exception e) {
         e.printStackTrace();
      }

      return army; // end case 2
  }

   public synchronized void removeActor() {
      for (int i = lActors.size() - 1; i >= 0; i--)
       if (lActors.get(i).getHealth() <= 0) {
          lActors.remove(i);
         }
       }
}// end class Army


View Source Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import objects.Tree;

import actors.Actor;


import simulator.Simulator;

/**
 * <i>View</i> is a type of <i>JFrame</i>. It manages the application window. A <i>JPanel</i> is placed inside. The <i>JPanel</i> is the canvas on which <i>Actor</i> objects are drawn.
 *
 * @author Rex Woollard
 *
 */

@SuppressWarnings("serial")
public class View extends JFrame {
   ImageIcon background = new ImageIcon("Images/background.png");
   private Simulator simulator;

   public View(int nWidth, int nHeight) {
      setSize(nWidth, nHeight);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("Lord of the Rings: Battelfield Simulator");
      this.getContentPane().setBackground(Color.BLACK);
      add(new Panel());
      setVisible(true);
   } // end View constructor

   public void setSimulator(Simulator s) {
      simulator = s;
   }

   public Point2D getRandomLocation(String name) {
      double x = Math.random() * getWidth();
      double y = Math.random() * getHeight();
     
      if(y < 325)
         y =+ 325;
     
      if(name == "Red" && x > getWidth()/2)
         x =- getWidth()/2;
     
      if(name == "Blue" && x < getWidth()/2)
         x =+ getWidth()/2;
         
     
     
     
      return new Point2D.Double(x,y);
   }

   public void ensureLocationIsWithinBounds(Actor aActorToMove) {
      double dX = aActorToMove.getLocation().getX();
      double dY = aActorToMove.getLocation().getY();
      if (dX > getWidth() - 86)
         dX = aActorToMove.getLocation().getX() - 2.0;
      if (dX < 0.0)
         dX = 0.0;
      if (dY > getHeight() - 132)
         dY = aActorToMove.getLocation().getY() - 2.0;
      if (dY < 325.0)
         dY = 325.0;
      aActorToMove.spriteDirection();
      aActorToMove.getLocation().setLocation(dX, dY);
   }

   /** <i>Panel</i> is a type of <i>JPanel</i>. It is an inner class, so automatically has access to the parent class (<i>View</i>) instance variables, in particular <i>simulator</i>. */
   public class Panel extends JPanel {
      /**
       * void paintComponent: CALLBACK method is invoked by Operating System during PAINT processing
       * @param g
       * Graphics: contains device context information
       */

      public synchronized void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawImage(background.getImage(), 0, 0, null);
         Graphics2D g2 = (Graphics2D) g;
         if (simulator != null)
            simulator.paint(g2);
      }
   }
}
4  Game Development / Newbie & Debugging Questions / Re: java.util.ConcurrentModificationException on: 2010-04-15 21:53:04
Thanks guys!, I fixxed it Smiley
5  Game Development / Newbie & Debugging Questions / Re: java.util.ConcurrentModificationException on: 2010-04-15 06:10:45
Yeah, it didn't work. When you create an army it doesn't display it on the JFrame. Although when I used a toString() method, I found out the Actor object do exist. What should I look for to fix this problem?

Army Class
Quote
package army;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import util.InputGUI;
import util.InputGUI.ExhaustedInputAttempts;
import view.View;

import actors.Actor;
import actors.ActorFactory;

/**
 * <i>Army</i> class will manage a collection of <i>Actor</i> objects.
 *
 * @author Rex Woollard
 */
public class Army implements Serializable {
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   /** Reference to the <i>String</i> holding the name of the <i>Army</i> */
   private String sName;
   /** Reference to the <i>Color</i> object; used to identify <i>Actor</i> objects associated with this <i>Army</i> */
   @SuppressWarnings("unused")
   private Color cArmyColor;
   /** Collection to hold references to <i>Actor</i> objects. */
   private List<Actor> lActors;
   private int nNextActorToMove;

   public Army(String sName, Color cArmyColor) {
      this.sName = sName;
      this.cArmyColor = cArmyColor;
      lActors = new ArrayList<Actor>();
      nNextActorToMove = 0;
   } // end Army constructor

   /** Probes the collection to find the current number of <i>Actor</i> objects */
   public int getNumActors() {
      return lActors.size();
   }

   /** Returns a reference to the <i>String</i> object storing the <i>Army</i> name */
   public String getName() {
      return sName;
   }

   /**
    * On each call, adds more <i>Actor</i> objects to collection.
    *
    * @param view
    *          used to set the boundaries on x,y values in Point2D coordinate objects.
    * @throws ExhaustedInputAttempts
    */
   public void createActors(View view) throws ExhaustedInputAttempts {
      boolean bIsAutomatic = true;
      int nNumActors = InputGUI.getIntGUI(sName + ": Number of Actors to Add:", 1, 10000);

      for (int n = 0; n < nNumActors; ++n)
         lActors.add(ActorFactory.createNewActor(bIsAutomatic, view.getRandomLocation(getName()), getName()));
      view.repaint();
   } // end void createActors()

   /**
    * Displays text-oriented values of all <i>Actor</i> objects in the <i>Army</i> collection.
    *
    * @param psOutputStream
    *          could receive <i>System.out</i> or an explicitly opened file stream on disk.
    */
   public void display(PrintStream psOutputStream) {
      for (Actor a : lActors)
         psOutputStream.println(a);
   } // end void display()

   /**
    * Method called by the <i>paintComponent</i> method in <i>View.Panel</i>.
    *
    * @param g2
    *          reference to Graphics2D object that contains device context information
    * @return
    */
   public void paint(Graphics2D g2) {
   //This line is completely unnecessary
   Graphics g = (Graphics) g2;
     synchronized(lActors) {
       for (Actor aCurrentActorToDisplay : lActors) {
         aCurrentActorToDisplay.getDisplayActor(g);
       }
      }
   } // end void paint()

   /** Allows user to edit the values in a single <i>Actor</i> object, using an <i>int</i> value as the selection index */
   public void editOneActor() {
      try {
         lActors.get(InputGUI.getIntGUI("Edit Actor", 0, lActors.size() - 1)).setGUI();
         // Preceding line could be expanded to the following three lines. They do the same thing.
         // int nIndex = get(InputGUI.getIntGUI("Edit Actor", 0, lActors.size()-1));
         // Actor aActorToEdit = lActors.get(nIndex);
         // aActorToEdit.setGUI();
      } catch (InputGUI.ExhaustedInputAttempts e) {
         InputGUI.showErrorGUI(e.getMessage());
      }
   } // end editOneActor()

   public Actor getActiveActor() {
      if (nNextActorToMove >= lActors.size())
         nNextActorToMove = 0;

      return lActors.get(nNextActorToMove++);
   }

   public Actor findNearestActor(Actor aActorToMove) {
      int nTargetActor = 0;

      for (Actor a : lActors) {
         if (aActorToMove.getLocation().distance(a.getLocation()) < aActorToMove.getLocation().distance(lActors.get(nTargetActor).getLocation())) {
            nTargetActor = lActors.indexOf(a);
         }
      }
      return lActors.get(nTargetActor);
   }

   public void saveToDisk() {
      try {
         ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(getName() + ".ser"));

         // Magic starts here ******************************************************************
         // Entire Student object is written to disk; all the related structural information is embedded in file
         out.writeObject(this);
         // Magic ends here ********************************************************************

         out.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public Army loadFromDisk() {
      Army army = null;
      // Using a try block in case there is a file I/O error
      try {
         ObjectInputStream in = new ObjectInputStream(new FileInputStream(getName() + ".ser"));

         // Magic starts here ******************************************************************
         // Student object is read from disk with all the structural information restored from file
         army = (Army) in.readObject();
         // Magic ends here ********************************************************************

         in.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return army; // end case 2
   }

   public void removeActor() {
     synchronized(lActors) {
       for (int i = lActors.size() - 1; i > 0; i--)
         if (lActors.get(i).getHealth() <= 0) {
           lActors.remove(i);
         }
       }
     }

}// end class Army


Quote
package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import objects.Tree;

import actors.Actor;


import simulator.Simulator;

/**
 * <i>View</i> is a type of <i>JFrame</i>. It manages the application window. A <i>JPanel</i> is placed inside. The <i>JPanel</i> is the canvas on which <i>Actor</i> objects are drawn.
 *
 * @author Rex Woollard
 *
 */
@SuppressWarnings("serial")
public class View extends JFrame {
   // TODO Access specifiers . .  .
   ImageIcon background = new ImageIcon("Images/background.png");
   private Simulator simulator;
   Tree tree = null;

   public View(int nWidth, int nHeight) {
      setSize(nWidth, nHeight);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("Lord of the Rings: Battelfield Simulator");
      this.getContentPane().setBackground(Color.BLACK);
      add(new Panel());
      setVisible(true);
   } // end View constructor

   public void setSimulator(Simulator s) {
      simulator = s;
   }

   public Point2D getRandomLocation(String name) {
      double x = Math.random() * getWidth();
      double y = Math.random() * getHeight();
      
      if(y < 325)
         y =+ 325;
      
      if(name == "Red" && x > getWidth()/2)
         x =- getWidth()/2;
      
      if(name == "Blue" && x < getWidth()/2)
         x =+ getWidth()/2;
         
      
      
      
      return new Point2D.Double(x,y);
   }

   public void ensureLocationIsWithinBounds(Actor aActorToMove) {
      double dX = aActorToMove.getLocation().getX();
      double dY = aActorToMove.getLocation().getY();
      if (dX > getWidth() - 86)
         dX = aActorToMove.getLocation().getX() - 2.0;
      if (dX < 0.0)
         dX = 0.0;
      if (dY > getHeight() - 132)
         dY = aActorToMove.getLocation().getY() - 2.0;
      if (dY < 325.0)
         dY = 325.0;
      aActorToMove.spriteDirection();
      aActorToMove.getLocation().setLocation(dX, dY);
   }

   /** <i>Panel</i> is a type of <i>JPanel</i>. It is an inner class, so automatically has access to the parent class (<i>View</i>) instance variables, in particular <i>simulator</i>. */
   public class Panel extends JPanel {
      /**
       * void paintComponent: CALLBACK method is invoked by Operating System during PAINT processing
       * @param g
       * Graphics: contains device context information
       */
      public void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawImage(background.getImage(), 0, 0, null);
         Graphics2D g2 = (Graphics2D) g;
      }
   }
}
6  Game Development / Newbie & Debugging Questions / Re: java.util.ConcurrentModificationException on: 2010-04-15 04:38:30
Ahhh, I tried this and my Actors disappeared

public synchronized void paint(Graphics2D g2) {
      Graphics g = (Graphics) g2;
      for (Actor aCurrentActorToDisplay : lActors) {
         aCurrentActorToDisplay.getDisplayActor(g);
}
}

7  Game Development / Newbie & Debugging Questions / Re: java.util.ConcurrentModificationException on: 2010-04-15 04:26:47
"Synchronize on your IActors Collection or switch to active rendering"

Hmm, how do you synchronize or switch to active rendering?
(Sorry, I'm just learning about threads now, and never heard about those 2 options)
8  Game Development / Newbie & Debugging Questions / Solved on: 2010-04-15 04:15:37
Solved
Thanks everyone Smiley 
9  Game Development / Artificial Intelligence / AI Sprite Change on: 2010-04-04 05:14:44
Screenshot
http://i44.tinypic.com/n5ih6u.jpg

It's just a simulation for a school project where there are 2 teams. Basicly what happens is that an "Actor" finds the closest "Actor" to him and starts going to that location or runs away, depending on what race they are. Thing is, I'm not really sure how to change the sprite image based on there direction. Any ideas?

P.S, There will be an up, down, left, and right image



   public void move (Actor aTargetActor)
   {
      double xDirection, yDirection, radian;
      double dX, dY = 0;
      
      if(!actorCollision(this, aTargetActor)){
       xDirection = aTargetActor.p2dLocation.getX() - this.p2dLocation.getX();
       yDirection = aTargetActor.p2dLocation.getY() - this.p2dLocation.getY();       
       
       radian = Math.atan2(yDirection, xDirection);
              
      dY = (this.p2dLocation.getY()-(Math.sin(radian)/100)*this.getSpeed());
       dX = (this.p2dLocation.getX()-(Math.cos(radian)/100)*this.getSpeed());

      this.p2dLocation.setLocation(dX, dY);
       }
   }
   
   
   public boolean actorCollision(Actor a, Actor b)
   {
   double xD, yD;

   xD = Math.abs(a.getLocation().getX() - b.getLocation().getX());
   yD = Math.abs(a.getLocation().getY() - b.getLocation().getY());

   if (xD > 20.0)
   {
      if (yD > 20.0)
      return true;
   }
   return false;
   }
}








Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (24 views)
2014-04-15 03:46:01

UprightPath (39 views)
2014-04-14 17:39:50

UprightPath (21 views)
2014-04-14 17:35:47

Porlus (37 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (120 views)
2014-04-08 23:06:04

tom_mai78101 (220 views)
2014-04-05 13:34:39

trollwarrior1 (187 views)
2014-04-04 12:06:45

CJLetsGame (194 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!