This site requires JavaScript, please enable it in your browser!
Greenfoot back
jswlhw
jswlhw wrote ...

2012/7/2

Several Questions - New to Greenfoot and Java

jswlhw jswlhw

2012/7/2

#
Working thru tutorial http://shallwelearn.com/joomla/index.php?option=com_content&view=article&id=131:create-a-tic-tac-toe-game-board-lesson-2-java-games-with-greenfoot-&catid=2:java-greenfoot. 1. When I code for a cell bigger than 60, even though I only want 3, it adds more cells - 5 when i use 100. WHY? What I want is a large TIC TAC TOE area. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Tic Tac Toe Board. * * @author (John) * @version (07/01/2012) */ public class Board extends World { /** * Constructor for objects of class Board. * */ public Board() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(3, 3, 100); setBackground ("cell.jpg"); 2. How do you move the grid to start in upper right hand corner or on left edge instead of centered? 3. I can make the ide larger = top, bottom, left and right resizable but cannot make the class area larger. Is the "World", "Class" and "execution" area fixed.
danpost danpost

2012/7/2

#
You need to scale the size of the image used. Try this:
super(3, 3, 100);
GreenfootImage cell = new GreenfootImage("cell.jpg");
cell.scale(100, 100);
setBackground(cell);
The size of the cell does not determine the size of the image used in the background. The world will always take the image set as the background and paint it at its current size at (0, 0). If the image does not cover the width and/or height of the world, it will paint more at (m * imageWidth, n * imageHeight), measured in pixels (not cells), where m and n are integers. You asked if the World, Class and execution area were fixed: once a world is created, the width, height, and cell size are fixed. The only way to change them is to create a new world with a different 'super' world constructor call (that is, with different parameters) and set that world as the current world. My 'Total Infestation' scenario does just that. It calls the constructor of the Grid world with a parameter to indicate the level number. Something like this:
Greenfoot.setWorld(new Grid(levelNum);
and the Grid constructor starts like this:
public Grid(int level)
{
    super(120 + 60 * level, 80 +  40 * level, 1);
I am not sure I understand what you mean by making the Class area larger. The actors 'live' in the world, and can be of whatever size you make them (as long as they have size). The execution area can only be changed in the same way as changing (setting) a new world. There is a world constructor that excepts a fourth field of boolean type. It determines whether the world is bounded or not. If not bounded, the actors can move right out of the window and then back again if they move back into view. My '3D Motion Demo' is an example of an unbounded world.
jswlhw jswlhw

2012/7/3

#
Thanks for the super fast answer. 1. Your answer to this was right on. Have tried and works perfectly. 2. More explanation - I want to keep the world view but move only the grid to starting in upper left. Tried to upload image to illustrate but could not figure out how since I did not have a URL. 3. More explanation - When bulding/coding something is it possible to make the "class" or right hand side of the IDE larger? Once again thanks for your quick response.
danpost danpost

2012/7/3

#
You should be able to 'drawImage' your image to the background of the world using offsets to determine what actually shows. You will need to keep track of the x and y offsets for the image drawing (int imgX = 0, imgY = 0;). For example, let us say your world is 600 wide by 400 high and the image you are using is 1200 x 800. Your actor would start at (300, 200), the center of the viewing area when (0, 0) is the top-left corner of your image. Program your actor normally, as if it was to be moving. But, now, in the world, check the actors location. If not at (300, 200), get the x and y offsets (int dx = actor.getX() - 300; int dy = actor.getY() - 200;) and move everything in the world back by those offset amounts (for (Object obj : getObjects(null)) { Actor actor = (Actor) obj; actor.setLocation(actor.getX() - dx, actor.getY() - dy); } and then adjust the background with getBackground().drawImage(image, imgX - dx, imgY - dy);. You will have to perform checks to see if imgX - dx or imgY - dy are outside the world bounds before moving anything back or adjusting the background. As far as making the 'class' area larger: you will not be able to increase the font size or box size, but by dragging the edges of your greenfoot frame, you should be able to expand the area that those objects are in. BTW, I had, at one time, need to display an image here. I was referred to http://photobucket.com. It is a free image hosting site. Just copy/paste the URL from the site to the 'image' dialog when you press the tag below the 'Post a reply' input box.
You need to login to post a reply.