Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Solved  (Read 1430 times)
0 Members and 1 Guest are viewing this topic.
Offline Zaroth

Senior Newbie





« Posted 2010-04-15 04:15:37 »

Solved
Thanks everyone Smiley 
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2010-04-15 04:20:25 »

Since you are not using active rendering, your paint method could be called any time by the event dispatch thread.  So in the middle of your delete loop your paint method could start up.  You have 2 alternatives.  Synchronize on your IActors Collection or switch to active rendering.

Offline Zaroth

Senior Newbie





« Reply #2 - Posted 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)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zaroth

Senior Newbie





« Reply #3 - Posted 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);
}
}

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #4 - Posted 2010-04-15 05:34:56 »

"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)

If you use active rendering then you don't have to deal with threads because you do everything in the main thread.

With passive rendering you put your render code in the paint method and it gets called by the event dispatch thread which can interrupt the main thread at any time.

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #5 - Posted 2010-04-15 05:38:19 »

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



Are you saying it worked or it didn't?

This is what I was suggesting.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void removeActor() {
  synchronized(lActors) {
    for (int i = lActors.size() - 1; i > 0; i--)
      if (lActors.get(i).getHealth() <= 0) {
        lActors.remove(i);
      }
    }
  }
}


public void paint(Graphics2D g2) {
//This line is completely unnecessary
//Graphics g = (Graphics) g2;
 synchronized(lActors) {
    for (Actor aCurrentActorToDisplay : lActors) {
      aCurrentActorToDisplay.getDisplayActor(g);
    }
  }
}

Offline Zaroth

Senior Newbie





« Reply #6 - Posted 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;
      }
   }
}
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #7 - Posted 2010-04-15 09:27:31 »

I didn't check the whole source code, but this
1  
for (int i = lActors.size() - 1; i > 0; i--)

should be
1  
for (int i = lActors.size() - 1; i >= 0; i--)

lots of sillystupid games: http://www.emaggame.com
Offline Noobtastic

Senior Newbie





« Reply #8 - Posted 2010-04-15 19:28:03 »

Perhaps I am missing the point...but...

It looks to me like it is not a threading issue at all.
It looks to me like he is removing an element from the data structure he is using in his for loop definition.  That will cause concurrent mod exceptions for sure.

Change your loop to build a stack of actors to remove when you go through your loop.
Then, afterward, remove the stack from your first data structure.

Offline Zaroth

Senior Newbie





« Reply #9 - Posted 2010-04-15 21:53:04 »

Thanks guys!, I fixxed it Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wildern

Junior Member





« Reply #10 - Posted 2010-04-16 02:06:29 »

Perhaps I am missing the point...but...

It looks to me like it is not a threading issue at all.
It looks to me like he is removing an element from the data structure he is using in his for loop definition.  That will cause concurrent mod exceptions for sure.

Change your loop to build a stack of actors to remove when you go through your loop.
Then, afterward, remove the stack from your first data structure.



I agree, and suggest using an Iterator as it can safely remove an element while traversing the list.
1  
2  
3  
4  
5  
6  
for (Iterator<Actor> iterator = lActors.iterator(); iterator.hasNext();) {
    Actor a = iterator.next();
    if (a.getHealth() <= 0) {
        iterator.remove();
    }
}
Offline Swattkidd7

Junior Member





« Reply #11 - Posted 2010-04-17 08:55:37 »

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...
Offline Zaroth

Senior Newbie





« Reply #12 - Posted 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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (46 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (32 views)
2014-07-23 20:56:16
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!