APL/APL2.1/TP11/Volume/Volume.java

41 lines
1.2 KiB
Java
Raw Normal View History

2022-03-28 17:27:59 +02:00
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Volume extends JComponent implements MouseWheelListener {
private static final int margin = 10;
private static final int numCircles = 10;
private int currentLevel = 0;
public void mouseWheelMoved(MouseWheelEvent ev) {
currentLevel -= ev.getWheelRotation();
currentLevel = Math.max(0, Math.min(currentLevel, 11));
repaint();
}
@Override
protected void paintComponent(Graphics brush) {
Graphics newBrush = brush.create();
if (this.isOpaque()) {
newBrush.setColor(this.getBackground());
newBrush.fillRect(0, 0, this.getWidth(), this.getHeight());
}
newBrush.setColor(new Color(50, 50, 50));
newBrush.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < numCircles; i++) {
if (currentLevel > i) newBrush.setColor(new Color(230, 170, 0));
else newBrush.setColor(new Color(200, 200, 200));
newBrush.fillOval(margin + (getWidth() - margin * 2) / numCircles * i, getHeight() / 2, getHeight() / 4, getHeight() / 4);
}
}
public Volume() {
super();
}
}