1 / 14

Extending GridWorld

Extending GridWorld. Topics. Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box Launching a display dialog box Displaying messages Removing grid lines STEP button

alaula
Download Presentation

Extending GridWorld

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Extending GridWorld

  2. Topics • Using a different image than normal • Multiple images for a class • Controlling the mouse click • Controlling the keys pressed • Launching an input dialog box • Launching a display dialog box • Displaying messages • Removing grid lines • STEP button • RUN button

  3. Extending Actors And Using a Different Image The first and easiest option to extend Actor with a different image is to do the following: • Extend Actor and override the act method • Create a .GIF image file with the same name as the class. • Store the .GIF file in the same directory as the class file Note: You don’t have to extend Actor for this to work. It works for any grid occupant.

  4. Extending Actors And Using a Different Image However, if you have a grid of Actors and you want to occupy some of the cells with something other than Actor images, you can follow this process: • Create an empty class that is named ClassnameDisplay and extends DefaultDisplay. In other words, if you are creating an object class named PlayActor_JohnDoe, then create an empty PlayActor_JohnDoeDisplay class as follows: • import info.gridworld.gui.DefaultDisplay; • public class PlayActor_JohnDoeDisplay extends DefaultDisplay • { • //Since nothing is coded here, it disregards the Actor.gif • //Without this class the gui will use the closest • //related gif file - Actor.gif • }

  5. Extending Actors And Using a Different Image • The default is that the image is shown as text in a colored cell. However, if this is not what you want, then you can override the getText() method in your class and display what you want. The following example shows how you could output a number: • public String getText() • { • return ""+ number; • } • Optionally, instead of defining the getText() method, you can define the toString() method and that will be used to display the image: Note: in either case the first 8 characters of the string are displayed in the cell • public String toString() • { • return ""+ name; • }

  6. Extending Actors And Using a Different Image • If a getColor() method is defined in your class or the superclass, it will be used to determine how to fill the background color in the cell. Therefore if you extend Actor, you can use setColor() to set the color and this will be the background color. • If you have a textColor instance variable of type Color defined in your class, its value will be used to determine the color of the text. You can create an accessor method to retrieve the textColor. Note: if the background color and the text color are the same, then the text color will automatically be the reverse of the background color.

  7. Creating Multiple Images for a Single Type of Grid Occupant You can have multiple images for a grid occupant. Suppose you have 52 card images, and all of these are Card objects. You could simply name each of the GIF files with the name of the class followed by a suffix. Then you would override the getImageSuffix method in the Card class. Here are the instructions: • Create a GIF file of each card image • Name the files: Card_AceOfHearts.gif, Card_TwoOfClubs.gif • Save the files in the same Java package as the class file • You must have a file named Card.gif even if you don’t use it • Override the getImageSuffix method in the Card class and GridWorld will use this to find the image file with this suffix: public String getImageSuffix() { return "_" + cardSuffix; // cardSuffix is instance variable containing suffix }

  8. Creating a World Using a Bounded Grid BoundedGrid<Integer> grid; grid = new BoundedGrid<Integer>(5,5); grid.put(new Location(2,2),4); grid.put(new Location(1,1),9); grid.put(new Location(0,4),11); grid.put(new Location(4,3),5); grid.put(new Location(2,0),1); World<Integer> world; world = new World<Integer>(grid); world.show();

  9. Creating World with a Black Background This is one way to make grid lines disappear. When the background and the grid lines are both black, you cannot see the grid lines. Check out the lecture “Modifying GridWorld Classes” for another way of making grid lines disappear. Code in driver program: BoundedGrid grid = new BoundedGrid(4,7); //4 rows and 7 columns for(int r=0; r<grid.getNumRows(); r++) for (int c=0; c<grid.getNumCols(); c++) grid.put(new Location(r,c), new Background(Color.BLACK)); CardWorld world = new CardWorld(grid); world.show(); publicclass Background extends Actor { public Background(Color color) { setColor(color); } public String getText() { return ""; } } publicclass BackgroundDisplay extends DefaultDisplay { }

  10. locationClicked import info.gridworld.world.World; import info.gridworld.grid.Location; public class NumberWorld extends World { public boolean locationClicked(Location loc) { add(loc, new RowColMultiplyBug(loc.getCol()*loc.getRow())); System.out.println("Location "+loc+" clicked"); return true; } }

  11. keyPressed publicboolean keyPressed(String description, Location loc) { if (description.equals("SPACE")) //when user presses spacebar, perform swapCards(loc) { String yesNo = JOptionPane.showInputDialog("Do you want to swap cards (Y/N)?"); if (yesNo.equals("Y")) { swapCards(loc); //method to swap cardsreturn true; } returnfalse; } else if (description.equals("H") || description.equals("shift H")) //when user presses h or H { JOptionPane.showMessageDialog(null, "This is not really a game. \n" + "Click on the cards you want to swap."); returntrue; } else { returnfalse; } } Helpful Tip: To determine the description of a key, simply put a print statement in this keyPressed method to print description to the console; then press the key

  12. step Here is how you could simulate clicking on all objects in the world when the STEP button is pressed: publicvoid step() { ArrayList<Location> occupiedCells = getGrid().getOccupiedLocations(); for (int i=1; i<occupiedCells.size(); i++) { locationClicked(occupiedCells.get(i)); } setMessage("Step is pressed"); }

More Related