import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Schachbrett extends JPanel implements MouseMotionListener, MouseListener
{
private JFrame frame;
private JPanel visualArea;
final private int feldSize = 70;
final private int textSpace = 50;
private Spielfigur[][] spielFigur;
public Schachbrett()
{
initSpielfiguren();
frame = new JFrame("Schachbrett");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(1024,768);
frame.setPreferredSize(frame.getSize());
frame.setLocationRelativeTo(null);
frame.setResizable(false);
this.setSize((feldSize*8+1)+ textSpace, (feldSize*8+1) + textSpace);
this.setPreferredSize(new Dimension((feldSize*8+1)+ textSpace, (feldSize*8+1)+ textSpace));
this.setMaximumSize(this.getSize());
this.setBackground(new Color(255,23,132));
this.addMouseMotionListener(this);
visualArea = new JPanel();
visualArea.setSize(new Dimension(frame.getSize()));
visualArea.setPreferredSize(new Dimension(frame.getSize()));
visualArea.setLayout(new BorderLayout());
visualArea.setBackground(Color.green);
visualArea.add(this, BorderLayout.EAST);
frame.getContentPane().add(visualArea);
frame.pack();
frame.setVisible(true);
}
public Dimension getFigurPos(int x, int y)
{
return new Dimension(this.spielFigur[x][y].getImage().getWidth(this),
this.spielFigur[x][y].getImage().getHeight(this));
}
private void initSpielfiguren()
{
spielFigur = new Spielfigur[4][8];
// Figuren Farbe Weiss
spielFigur[0][0] = new Spielfigur('T', false, 0, 0, feldSize, textSpace, 40);
spielFigur[0][1] = new Spielfigur('S', false, 1, 0, feldSize, textSpace, 40);
spielFigur[0][2] = new Spielfigur('L', false, 2, 0, feldSize, textSpace, 40);
spielFigur[0][3] = new Spielfigur('D', false, 3, 0, feldSize, textSpace, 40);
spielFigur[0][4] = new Spielfigur('K', false, 4, 0, feldSize, textSpace, 40);
spielFigur[0][5] = new Spielfigur('L', false, 5, 0, feldSize, textSpace, 40);
spielFigur[0][6] = new Spielfigur('S', false, 6, 0, feldSize, textSpace, 40);
spielFigur[0][7] = new Spielfigur('T', false, 7, 0, feldSize, textSpace, 40);
//Bauern
for(int i=0; i < 8; i++)
spielFigur[1][i] = new Spielfigur('B', false, i, 1, feldSize, textSpace, 40);
//Figuren Farbe Schwarz
spielFigur[2][0] = new Spielfigur('T', true, 0, 7, feldSize, textSpace, 40);
spielFigur[2][1] = new Spielfigur('S', true, 1, 7, feldSize, textSpace, 40);
spielFigur[2][2] = new Spielfigur('L', true, 2, 7, feldSize, textSpace, 40);
spielFigur[2][3] = new Spielfigur('D', true, 3, 7, feldSize, textSpace, 40);
spielFigur[2][4] = new Spielfigur('K', true, 4, 7, feldSize, textSpace, 40);
spielFigur[2][5] = new Spielfigur('L', true, 5, 7, feldSize, textSpace, 40);
spielFigur[2][6] = new Spielfigur('S', true, 6, 7, feldSize, textSpace, 40);
spielFigur[2][7] = new Spielfigur('T', true, 7, 7, feldSize, textSpace, 40);
//Bauern
for(int i=0; i < 8; i++)
spielFigur[3][i] = new Spielfigur('B', true, i, 6, feldSize, textSpace, 40);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// Spielfeld zeichnen
for(int i=0; i < 8; i++)
for(int j=0; j < 8; j++)
{
g.drawRect((textSpace+ (feldSize*i)),
0 + (feldSize*j),
feldSize, feldSize);
if (i % 2 == 0 ^ j % 2 == 0)
g.setColor(Color.gray);
else
g.setColor(Color.orange);
g.fillRect(textSpace + (feldSize*i),
0 + (feldSize*j), feldSize, feldSize);
}
//Gitternetz nachträglich zeichnen
g.setColor(Color.black);
for(int i=0; i < 8; i++)
for(int j=0; j < 8; j++)
{
g.drawRect((textSpace + (feldSize*i)),
0 + (feldSize*j),
feldSize, feldSize);
}
// Horizontale Spielfeldbeschriftung
char []letter ={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
// Vertikale Spielfeldbeschriftung
char []digits ={'8', '7', '6', '5', '4', '3', '2', '1'};
for(int i=0; i < 8; i++)
{
g.drawChars(letter,i, 1, textSpace + (feldSize/2) + (feldSize *i), feldSize *8 + 20);
g.drawChars(digits, i, 1, 20, feldSize/2 + feldSize*i);
}
//Spielfiguren zeichnen
for(int i= 0; i < 8; i++)
for(int j=0; j <4; j++)
g.drawImage(spielFigur[j][i].getImage(),
spielFigur[j][i].getPositionX(),
spielFigur[j][i].getPositionY(),
this);
}
public void mouseDragged(MouseEvent e)
{
//Image img;
//Dimension dim;
//int x,y;
//x = (e.getX()- textSpace)/feldSize;
//y = e.getY()/feldSize;
//System.out.println("X: "+ x);
//System.out.println("Y: "+ y);*/
}
public void mouseMoved(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String []args)
{
new Schachbrett();
}
}