APL/DEV 3.2/TP03/Luminance/LumPanel.java

90 lines
2.2 KiB
Java
Raw Normal View History

2022-10-20 12:49:09 +02:00
import javax.swing.JPanel;
import java.awt.*;
2022-10-26 11:13:11 +02:00
import java.util.LinkedList;
2022-10-20 12:49:09 +02:00
import java.util.Random;
public class LumPanel extends JPanel {
2022-10-26 11:13:11 +02:00
private LinkedList<Color> l;
2022-10-20 12:49:09 +02:00
int width = 30;
int offset = 30;
int height = 60;
int spacing = 5;
2022-10-26 11:13:11 +02:00
int mx, my = 0;
2022-10-20 12:49:09 +02:00
public LumPanel() {
2022-10-26 11:13:11 +02:00
l = new LinkedList<Color>();
2022-10-20 12:49:09 +02:00
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();
}
}
2022-10-26 11:13:11 +02:00
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();
}
2022-10-20 12:49:09 +02:00
@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);
2022-10-26 11:13:11 +02:00
gg.setColor(Color.BLACK);
gg.fillOval(mx-3, my-3, 6, 6);
2022-10-20 12:49:09 +02:00
gg.setColor(c);
gg.fillPolygon(p);
gg.setColor(Color.BLACK);
gg.drawPolygon(p);
i++;
}
}
}