Basically, what I'm asking is, how can I multiply an integer (that may change between use) and a set number to create the size of the world? I'm sure this is possible, as I've seen it done somewhere before.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** base world used to get user data defining the new world */ public MyWorld() { super ( 1 , 1 , 1 ); GreenfootImage bg = null ; int boardNum = 0 ; for (;;) { // etc. (getting 'image') if (bg == null ) bg = image; else { GreenfootImage old = new GreenfootImage(bg); bg = new GreenfootImage(old.getWidth()+image.getWidth(), old.getHeight()); bg.drawImage(old, 0 , 0 ); bg.drawImage(image, old.getWidth(), 0 ); } boardNum++; } Greenfoot.setWorld( new MyWorld(bg)); } /** construct a new user-defined world */ public MyWorld(GreenfootImage bg) { super (bg.getWidth(), bg.getHeight(), 1 ); setBackground(bg); // etc. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class HorizontalBackground extends World { private String boardPositions; /** * */ public HorizontalBackground() { super ( 8332 , 1000 , 1 ); boardPositions = JOptionPane.showInputDialog( "Enter the order of the boards, separated by a space.\nExample: 1 2 2 1" ); String[] boards = boardPositions.split( " " ); if ((boardPositions != null ) && (boardPositions.isEmpty() != true )) { int x = 0 ; for ( int i= 0 ; i<boards.length; i++) { GreenfootImage image = new GreenfootImage( "Horizontal Hexagon Board (33x10) " + boards[i] + ".png" ); getBackground().drawImage(image, x, 0 ); // draw image on background x += image.getWidth(); // set new x-coordinate for next image draw } } else { JOptionPane.showMessageDialog( null , "One of the fields are empty, please try again!" ); } } } |