Article View: borland.public.jbuilder.java.swing
Article #510How to combinate swing and graphics "chess game"
From: Payam
Date: Fri, 29 Jul 2005 20:51
Date: Fri, 29 Jul 2005 20:51
204 lines
6553 bytes
6553 bytes
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(ScrollPaneConstants.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;
}
}
Technical Details
Message-ID:
<1122670284.647704.72900@g44g2000cwa.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!g44g2000cwa.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 111d45,cb6f07a6acaf8d68,start X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!g44g2000cwa.googlegroups.com!not-for-mail From: "Payam" <payam.yavari@gmail.com> Newsgroups: borland.public.jbuilder.java.swing Subject: How to combinate swing and graphics "chess game" Date: 29 Jul 2005 13:51:24 -0700 Organization: http://groups.google.com Lines: 205 Message-ID: <1122670284.647704.72900@g44g2000cwa.googlegroups.com> NNTP-Posting-Host: 213.65.110.223 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1122670290 10332 127.0.0.1 (29 Jul 2005 20:51:30 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 29 Jul 2005 20:51:30 +0000 (UTC) User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: g44g2000cwa.googlegroups.com; posting-host=213.65.110.223; posting-account=io54nw0AAAA-_1Qyh9eWRSJm2If1P481