8 Replies - 3743 Views - Last Post: 30 April 2013 - 07:54 PM Rate Topic: -----

#1 StoicStein  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-March 13

Question: How to close a window with an Actor

Posted 31 March 2013 - 05:53 PM

My goal is to try and close the original window with MaskMan then open Termina and replace MaskMan with Ben
Termina is an ActorWorld in which only Ben can use his act() in.
Ben just extends the Actor class.

Here is MaskMan:
import info.gridworld.grid.*;
import info.gridworld.actor.*;
import java.util.ArrayList;
public class MaskMan extends Actor 
{
    Termina termina = new Termina();
    public MaskMan()
    {
        setColor(null);
    }
    public void act()
    {
        ArrayList<Actor> actors = new ArrayList<Actor>();
        Location meHere = getLocation();
        for(Location loc:getGrid().getOccupiedLocations())
            actors.add(getGrid().get(loc));
        actors.remove(this);
        for(Actor actor:actors)
        {
            Location loc = actor.getLocation();
            actor.removeSelfFromGrid();
            termina.add(loc, actor);
        }
        termina.add(meHere, new Ben());
        termina.show();
        //System.exit(0);
    }
}



System.exit(0) just resets the JVM thus getting rid of both windows :/

Is This A Good Question/Topic? 0
  • +

Replies To: Question: How to close a window with an Actor

#2 carterf  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 28-April 13

Re: Question: How to close a window with an Actor

Posted 28 April 2013 - 07:10 PM

View PostStoicStein, on 31 March 2013 - 05:53 PM, said:

My goal is to try and close the original window with MaskMan then open Termina and replace MaskMan with Ben
Termina is an ActorWorld in which only Ben can use his act() in.
Ben just extends the Actor class.

Here is MaskMan:
import info.gridworld.grid.*;
import info.gridworld.actor.*;
import java.util.ArrayList;
public class MaskMan extends Actor 
{
    Termina termina = new Termina();
    public MaskMan()
    {
        setColor(null);
    }
    public void act()
    {
        ArrayList<Actor> actors = new ArrayList<Actor>();
        Location meHere = getLocation();
        for(Location loc:getGrid().getOccupiedLocations())
            actors.add(getGrid().get(loc));
        actors.remove(this);
        for(Actor actor:actors)
        {
            Location loc = actor.getLocation();
            actor.removeSelfFromGrid();
            termina.add(loc, actor);
        }
        termina.add(meHere, new Ben());
        termina.show();
        //System.exit(0);
    }
}



System.exit(0) just resets the JVM thus getting rid of both windows ://>


Use reflection to do this, I'll look into it later if you haven't figured it out.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Question: How to close a window with an Actor

Posted 28 April 2013 - 07:14 PM

Try and avoid Reflection. It's computationally expensive.
Was This Post Helpful? 0
  • +
  • -

#4 carterf  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 28-April 13

Re: Question: How to close a window with an Actor

Posted 29 April 2013 - 04:00 PM

View Postmacosxnerd101, on 28 April 2013 - 07:14 PM, said:

Try and avoid Reflection. It's computationally expensive.

True, but I'm certain that access to any frame used in grid world will be limited to the private scope, and he is asking how to access it from a critter. Not sure how else to go about doing it without reflection.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Question: How to close a window with an Actor

Posted 29 April 2013 - 04:05 PM

This is probably the sign of a poor design then. It sounds like a quick fix rather than doing things the right way.

Though to be fair, if I were trying to really do a lot of GUI manipulation, I would probably move on from GridWorld and onto Swing or a third party GUI library.
Was This Post Helpful? 0
  • +
  • -

#6 carterf  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 28-April 13

Re: Question: How to close a window with an Actor

Posted 29 April 2013 - 04:13 PM

View Postmacosxnerd101, on 29 April 2013 - 04:05 PM, said:

This is probably the sign of a poor design then. It sounds like a quick fix rather than doing things the right way.

Though to be fair, if I were trying to really do a lot of GUI manipulation, I would probably move on from GridWorld and onto Swing or a third party GUI library.


Yeah, and I think this is proof of the flawed educational value of the GridWorld case study. Students are asked to program in the bubble of GridWorld for several months, and they naturally begin to forget about what occurs outside of the bubble. Slowly but surely, we slip away from the concepts underlying computer science, exchanging complicated truths for the lull of GridWorld's linear, object oriented, perfection.

OT:
If you can explain a little bit more about what your main project is, I would be happy to try to point you in the right direction.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Question: How to close a window with an Actor

Posted 29 April 2013 - 04:17 PM

Programming in GridWorld for 7 months?! That sounds painful. I'm glad we only spent a few weeks on it when I took this class. Though I would hardly call GridWorld perfect, by any stretch of the imagination. :)
Was This Post Helpful? 0
  • +
  • -

#8 StoicStein  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-March 13

Re: Question: How to close a window with an Actor

Posted 30 April 2013 - 06:43 PM

Quote

If you can explain a little bit more about what your main project is, I would be happy to try to point you in the right direction.


This was supposed to close gridworld and open a new one with my version of the actor world.
close GUI.ActorWorld and then open GUI.Termina

If there is a way to override the GUIs ActorWorld, that would be preferred.

I can only change things through an actor put onto the grid.

The Runner class will look like:
import info.gridworld.actor.ActorWorld;
class Runner
{
    public static void main(String[]args)
    {
        ActorWorld world = new ActorWorld();
        world.add(new playerMadeMonster());
        world.add(new playerMadeMonster());
        world.add(new playerMadeMonster());
        world.add(new playerMadeMonster());
        world.show();
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 carterf  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 28-April 13

Re: Question: How to close a window with an Actor

Posted 30 April 2013 - 07:54 PM

View PostStoicStein, on 30 April 2013 - 06:43 PM, said:

Quote

If you can explain a little bit more about what your main project is, I would be happy to try to point you in the right direction.


This was supposed to close gridworld and open a new one with my version of the actor world.
close GUI.ActorWorld and then open GUI.Termina

If there is a way to override the GUIs ActorWorld, that would be preferred.

I can only change things through an actor put onto the grid.

The Runner class will look like:
import info.gridworld.actor.ActorWorld;
class Runner
{
    public static void main(String[]args)
    {
        ActorWorld world = new ActorWorld();
        world.add(new playerMadeMonster());
        world.add(new playerMadeMonster());
        world.add(new playerMadeMonster());
        world.add(new playerMadeMonster());
        world.show();
    }
}




Ok, ok, please don't submit this to any code safety competitions, here you go:
final info.gridworld.world.World fWorld = world; //Only necessary because we are using an inner class for our thread. 
new Thread(new Runnable(){
	public void run()
	{
		try
		{
			Thread.sleep(3000);
			java.lang.reflect.Field tmpField = info.gridworld.world.World.class.getDeclaredField("frame");
			tmpField.setAccessible(true);
			WorldFrame worldf = (info.gridworld.gui.WorldFrame)tmpField.get(dum);
			((javax.swing.JFrame)worldf).dispatchEvent(new java.awt.event.WindowEvent(worldf, java.awt.event.WindowEvent.WINDOW_CLOSING));
		}catch(Exception e){
			System.out.println("Something Went Wrong: "+e);
		}
	}
}).start();


This starts up a new thread, waits 3000 milliseconds, and then closes the window.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1