public class Test
{
public static void main(String[] args)
throws Exception
{
Benutzeroberfläche b = new Benutzeroberfläche(10, 10, 2, 2, 7, 7);
for (int i = 0; i < 10; i++)
{
b.einfärben(i, 3, Color.BLUE);
Thread.sleep(1000);
}
}
}
class Benutzeroberfläche
{
private Zeichenblatt zeichenblatt;
JFrame frame = new JFrame("Finde das Ziel");
public Benutzeroberfläche(int breite, int hoehe, int sx, int sy, int zx, int zy)
{
zeichenblatt = new Zeichenblatt(breite, hoehe);
// einen JFrame erzeugen
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
zeichenblatt.setPreferredSize(new Dimension(300, 300));
frame.add(zeichenblatt, BorderLayout.CENTER);
frame.add(createControls(), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
this.startzielknoten(sx, sy, zx, zy);
}
private Component createControls()
{
JPanel panel = new JPanel(new FlowLayout());
JButton start = new JButton("Start");
panel.add(start);
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
return panel;
}
public void startzielknoten(int sx, int sy, int zx, int zy)
{
zeichenblatt.seteinfärben(sx, sy, Color.RED);
zeichenblatt.seteinfärben(zx, zy, Color.RED);
zeichenblatt.repaint();
}
public void einfärben(int x, int y, Color farbe)
{
zeichenblatt.seteinfärben(x, y, farbe);
zeichenblatt.repaint();
}
}
class Zeichenblatt
extends JComponent
{
private int x;
private int y;
private Color[][] farbe = new Color[10][10];
Graphics2D g2d;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g2d = (Graphics2D)g;
this.zeichnesenkrecht();
this.zeichnewaagrecht();
this.zeichnekreise();
}
public void zeichnekreise()
{
for (int i = 0; i < x; i++)
{
for (int k = 0; k < y; k++)
{
g2d.setColor(farbe[i][k]);
g2d.fillOval((i * 30), (k * 30), 20, 20);
}
}
}
public void zeichnesenkrecht()
{
g2d.setColor(Color.GRAY);
for (int i = 0; i < x - 1; i++)
{
for (int k = 0; k < y; k++)
{// nur für die letzten nicht
g2d.drawLine((i * 30) + 15, (k * 30) + 10, ((i + 1) * 30) + 15, (k * 30) + 10);
}
}
}
public void zeichnewaagrecht()
{
g2d.setColor(Color.GRAY);
for (int i = 0; i < x; i++)
{// nur für die letzten nicht
for (int k = 0; k < y - 1; k++)
{
g2d.drawLine((i * 30) + 10, (k * 30) + 15, ((i) * 30) + 10, ((k + 1) * 30) + 15);
}
}
}
public void seteinfärben(int i, int k, Color farbe)
{
this.farbe[i][k] = farbe;
}
public Zeichenblatt(int x, int y)
{
this.x = x;
this.y = y;
for (int i = 0; i < x; i++)
{
for (int k = 0; k < y; k++)
{
this.farbe[i][k] = Color.LIGHT_GRAY;
}
}
}
}