/** * Called when the left mouse button has been clicked (pressed + released) * * @param x X position of the mouse pointer * @param y Y position of the mouse pointer */ public void handleLeftMouseClicked(int x, int y) { BlockContainerInterface cont = null; PipeBlock[] blocks = null; if (x >= contAllowed.getDrawingPositionX(0) && x <= (contAllowed.getDrawingPositionX(contAllowed.getSizeX() - 1) + contAllowed.getSquareSize()) && y >= contAllowed.getDrawingPositionY(0) && y <= (contAllowed.getDrawingPositionY(contAllowed.getSizeY() - 1) + contAllowed.getSquareSize())) { cont = contAllowed; selectedAllowed = true; blocks = blocksAllowed; } else if (x >= contNotAllowed.getDrawingPositionX(0) && x <= (contNotAllowed.getDrawingPositionX(contNotAllowed.getSizeX() - 1) + contNotAllowed.getSquareSize()) && y >= contNotAllowed.getDrawingPositionY(0) && y <= (contNotAllowed.getDrawingPositionY(contNotAllowed.getSizeY() - 1) + contNotAllowed.getSquareSize())) { cont = contNotAllowed; selectedAllowed = false; blocks = blocksNotAllowed; } else { return; } int blockPosX = (x - cont.getOffsetX()) / blockSize; int blockPosY = (y - cont.getOffsetY()) / blockSize; selPosX = blockPosX; selPosY = blockPosY; selectedBlock = null; for (int i = 0; i < blocks.length; i++) { if (blockPosX == blocks[i].getPosX() && blockPosY == blocks[i].getPosY()) { selectedBlock = blocks[i]; break; } } }
public void init(GameEnvironmentInterface environ) { this.environment = environ; this.contAllowed = new BlockContainerData(offsetX + 1, offsetY + 20, 3, 10, blockSize); this.contNotAllowed = new BlockContainerData(offsetX + 1 + 6 * blockSize, offsetY + 20, 3, 10, blockSize); levelFactoryAllowed = new LevelFactory(environment, contAllowed, LevelFactory.GameType.UntilWaterStopped); levelFactoryNotAllowed = new LevelFactory(environment, contNotAllowed, LevelFactory.GameType.UntilWaterStopped); int rightColumnX = contNotAllowed.getDrawingPositionX(0) + contNotAllowed.getSizeX() * blockSize + 10; buttons = new EButton[4]; buttons[0] = EButton.create("Save"); buttons[0].getComponent().setBounds(rightColumnX, offsetY + 20, 73, 25); buttons[1] = EButton.create("Exit"); buttons[1].getComponent().setBounds(rightColumnX, offsetY + 50, 73, 25); buttons[2] = EButton.create("-->"); buttons[2] .getComponent() .setBounds(offsetX + contAllowed.getSizeX() * blockSize + 10, offsetY + 50, 73, 25); buttons[3] = EButton.create("<--"); buttons[3] .getComponent() .setBounds(offsetX + contAllowed.getSizeX() * blockSize + 10, offsetY + 80, 73, 25); if (buttons != null) { for (int i = 0; i < buttons.length; i++) { buttons[i].addActionListener(this); environment.getScreenHandler().add(buttons[i].getComponent()); } } textFields = new TextField[5]; textFields[0] = new TextField(String.valueOf(levelFactoryAllowed.getNumberOfEmptyBlocks()), 2); textFields[0].setBounds(rightColumnX + 150, 100 + 3, 40, 18); textFields[0].setBackground(Color.white); textFields[1] = new TextField(String.valueOf(levelFactoryAllowed.getNumberOfStartBlocks()), 2); textFields[1].setBounds(rightColumnX + 150, 100 + 28, 40, 18); textFields[1].setBackground(Color.white); textFields[2] = new TextField(String.valueOf(levelFactoryAllowed.getTimeUntilWater()), 2); textFields[2].setBounds(rightColumnX + 150, 100 + 53, 40, 18); textFields[2].setBackground(Color.white); textFields[3] = new TextField(String.valueOf(levelFactoryAllowed.getWaterSpeed()), 2); textFields[3].setBounds(rightColumnX + 150, 100 + 78, 40, 18); textFields[3].setBackground(Color.white); textFields[4] = new TextField(String.valueOf(levelFactoryAllowed.getLeftToFill()), 2); textFields[4].setBounds(rightColumnX + 150, 100 + 103, 40, 18); textFields[4].setBackground(Color.white); for (int i = 0; i < textFields.length; i++) { environment.getScreenHandler().add(textFields[i]); } selectedBlock = null; blocksAllowed = levelFactoryAllowed.getAllowedBlocks(); blocksNotAllowed = levelFactoryNotAllowed.getNotAllowedBlocks(); bExit = false; mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & e.BUTTON1_MASK) != 0) { handleLeftMouseClicked( environment.getScreenHandler().getScreenX(e.getX()), environment.getScreenHandler().getScreenY(e.getY())); } } }; environment.getScreenHandler().getContainer().setBackground(Color.black); environment.getScreenHandler().getContainer().requestFocus(); environment.getScreenHandler().getContainer().addMouseListener(mouseListener); }
/** Draw all the game graphics */ public void draw() { Graphics g = environment.getScreenHandler().getCurrentGraphics(); g.clearRect( 0, 0, environment.getScreenHandler().getWidth(), environment.getScreenHandler().getHeight()); int gameSizeX = (contAllowed.getSizeX() + contNotAllowed.getSizeX() + 3) * blockSize + 2; int gameSizeY = contAllowed.getSizeY() * blockSize + 2; g.setColor(Color.white); g.drawString( "Allowed:", contAllowed.getDrawingPositionX(0), contAllowed.getDrawingPositionY(0) - 3); g.drawString( "Not allowed:", contNotAllowed.getDrawingPositionX(0), contNotAllowed.getDrawingPositionY(0) - 3); g.setColor(Color.blue); g.drawRect( contAllowed.getDrawingPositionX(0), contAllowed.getDrawingPositionY(0), contAllowed.getSizeX() * contAllowed.getSquareSize(), contAllowed.getSizeY() * contAllowed.getSquareSize()); g.drawRect( contNotAllowed.getDrawingPositionX(0), contNotAllowed.getDrawingPositionY(0), contNotAllowed.getSizeX() * contNotAllowed.getSquareSize(), contNotAllowed.getSizeY() * contNotAllowed.getSquareSize()); if (blocksAllowed != null) { for (int i = 0; i < blocksAllowed.length; i++) { blocksAllowed[i].draw(g); } } if (blocksNotAllowed != null) { for (int i = 0; i < blocksNotAllowed.length; i++) { blocksNotAllowed[i].draw(g); } } if (selectedBlock != null) { g.setColor(Color.white); BlockContainerInterface cont; if (selectedAllowed) { cont = contAllowed; } else { cont = contNotAllowed; } g.drawRect( selectedBlock.getMovingDrawingPosX(), selectedBlock.getMovingDrawingPosY(), cont.getSquareSize(), cont.getSquareSize()); } g.setColor(Color.white); int rightColumnX = contNotAllowed.getDrawingPositionX(0) + contNotAllowed.getSizeX() * blockSize + 10; g.drawString("Number of empty blocks:", rightColumnX, 100 + 15); g.drawString("Number of start blocks:", rightColumnX, 100 + 40); g.drawString("Initial time until water:", rightColumnX, 100 + 65); g.drawString("Initial water speed:", rightColumnX, 100 + 90); g.drawString("Initial blocks to fill:", rightColumnX, 100 + 115); rightColumnX = offsetX + gameSizeX + 20; g.setColor(Color.red); g.drawString("by Erland Isaksson", rightColumnX, offsetY + gameSizeY); environment.getScreenHandler().paintComponents(g); }