import javax.swing.*;
import java.awt.*;
public class NikolausTest extends JFrame {
private boolean ani = false;
public NikolausTest() {
setSize(300, 400);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
if (ani) {
Graphics2D g2d = (Graphics2D)g;
paintStrich(250, 250, 350, 150, g2d);
paintStrich(250, 50, 150, 150, g2d);
paintStrich(50, 50, 150, 350, g2d);
paintStrich(50, 250, 350, 350, g2d);
paintStrich(250, 50, 350, 150, g2d);
paintStrich(50, 150, 150, 50, g2d);
paintStrich(150, 250, 50, 150, g2d);
paintStrich(250, 50, 150, 350, g2d);
g2d.setColor(getBackground());
paintStrich(50, 250, 350, 150, g2d);
paintStrich(250, 150, 150, 50, g2d);
paintStrich(150, 50, 50, 150, g2d);
paintStrich(50, 250, 150, 350, g2d);
paintStrich(250, 50, 350, 350, g2d);
paintStrich(50, 50, 350, 150, g2d);
paintStrich(50, 250, 150, 150, g2d);
paintStrich(250, 250, 150, 350, g2d);
ani = false;
}
}
private void paintStrich(int x1, int x2, int y1, int y2, Graphics2D g) {
if (x1 == x2) {
paintY(x1, y1, y2, g);
}
else if (y1 == y2) {
paintX(x1, x2, y1, g);
}
else {
paintDiagonal(x1, x2, y1, y2, g);
}
}
private void paintDiagonal(int x1, int x2, int y1, int y2, Graphics2D g) {
if (x1 < x2 && y1 < y2) {
for (int x = x1, y = y1; x < x2; x++, y++) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else if (x1 > x2 && y1 > y2){
for (int x = x1, y = y1; x > x2; x--, y--) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else if (x1 < x2 && y1 > y2){
for (int x = x1, y = y1; x < x2; x++, y--) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else if (x1 > x2 && y1 < y2){
for (int x = x1, y = y1; x > x2; x--, y++) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void paintY(int x1, int y1, int y2, Graphics2D g) {
if (y1 < y2) {
for (int i = y1; i < y2; i++) {
g.drawLine(x1, i, x1, i);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else {
for (int i = y1; i > y2; i--) {
g.drawLine(x1, i, x1, i);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void paintX(int x1, int x2, int y1, Graphics2D g) {
if (x1 < x2) {
for (int i = x1; i < x2; i++) {
g.drawLine(i, y1, i, y1);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else {
for (int i = x1; i > x2; i--) {
g.drawLine(i, y1, i, y1);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void drawHouse() {
ani = true;
repaint();
}
public static void main(String[] args) {
new NikolausTest().drawHouse();
}
}