APL/DEV 3.2/TP03/Luminance/LumPanel.java
2022-10-26 11:13:11 +02:00

90 lines
2.2 KiB
Java

import javax.swing.JPanel;
import java.awt.*;
import java.util.LinkedList;
import java.util.Random;
public class LumPanel extends JPanel {
private LinkedList<Color> l;
int width = 30;
int offset = 30;
int height = 60;
int spacing = 5;
int mx, my = 0;
public LumPanel() {
l = new LinkedList<Color>();
Random r = new Random();
for (int i = 0; i < 10; i++) {
l.add(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
}
}
private double lerp(double a, double b, double t) {
return a + (b - a) * t;
}
public void removeParralel(int x, int y) {
double dx = (double)x;
double dy = (double)y;
dx -= lerp(offset, 0, dy / (double)height);
dx -= spacing * Math.floor(dx / width);
int index = (int)Math.floor(dx / width);
if (index >= 0 && index < l.size()) {
Color c = l.get(index);
System.out.println("Luminance : " + (21 * c.getRed() + 72 * c.getGreen() + 7 * c.getBlue()));
l.remove(index);
repaint();
}
}
public void setMCoords(int x, int y) {
double dx = (double)x;
double dy = (double)y;
dx -= lerp(offset, 0, dy / (double)height);
dx -= spacing * Math.floor(dx / width);
this.mx = (int)dx;
this.my = (int)dy;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
Graphics gg = g.create();
if (isOpaque()) {
gg.setColor(getBackground());
gg.fillRect(getX(), getY(), getWidth(), getHeight());
}
int i = 0;
for (Color c : l) {
Polygon p = new Polygon();
int x = getX() + spacing * i + width * i;
p.addPoint(x + offset, getY());
p.addPoint(x + offset + width, getY());
p.addPoint(x + width, getY() + height);
p.addPoint(x, getY() + height);
gg.setColor(Color.BLACK);
gg.fillOval(mx-3, my-3, 6, 6);
gg.setColor(c);
gg.fillPolygon(p);
gg.setColor(Color.BLACK);
gg.drawPolygon(p);
i++;
}
}
}