Button Application

<< Dueling Robots Example | Applications | Timer Applicaiton >>

These can be used as buttons for the user to make a choice, or even as locations on a board game like Chinese checkers or Battleship.

TriangleButton.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

public class TriangleButton {
	private int x,y, size;
	private Color color, borderColor;
	private Polygon shape;

	public TriangleButton(int x, int y, int size, Color c, Color b, boolean up){
		this.x=x;
		this.y=y;
		this.size=size;
		this.setColor(c);
		this.setBorderColor(b);
		shape = new Polygon();
		shape.addPoint(x, y);
		int deltaX = size/2;
		int deltaY = (int)(0.5*size*Math.sqrt(3));
		if (up)
			deltaY*=-1;
		shape.addPoint(x+deltaX, y+deltaY);
		shape.addPoint(x+size, y);

	}
	public TriangleButton(int x, int y, int size, boolean up){
		this(x,y,size,Color.YELLOW, Color.BLACK, up);
	}
	public TriangleButton(int x, int y, int size, Color c, boolean up){
		this(x,y,size,c, Color.BLACK, up);
	}
	public TriangleButton(int x, int y, int size, Color c){
		this(x,y,size,c, Color.BLACK, true);
	}
	public boolean contains(int x0, int y0){
		return shape.contains(x0, y0);
	}
	public void draw(Graphics g){
		g.setColor(color);
		g.fillPolygon(shape);
		g.setColor(borderColor);
		g.drawPolygon(shape);
	}
	public Color getBorderColor() {
		return borderColor;
	}
	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}


}

Board.java

import java.awt.Graphics;

public class Board {
	public final static int GAP=3; 
	private int top, left, tileSize, rows, col;
	private TriangleButton[][] tiles;

	public Board(int top, int left, int tileSize, int rows, int cols){
		this.top=top;
		this.left=left;
		this.tileSize=tileSize;
		tiles = new TriangleButton[rows][cols];
		for (int row=0; row< tiles.length; row++)
			for(int col=0; col<tiles[0].length; col++){
				int x0=left+col*tileSize+col*GAP;
				int y0=(int)(top +row*tileSize*Math.sqrt(3)+(row/2)*GAP);
				tiles[row][col]=new TriangleButton(x0, y0, tileSize, (row%2)==0);
			}

	}
	public void draw(Graphics g){
		for (TriangleButton[] row:tiles)
			for (TriangleButton b: row)
				b.draw(g);
	}
}

BoardTester.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BoardTester extends JPanel implements KeyListener
{
	public static int WIDTH=800;
	public static int HEIGHT=600;
    private Font titleFont, regularFont;
    private Board board;
    private TriangleButton tb;


    public BoardTester()
    {

    	//initialize variables here...
    	titleFont = new Font("Roman", Font.BOLD, 18);
    	regularFont = new Font("Helvetica", Font.PLAIN, 12);
    	board = new Board(200,  200,  30,  5, 8);
    	tb = new TriangleButton(100, 400, 50, Color.RED, true);

    }
	public static void main(String[] args) {
		BoardTester app= new BoardTester();
		JFrame window = new JFrame("Board Tester");
		window.setSize(WIDTH, HEIGHT);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.getContentPane().add(app);
		window.addKeyListener(app);
		//window.pack();
		window.setVisible(true);

	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(),getHeight());
		g.setColor(Color.BLUE);
		g.setFont(titleFont);
		g.drawString("Board Tester", 20, 20);
		g.setColor(Color.BLACK);
		g.setFont(regularFont);
		g.drawString("Version 1.0", 20, 40);

		board.draw(g);
		tb.draw(g);

	}
	// update is a workaround to cure Windows screen flicker problem
	public void update(Graphics g){
		paint(g);
	}

	// These 3 methods need to be declares to implement the KeyListener Interface
	@Override
	public void keyTyped(KeyEvent e) {}

	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}

}