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

72 lines
1.8 KiB
Java

import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
public class LumPanel extends JPanel {
private ListChain<Color> l;
int width = 30;
int offset = 30;
int height = 60;
int spacing = 5;
public LumPanel() {
l = new ListChain<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();
}
}
@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(c);
gg.fillPolygon(p);
gg.setColor(Color.BLACK);
gg.drawPolygon(p);
i++;
}
}
}