//<applet code="GameApplet.java" width="400" height="500"> the code did not work!</applet>
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class GameApplet extends Applet implements MouseListener
{
private static final long serialVersionUID = 6599028514896642279L;
public static final int RECTANGLE = 0;
public static final int CIRCLE = 1;
private Grid grid;
private int unitSize;
private int cellShape;
private boolean gridIsVisible;
private boolean fillCell;
private boolean win;
private Color gridColor;
private Color cellColor;
private Color backgroundColor;
public void init()
{
this.addMouseListener(this);
grid = new Grid(7,8);
unitSize = 50;
cellShape = 1;
gridIsVisible = true;
fillCell = false;
win = false;
gridColor = Color.GRAY;
cellColor = Color.RED;
backgroundColor = new Color(236, 236, 236);
repaint();
}
/**
* paints the component in a frame
*/
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
if (win)
{
}
else
{
g2.setColor(this.backgroundColor);
g2.fillRect(0, 0, this.unitSize * this.grid.getSize(),
this.unitSize * this.grid.getSize());
Rectangle2D.Double r;
Ellipse2D.Double e = new Ellipse2D.Double();
for (int i = 0; i < this.grid.getSize(); i++)
{
for (int j = 0; j < this.grid.getSize(); j++)
{
r = new Rectangle2D.Double(i * this.unitSize, j
* this.unitSize, this.unitSize,
this.unitSize);
e = new Ellipse2D.Double(i * this.unitSize, j
* this.unitSize, this.unitSize,
this.unitSize);
// if is occupied, fill in cell
if (grid.isOccupied(i, j))
{
g2.setColor(this.cellColor);
if (this.cellShape == GridComponent.RECTANGLE)
{
if (!fillCell)
{
r.setFrame(r.x + (this.unitSize / 5),
r.y + (this.unitSize / 5),
3 * (this.unitSize / 5),
3 * (this.unitSize / 5));
}
g2.fill(r);
}
else if (this.cellShape == GridComponent.CIRCLE)
{
if (!fillCell)
{
e.setFrame(r.x + (this.unitSize / 5),
r.y + (this.unitSize / 5),
3 * (this.unitSize / 5),
3 * (this.unitSize / 5));
}
g2.fill(e);
}
}
}
}
if (gridIsVisible)
{
g2.setColor(this.gridColor);
for (int i = 0; i <= this.grid.getSize(); i++)
{
g2.drawLine(i * this.unitSize, 0, i * this.unitSize,
this.grid.getSize() * this.unitSize);
g2.drawLine(0, i * this.unitSize, this.grid.getSize()
* this.unitSize, i * this.unitSize);
}
}
}
}
public Grid getGrid()
{
return this.grid;
}
public void setBoxSize(int s)
{
this.unitSize = s;
this.repaint();
}
public void setVisibleGrid(boolean isVisible)
{
if (isVisible)
gridIsVisible = true;
if (!isVisible)
gridIsVisible = false;
}
public boolean getVisibleGrid()
{
return gridIsVisible;
}
public void setCellShape(int shape)
{
if (shape == GridComponent.RECTANGLE)
cellShape = GridComponent.RECTANGLE;
else if (shape == GridComponent.CIRCLE)
cellShape = GridComponent.CIRCLE;
}
public void setGridColor(Color c)
{
gridColor = c;
}
public void setCellColor(Color c)
{
cellColor = c;
}
public Color getGridColor()
{
return gridColor;
}
public Color getCellColor()
{
return cellColor;
}
public void setBackgroundColor(Color c)
{
backgroundColor = c;;
}
public Color getBackgroundColor()
{
return backgroundColor;
}
public void setCellsFilled(boolean f)
{
fillCell = f;
}
public boolean areCellsFilled()
{
return fillCell;
}
public void mouseExited(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{
this.grid.click(Math.round((e.getY() / this.unitSize)), Math.round((e
.getX() / this.unitSize)));
win = this.grid.checkWin();
this.repaint();
}
}