I wrote a program that contains a few JPanel of the same kind in an array. anyways thats all good and dandy.
my problem is that the painting of them with the images is done very good. but it doesn't stop doing it as if its in a loop??
Here is the code:
package Try1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
// slot is a defined place on the backgammon board that can hold
// one type of pieces black or white.
//there are 24 slots 12 on each side and 6 in each quartor.
//if a slot holds 2 or more pieces those pieces cannot be eaten.
public class Slot extends JPanel{
private int ap=0;
private int slotNumber;
private int piecesAmount;
private SlotType type;
private Stack<WhitePiece> wPieces;
private Stack<BlackPiece> bPieces;
private Image wPieceImage;
private Image bPieceImage;
public Slot() throws IOException{
setLayout(null);
setOpaque(false);
//setBackground(new Color(0,0,0,0));
type = SlotType.empty;
piecesAmount = 0;
setSize(300,40);
wPieces = new Stack<WhitePiece>();
bPieces = new Stack<BlackPiece>();
wPieceImage = ImageIO.read(new File("pics/whitePiece.png"));
bPieceImage = ImageIO.read(new File("pics/blackPiece.png"));
}
public Slot(int pA, int sN, SlotType t) throws IOException{
setLayout(null);
setOpaque(false);
if(t != SlotType.empty){
piecesAmount = pA;
slotNumber = sN;
type = t;
wPieces = new Stack<WhitePiece>();
bPieces = new Stack<BlackPiece>();
wPieceImage = ImageIO.read(new File("pics/whitePiece.png"));
bPieceImage = ImageIO.read(new File("pics/blackPiece.png"));
if(t == SlotType.black){
for(int i=0;i<pA;i++)
bPieces.push(new BlackPiece());
}else{
for(int i=0;i<pA;i++)
wPieces.push(new WhitePiece());
}
}
}
public void paintComponent(Graphics g){
ap++;
System.out.println(ap);
super.paintComponent(g);
if(type == SlotType.empty){
System.out.println("no type selected slot is empty Slot Number"+slotNumber);
}else
if(type == SlotType.white){
if(!wPieces.isEmpty()){
if(slotNumber <= 12){
for(int i=0;i<piecesAmount;i++){
g.drawImage( wPieceImage, 5, i*30, null);
}
}
else{
for(int i=0;i<piecesAmount;i++){
g.drawImage(wPieceImage, 5,220-(i*30), null);
}
}
System.out.println("Slot #"+slotNumber+" was drawed");
}else{
System.out.println("Slot Stack is Empty Slot #"+slotNumber);
}
}else
{
if(!bPieces.isEmpty()){
if(slotNumber<=12){
for(int i=0;i<piecesAmount;i++){
g.drawImage(bPieceImage, 5, i*30, 30, 30, null);
}
}else{
for(int i=0;i<piecesAmount;i++){
g.drawImage(bPieceImage, 5, 220-(i*30), 30, 30, null);
}
}
}
else{
System.out.println("Slot Stack is empty Slot #"+slotNumber);
}
}
}
public SlotType getType(){
return type;
}
public void setType(SlotType t){
if(piecesAmount == 0)
type = t;
}
public int getPiecesAmount(){
return piecesAmount;
}
public void setPiecesAmount(int pa) throws IOException{
if(type != SlotType.empty){
piecesAmount = pa;
if(type == SlotType.black){
if(pa>bPieces.size())
for(int i=0;i<(pa-bPieces.size());i++)
bPieces.push(new BlackPiece());
else
if(pa<bPieces.size())
for(int i=0;i<(bPieces.size()-pa);i++)
bPieces.pop();
}
else{
if(pa>wPieces.size())
for(int i=0;i<(pa-wPieces.size());i++)
wPieces.push(new WhitePiece());
else
if(pa<wPieces.size())
for(int i=0;i<(wPieces.size()-pa);i++)
wPieces.pop();
}
}else{
System.out.println("Slot #"+slotNumber+" is Empty Slot");
}
}
public void decreasePiecesAmount(){
if(type != SlotType.empty){
piecesAmount --;
if(type == SlotType.black)
bPieces.pop();
else
wPieces.pop();
}
}
public void increasePiecesAmount() throws IOException{
if(type != SlotType.empty){
piecesAmount ++;
if(type == SlotType.black)
bPieces.push(new BlackPiece());
else
wPieces.push(new WhitePiece());
}
}
public void pushPiece(){
}
protected void setSlotNumber(int sN){
slotNumber = sN;
}
public int getSlotNumber(){
return slotNumber;
}
public String toString(){
return "Slot #"+slotNumber+"\nSlot Type is: "+type.toString()+"\nAmount of pieces is: "+piecesAmount;
}
}
and here is the code of the class that contains this class:
package Try1;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Board extends JPanel {
private int drawedAmount = 0;
private Slot[][] board;
private Image image;
private Image wpieceImage;
private Image bpieceImage;
public Board() throws IOException{
setLayout(null);
board = new Slot[2][12];
image = ImageIO.read(new File("pics/board.png"));
wpieceImage = ImageIO.read(new File("pics/whitePiece.png"));
bpieceImage = ImageIO.read(new File("pics/blackPiece.png"));
JLabel ap = new JLabel(String.valueOf(drawedAmount));
ap.setBounds(210,10,30,40);
add(ap);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(image, 0, 0, null);
try {
drawBoard(page,board);
drawedAmount++;
} catch (IOException e) {
e.printStackTrace();
}
/*
//first 2
page.drawImage(bpieceImage, 123, 20, null);
page.drawImage(bpieceImage, 123, 50, null);
//5 blacks
page.drawImage(wpieceImage, 350, 20, null);
page.drawImage(wpieceImage, 350, 50, null);
page.drawImage(wpieceImage, 350, 80, null);
page.drawImage(wpieceImage, 350, 110, null);
page.drawImage(wpieceImage, 350, 140, null);
// 3 whites
page.drawImage(wpieceImage, 472, 20, null);
page.drawImage(wpieceImage, 472, 50, null);
page.drawImage(wpieceImage, 472, 80, null);
//5 blacks
page.drawImage(bpieceImage, 650, 20, null);
page.drawImage(bpieceImage, 650, 50, null);
page.drawImage(bpieceImage, 650, 80, null);
page.drawImage(bpieceImage, 650, 110, null);
page.drawImage(bpieceImage, 650, 140, null);
//2 whites
page.drawImage(wpieceImage, 124, 577, null);
page.drawImage(wpieceImage, 124, 547, null);
//5 blacks
page.drawImage(bpieceImage, 348, 577, null);
page.drawImage(bpieceImage, 348, 547, null);
page.drawImage(bpieceImage, 348, 517, null);
page.drawImage(bpieceImage, 348, 487, null);
page.drawImage(bpieceImage, 348, 457, null);
//3 blacks
page.drawImage(bpieceImage, 470, 577, null);
page.drawImage(bpieceImage, 470, 547, null);
page.drawImage(bpieceImage, 470, 517, null);
//5 whites
page.drawImage(wpieceImage, 650, 577, null);
page.drawImage(wpieceImage, 650, 547, null);
page.drawImage(wpieceImage, 650, 517, null);
page.drawImage(wpieceImage, 650, 487, null);
page.drawImage(wpieceImage, 650, 457, null); */
}
//resets board to the start of backgammon.
public void resetBoard() throws IOException{
//setting slot numbers.
for(int i=0;i<2;i++)
for(int j=0;j<12;j++){
if(i==0){
board[i][j] = new Slot();
board[i][j].setSlotNumber(j+1);
}
else{
board[i][j] = new Slot();
board[i][j].setSlotNumber(23-j+1);
}
}
// #1
board[0][0].setType(SlotType.black);
board[0][0].setPiecesAmount(2);
System.out.println(board[0][0].toString());
// #6
board[0][5].setType(SlotType.white);
board[0][5].setPiecesAmount(5);
System.out.println(board[0][5].toString());
// #8
board[0][7].setType(SlotType.white);
board[0][7].setPiecesAmount(3);
System.out.println(board[0][7].toString());
// #12
board[0][11].setType(SlotType.black);
board[0][11].setPiecesAmount(5);
System.out.println(board[0][11].toString());
// #13
board[1][11].setType(SlotType.white);
board[1][11].setPiecesAmount(5);
System.out.println(board[1][11].toString());
// # 17
board[1][7].setType(SlotType.black);
board[1][7].setPiecesAmount(3);
System.out.println(board[1][7].toString());
// #19
board[1][5].setType(SlotType.black);
board[1][5].setPiecesAmount(5);
System.out.println(board[1][5].toString());
// #24
board[1][0].setType(SlotType.white);
board[1][0].setPiecesAmount(2);
System.out.println(board[1][0].toString());
//setting bounds for all slots.
for(int i=0;i<2;i++)
for(int j=0;j<12;j++)
if(i==0){
if(j<=6){
board[i][j].setBounds((j*45)+118,20,40,250);
add(board[i][j]);
}
else{
board[i][j].setBounds(((j-7)*56)+422,20,40,250);
add(board[i][j]);
}
}
else{
if(j<=6){
board[i][j].setBounds((j*45)+118,360,40,250);
add(board[i][j]);
}
else{
board[i][j].setBounds(((j-7)*56)+422,360,40,250);
add(board[i][j]);
}
}
}
public void drawBoard(Graphics g,Slot[][] board) throws IOException{
for(int i=0;i<2;i++){
for(int j=0;j<12;j++){
board[i][j].repaint();
}
}
}
public void movePiece(int fslotNumber,int lslotNumber) throws IOException{
//find the slot in which piece is loacted.
int []firstSlotPosition= new int[2];
int dobreak=1;
for(int i=0;i<2;i++){
for(int j=0;j<12;j++){
if(board[i][j].getSlotNumber()==fslotNumber){
firstSlotPosition[0]=i;
firstSlotPosition[1]=j;
dobreak=0;
break;
}
}
if(dobreak==0)
break;
}
//find the slot that piece should move to.
int []finalSlotPosition= new int[2];
int dobreak2=1;
for(int i=0;i<2;i++){
for(int j=0;j<12;j++){
if(board[i][j].getSlotNumber()==lslotNumber){
finalSlotPosition[0]=i;
finalSlotPosition[1]=j;
dobreak2=0;
break;
}
}
if(dobreak2==0)
break;
}
// make move
// if the place where we want to move is empty set type
// and if its taken by another color dont make move.
boolean makeMove = true;
if(board[finalSlotPosition[0]][finalSlotPosition[1]].getType()==SlotType.empty){
board[finalSlotPosition[0]][finalSlotPosition[1]].setType(board[firstSlotPosition[0]][firstSlotPosition[1]].getType());
}else
if(board[finalSlotPosition[0]][finalSlotPosition[1]].getType()!=board[firstSlotPosition[0]][firstSlotPosition[1]].getType()){
System.out.println("move canceled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
makeMove = false;
}
if(makeMove==true){
board[firstSlotPosition[0]][firstSlotPosition[1]].decreasePiecesAmount();
board[finalSlotPosition[0]][finalSlotPosition[1]].increasePiecesAmount();
}
}
}
This is the problem:
public void drawBoard(Graphics g,Slot[][] board) throws IOException{
for(int i=0;i<2;i++){
for(int j=0;j<12;j++){
board[i][j].repaint();
}
}
}
You call drawBoard()
from paintComponent()
. repaint()
does not do what you think it does. It requests repainting the component. That will eventually result in a paintComponent()
call. That has the effect of endless repainting.
As they are JComponents
on your game board, swing will call their respective paintComponent()
methods automatically, when the board is painted. You should not need drawBoard()
at all.