TP03 ; graphsup proto
This commit is contained in:
36
DEV 3.2/TP03/Luminance/LumListener.java
Normal file
36
DEV 3.2/TP03/Luminance/LumListener.java
Normal file
@@ -0,0 +1,36 @@
|
||||
import java.awt.event.*;
|
||||
|
||||
public class LumListener implements MouseListener {
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
LumPanel p = (LumPanel)e.getSource();
|
||||
p.removeParralel(e.getX(), e.getY());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
72
DEV 3.2/TP03/Luminance/LumPanel.java
Normal file
72
DEV 3.2/TP03/Luminance/LumPanel.java
Normal file
@@ -0,0 +1,72 @@
|
||||
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;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
DEV 3.2/TP03/Luminance/Luminance.java
Normal file
27
DEV 3.2/TP03/Luminance/Luminance.java
Normal file
@@ -0,0 +1,27 @@
|
||||
import javax.swing.JFrame;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
/**
|
||||
* Luminance
|
||||
*/
|
||||
public class Luminance {
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame f = new JFrame();
|
||||
|
||||
LumPanel lm = new LumPanel();
|
||||
lm.setLocation(0, 0);
|
||||
lm.setSize(480, 100);
|
||||
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setLocation(200, 200);
|
||||
f.setSize(400, 100);
|
||||
f.setResizable(false);
|
||||
f.add(lm);
|
||||
f.setLayout(null);
|
||||
f.setVisible(true);
|
||||
|
||||
lm.addMouseListener(new LumListener());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user