Shpongle
November 19th, 2009, 09:34 PM
ok so iv an assignment to do and im flying through it, i have to make the game checkers / drafts . iv got my board class extending a JPanel . it that class an 8 by 8 array of colored panels are constructed and initialised to redpanels or blackpanels. iv then logic to set up properly and display the board in my game class. i add the board to my game class in the content pane and set that in the games constructor. i can access any board panel by array index. ie myboard.boardpanels[5][1] .
i have a pieces class shown below but i cant seem to get the pieces to show up on the board , since the pieces will be drawn on colored panels which extend jpanels i overloaded the paint method in the colored panel class.
in my game im trying to draw the pieces like this
mypiece = new DraftsPiece(10,10,100,Color.WHITE);
myBoard.boardpanels[5][1].add(mypiece);
myBoard.boardpanels[5][1].repaint();
please if anyone can help id greatly appreciate it , its driving me mad , and iv been at it for two days straight! all over javadocs and the web! , and still cant get it working , im gettin no compilation errors or runtime errors,
thanks
---------------------------------------------------------------------
/**
* to fill in!
*
* DraftsPieces class
* .
**/
package draftspkg;
import java.awt.Component;
import java.awt.Color;
import java.awt.Graphics;
public class DraftsPiece extends Component{
boolean isking;
int x, y, radius;
Color colour;
public DraftsPiece(int x, int y, int radius, Color colour){
this.x = x;
this.y = y;
this.radius = radius;
this.colour = colour;
isking = false;
}
//class functions
// draw the piece
public void draw(Graphics g) {
g.setColor(colour);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
public void setKing( ){
isking = true;
}
}
-------------------------------------------------------------------
/**
*
* Colored Panel class
*
*
**/
package draftspkg;
import javax.swing.JPanel;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
public class ColoredPanel extends JPanel{
public ColoredPanel(Color c){
super();
setBackground(c);
}
public ColoredPanel(Color c, int h , int w){
super();
setBackground(c);
setPreferredSize(new Dimension(h,w));
}
public void changeColor(Color c){
setBackground(c);
}
// override paint method to paint shapes on the panel
public void paint(Graphics g) {
super.paint(g);
// draw the shape
g.setColor(Color.WHITE);
g.fillOval(150, 150, 100, 100);
}
}
-------------------------------------------------------------------------
i have a pieces class shown below but i cant seem to get the pieces to show up on the board , since the pieces will be drawn on colored panels which extend jpanels i overloaded the paint method in the colored panel class.
in my game im trying to draw the pieces like this
mypiece = new DraftsPiece(10,10,100,Color.WHITE);
myBoard.boardpanels[5][1].add(mypiece);
myBoard.boardpanels[5][1].repaint();
please if anyone can help id greatly appreciate it , its driving me mad , and iv been at it for two days straight! all over javadocs and the web! , and still cant get it working , im gettin no compilation errors or runtime errors,
thanks
---------------------------------------------------------------------
/**
* to fill in!
*
* DraftsPieces class
* .
**/
package draftspkg;
import java.awt.Component;
import java.awt.Color;
import java.awt.Graphics;
public class DraftsPiece extends Component{
boolean isking;
int x, y, radius;
Color colour;
public DraftsPiece(int x, int y, int radius, Color colour){
this.x = x;
this.y = y;
this.radius = radius;
this.colour = colour;
isking = false;
}
//class functions
// draw the piece
public void draw(Graphics g) {
g.setColor(colour);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
public void setKing( ){
isking = true;
}
}
-------------------------------------------------------------------
/**
*
* Colored Panel class
*
*
**/
package draftspkg;
import javax.swing.JPanel;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
public class ColoredPanel extends JPanel{
public ColoredPanel(Color c){
super();
setBackground(c);
}
public ColoredPanel(Color c, int h , int w){
super();
setBackground(c);
setPreferredSize(new Dimension(h,w));
}
public void changeColor(Color c){
setBackground(c);
}
// override paint method to paint shapes on the panel
public void paint(Graphics g) {
super.paint(g);
// draw the shape
g.setColor(Color.WHITE);
g.fillOval(150, 150, 100, 100);
}
}
-------------------------------------------------------------------------