Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.java.gui

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-29-2005, 08:56 PM
Payam
Guest
 
Posts: n/a
Default How to combinate swing and graphics "chess game"

Hi
I want to do a chess game as I'm learning java. I want to have a
chess board using some graphic class and button windows and other
components using swing. I started doing it using some tutorials, but my
chess board does not get the same features as JComponents. So I want to
now witch classes you would use to a chess game and if you have some
tips for how to use them.

This is how I have done it, my problem is that I have to set out the
chess board exactly otherwise the other JComponents will get on the
chess board. And my mouse event does not have the same features on the
chess board as on the text area.

Here is the code (All of it)
Aplication Starts
public class ChessApp {
public static void main(String Args[]){
Globalz.setMode("Application");
Chess chess = new Chess();
}

}

Applet starts
import javax.swing.*;

public class ChessApplet extends JApplet{

public void init ()
Globalz.setMode("Applet");
Chess chess = new Chess();

}
}

My Chess Class
import javax.swing.*;
public class Chess {
public Chess(){
JFrame w = new JFrame();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringPanel springPanel = new SpringPanel ();
w.setContentPane(springPanel);
w.setSize(800,800);
w.setVisible(true);
}

}

Class that handels the layout
import javax.swing.*
import java.awt.event.*;

public class SpringPanel extends JPanel implements MouseListener{
private
BoarderPanel boarderPanel = new BoarderPanel();
InfoTextPanel infoTextPanel = new InfoTextPanel();
JScrollPane infoTextScrollPanel = new
JScrollPane(infoTextPanel);

public
SpringPanel () {
System.out.println(Globalz.getMode());

infoTextScrollPanel.setHorizontalScrollBarPolicy(S crollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
addMouseListener(this);
SpringLayout layout = new SpringLayout ();
setLayout ( layout );

add (boarderPanel);
add (infoTextScrollPanel);

layout.putConstraint (SpringLayout.WEST, boarderPanel,
0, SpringLayout.WEST, this);
layout.putConstraint (SpringLayout.NORTH, boarderPanel,
0, SpringLayout.NORTH, this);

layout.putConstraint (SpringLayout.WEST, infoTextScrollPanel,
boarderPanel.getLengthX()+10,
SpringLayout.WEST, boarderPanel);
layout.putConstraint (SpringLayout.NORTH, infoTextScrollPanel,
0,SpringLayout.NORTH, boarderPanel);

}

public void mousePressed (MouseEvent e) {
infoTextPanel.addText(" Hold");
infoTextPanel.doit();

}

public void mouseReleased (MouseEvent e) {
infoTextPanel.addText(" Un Hold");
infoTextPanel.doit();

}

public void mouseEntered (MouseEvent e) {
infoTextPanel.addText(" Enterd");
infoTextPanel.doit();

}

public void mouseExited (MouseEvent e) {
infoTextPanel.addText(" Exit");
infoTextPanel.doit();

}

public void mouseClicked (MouseEvent e) {
infoTextPanel.addText(" Klick");
infoTextPanel.doit();

}
}

Class that prints the Chess board
import javax.swing.*;
import java.awt.*;

public class BoarderPanel extends JPanel{
private int lengthX;
private int lengthY;
private Color[] color = new Color [3];
private int range;
private int border;
private int fontSize;

public int getLengthX(){return lengthX;}
public int getLengthY(){return lengthY;}
public BoarderPanel(){
color[0] = Color.black;
color[1] = Color.white;
color[2] = Color.red;
range = 50;
border = 10;
fontSize=15;
lengthX=range*8+border+fontSize;
lengthY=range*8+border+fontSize;
}

public void paintComponent (Graphics g) {
setSize(lengthX,lengthY);
g.setColor(color[2]);
g.fillRect (fontSize, 0, lengthX, lengthY-fontSize );
for(int i=0; i<8; i++){
for (int m=0; m<8; m++){
if(((m+i) % 2) == 0)
g.setColor(color[1]);
else
g.setColor(color[0]);

g.fillRect(fontSize+border/2+m*range,border/2+i*range,range,range);
}
}
Font f = new Font("Courier",Font.BOLD + Font.BOLD,fontSize);
g.setFont(f);
g.setColor(Color.black);
for(int i=0; i<8; i++){

g.drawString(String.valueOf(8-i),0,border+fontSize/2+range/2+range*i);
g.drawString(String.valueOf((char)
('A'+i)),fontSize/2+range*i+range/2,lengthY);
}

}
}

Class that print out debug text
import javax.swing.*;
import java.awt.*;
public class InfoTextPanel extends JTextArea{
private
String text;
String temp;
public
InfoTextPanel(){
setFont(new Font("Serif", Font.PLAIN, 12));

setBackground(Color.white);
setForeground(Color.red);
setEditable(false);
setColumns(10);
setRows(10);

}
void addText(String str){
text = text +'\n'+ str;
}
void addTempText(String str){
temp = str;
}
void doit(){
setText(temp +'\n' +text);
}

}

Class that handels global variabels
public class Globalz {

private static String mode;
public static String getMode(){
return mode;
}
public static void setMode(String a){
mode = a;
}

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 08:30 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.