Anhang anzeigen 15042
[CODE lang="java" title="TestMill"]import javax.swing.JFrame;
public class TestMill {
public static void main(String[] args) {
JFrame frame = new JFrame("Mühle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MillPanel(500, 500));
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
}
[/CODE]
[CODE lang="java" title="MillPanel"]import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MillPanel extends JPanel {
private Color boardColor = Color.YELLOW.darker();
private Color lineColor = Color.BLACK;
private Rectangle bounds;
public MillPanel(int width, int height) {
setPreferredSize(new Dimension(width, height));
int off = (int) (width * .05);
bounds = new Rectangle(off, off, width - 2 * off, height - 2 * off);
setBackground(Color.DARK_GRAY);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawBoard(g);
}
private void drawBoard(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int width = bounds.width;
int height = bounds.height;
int stoneSize = getStoneSize();
Stroke oldStrok = g2D.getStroke();
g2D.setStroke(new BasicStroke(3));
g2D.setColor(boardColor);
g2D.fill(bounds);
int x = bounds.x + stoneSize;
int tmpWidth = width - 2 * stoneSize;
int step = tmpWidth / 3;
g2D.setColor(lineColor);
g2D.drawLine(bounds.x + width / 2, bounds.y + stoneSize, bounds.x + width / 2, bounds.y + height - stoneSize);
g2D.drawLine(x, bounds.y + height / 2, bounds.x + height - stoneSize, bounds.y + height / 2);
for (int i = 0; i < 3; i++) {
if (i == 2) {
g2D.setColor(boardColor);
g2D.fillRect(x, x, tmpWidth, tmpWidth);
}
g2D.setColor(lineColor);
g2D.drawRect(x, x, tmpWidth, tmpWidth);
tmpWidth -= step;
x += step / 2;
}
g2D.setStroke(oldStrok);
}
private int getStoneSize() {
return (int) (bounds.width * .1);
}
}[/CODE]