-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardComponent.java
84 lines (75 loc) · 1.4 KB
/
BoardComponent.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
*
* @author blybr
*/
public class BoardComponent extends JComponent
{
private PegGraphic[][] pegArray;
/**
* makes a visual for the game board
*/
public BoardComponent()
{
pegArray= new PegGraphic[4][4];
for (int i = 1; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
pegArray[i - 1][j] = new PegGraphic(i * 75, 400 - (j * 75));
}
}
}
/**
* paints current state of peg array
* @param g
*/
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
for(int i = 1; i < 5; i++)
{
for(int j = 0; j < 4; j++)
{
g2.setColor(Color.BLACK);
pegArray[i - 1][j].draw(g2);
}
}
}
/**
* gets the correct peg for each actionListener
* @param x
* @param y
* @return
*/
public PegGraphic getPeg(int x, int y)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(x==pegArray[i][j].getX() && y==pegArray[i][j].getY())
{
return pegArray[i][j];
}
}
}
return null; // should never return null
}
/**
* made to refresh only the BoardComponent
*/
public void paintAgain()
{
repaint();
}
}