14

Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

Embed Size (px)

Citation preview

Page 1: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box
Page 2: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

• 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

Page 3: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

The first and easiest option to extend Actor with a different image is to do the following:

1. Extend Actor and override the act method

2. Create a .GIF image file with the same name as the class.

3. 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.

Page 4: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

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:

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}

1. 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:

Page 5: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

public String getText(){ return ""+ number;}

2. 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:

3. Optionally, instead of defining the getText() method, you can define the toString() method and that will be used to display the image:

public String toString(){ return ""+ name;}

Note: in either case the first 8 characters of the string are displayed in the cell

Page 6: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

4. 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.

5. 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.

Page 7: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

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:1. Create a GIF file of each card image2. Name the files: Card_AceOfHearts.gif,

Card_TwoOfClubs.gif3. Save the files in the same Java package as the class

file4. You must have a file named Card.gif even if you don’t

use it5. 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}

Page 8: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

World classMethods commonly used in games

Name Usepublic boolean locationClicked (Location loc)

Activated by a mouse click in the grid. If overriding this method you should always return true. This method allows you to control what happens when clicking a cell.

public boolean keyPressed (String description, Location loc)

Activated by pressing a key. The description is the name of the key (i.e. SPACE, shift H, UP, DOWN, etc.). Note: Normally you would not want to override the arrow keys. If overriding this method you should always return true.

Page 9: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

World classMethods commonly used in games

Name Usepublic void setMessage (String newMessage)

The yellow area above the grid is the message display. You can use the setMessage method to determine what is displayed here. i.e. setMessage(“Location “ + loc + “ was clicked”);

public void step() Override this method to determine what is done when the STEP button is pressed.

public void run() Override this method to determine what is done when the RUN button is pressed. By default it will perform the step method continuously.

Page 10: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

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

Page 11: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

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 columnsfor(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();

public class Background extends Actor{ public Background(Color color) { setColor(color); } public String getText() { return ""; }}

public class BackgroundDisplay extends DefaultDisplay { }

Page 12: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

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

Page 13: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

public boolean 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 cards return true; } return false; } 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."); return true; } else { return false; }}

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

Page 14: Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box

Here is how you could simulate clicking on all objects in the world when the STEP button is pressed:

public void step(){ ArrayList<Location> occupiedCells = getGrid().getOccupiedLocations(); for (int i=1; i<occupiedCells.size(); i++) { locationClicked(occupiedCells.get(i)); } setMessage("Step is pressed");}