// JavaProject MysteriousShapes

// MysteriousShapes.java
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Canvas extends JPanel {
private static final long serialVersionUID = 0L;
private Ellipse2D ellipse;
private Rectangle2D rect;
private float alpha_ellipse;
private float alpha_rect;
private float diff_alpha_ellipse = -0.01f;
private float diff_alpha_rect = -0.01f;
private float x_ellipse, y_ellipse;
private float x_rect, y_rect;
boolean active_ellipse;
boolean interrupted_ellipse;
boolean active_rect;
boolean interrupted_rect;
public void randomizeUpperLeftPointEllipse() {
x_ellipse = (float) (233*Math.random());
y_ellipse = (float) (201*Math.random());
}
public void randomizeUpperLeftPointRect() {
x_rect = (float) (273*Math.random());
y_rect = (float) (201*Math.random());
}
public Canvas() {
addMouseListener(new Adapter());
randomizeUpperLeftPointEllipse();
randomizeUpperLeftPointRect();
ellipse = new Ellipse2D.Float(x_ellipse, y_ellipse, 100f, 60f);
rect = new Rectangle2D.Float(x_rect, y_rect, 60f, 60f);
alpha_ellipse = 1f;
alpha_rect = 1f;
}
private void defineRendering(Graphics2D g) {
RenderingHints rh;
rh = new RenderingHints (
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
g.setRenderingHints(rh);
}
private void defineComposite(Graphics2D g, float alpha) {
g.setComposite (
AlphaComposite.getInstance (
AlphaComposite.SRC_OVER,
alpha
)
);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
defineRendering(g2d);
g2d.setPaint(new Color(100, 90, 255));
defineComposite(g2d, alpha_ellipse);
g2d.fill(ellipse);
g2d.setPaint(new Color(246, 118, 14));
defineComposite(g2d, alpha_rect);
g2d.fill(rect);
g2d.dispose();
}
class Adapter extends MouseAdapter {
RunnableEllipse run_e;
RunnableRect run_r;
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (ellipse.contains(x, y)) {
if (active_ellipse) {
interrupted_ellipse = true;
active_ellipse = false;
} else {
run_e =
new RunnableEllipse();
interrupted_ellipse = false;
run_e.thread_ellipse.start();
}
}
if (rect.contains(x, y)) {
if (active_rect) {
interrupted_rect = true;
active_rect = false;
} else {
run_r =
new RunnableRect();
interrupted_rect = false;
run_r.thread_rect.start();
}
}
}
}
class RunnableEllipse implements Runnable {
Thread thread_ellipse;
public RunnableEllipse() {
thread_ellipse =
new Thread(this);
active_ellipse = true;
}
@Override
public void run() {
while
(!interrupted_ellipse) {
repaint();
alpha_ellipse += diff_alpha_ellipse;
if (alpha_ellipse < 0) {
randomizeUpperLeftPointEllipse();
ellipse =
new Ellipse2D.Float(x_ellipse, y_ellipse, 100f, 60f);
alpha_ellipse = 0;
diff_alpha_ellipse = -diff_alpha_ellipse;
}
if (alpha_ellipse > 1) {
alpha_ellipse = 1;
diff_alpha_ellipse = -diff_alpha_ellipse;
}
try {
Thread.sleep(40);
}
catch (InterruptedException e) { }
}
}
}
class RunnableRect implements Runnable {
Thread thread_rect;
public RunnableRect() {
thread_rect =
new Thread(this);
active_rect = true;
}
@Override
public void run() {
while (!interrupted_rect) {
repaint();
alpha_rect += diff_alpha_rect;
if (alpha_rect < 0) {
randomizeUpperLeftPointRect();
rect =
new Rectangle2D.Float(x_rect, y_rect, 60f, 60f);
alpha_rect = 0;
diff_alpha_rect = -diff_alpha_rect;
}
if (alpha_rect > 1) {
alpha_rect = 1;
diff_alpha_rect = -diff_alpha_rect;
}
try {
Thread.sleep(30);
} catch (InterruptedException e) { }
}
}
}
}
class CFrame extends JFrame {
private static final long serialVersionUID = 0L;
Image icon;
public CFrame() {
setTitle("Mysteriöse Figuren");
icon = Toolkit.getDefaultToolkit().getImage("dh.png");
setIconImage(icon);
setSize(349, 301);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new Canvas());
setVisible(true);
}
}
public class MysteriousShapes {
public static void main(String[] args) {
new CFrame();
}
}
Download MysteriousShapes