TP11 & TP12

This commit is contained in:
HORVILLE 2022-04-12 17:16:22 +02:00
parent 39ef600e6d
commit 619b3ac7b6
32 changed files with 408 additions and 29 deletions

Binary file not shown.

View File

@ -0,0 +1,40 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Observer implements MouseListener {
private JLabel prevSelected;
public void mouseClicked(MouseEvent evenement) {
JLabel lb = (JLabel)evenement.getSource();
if (prevSelected != null) {
prevSelected.setBackground(new Color(255, 255, 255));
}
lb.setBackground(new Color(0, 255, 255));
prevSelected = lb;
}
public void mouseEntered(MouseEvent evenement) {
JLabel lb = (JLabel)evenement.getSource();
if (lb == prevSelected) {
lb.setBackground(new Color(0, 255, 255));
} else {
lb.setBackground(new Color(200, 200, 200));
}
}
public void mouseExited(MouseEvent evenement) {
JLabel lb = (JLabel)evenement.getSource();
if (lb == prevSelected) {
lb.setBackground(new Color(0, 200, 255));
} else {
lb.setBackground(new Color(255, 255, 255));
}
}
public void mousePressed(MouseEvent evenement) {}
public void mouseReleased(MouseEvent evenement) {}
}

Binary file not shown.

View File

@ -2,42 +2,22 @@ import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Playlist extends JPanel implements MouseListener {
private JLabel prevSelection;
private JLabel prevHover;
public void mouseClicked(MouseEvent evenement) {
JLabel raiser = evenement.getComponent();
raiser.setBackground(new Color(0, 150, 255));
if (prevSelection != null) {
prevSelection.setBackground(new Color(255, 255, 255));
}
}
public void mouseEntered(MouseEvent evenement) {
}
public void mouseExited(MouseEvent evenement) {
}
public void mousePressed(MouseEvent evenement) {}
public void mouseReleased(MouseEvent evenement) {}
@Override
public void paintComponent(Graphics brush) {
}
public class Playlist extends JPanel {
private int elementCount;
private Observer obs;
public Playlist() {
super();
elementCount = 0;
obs = new Observer();
}
public void add(String title) {
elementCount += 1;
this.setLayout(new GridLayout(elementCount, 1));
JLabel l = new JLabel(title);
l.addMouseListener(this);
l.addMouseListener(obs);
l.setOpaque(true);
this.add(l);
}

Binary file not shown.

View File

@ -0,0 +1,25 @@
import java.awt.*;
import javax.swing.*;
public class TestPlaylist {
public static void main(String[] args) {
JFrame f = new JFrame("Fond");
f.setSize(200, 210);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Playlist p = new Playlist();
p.add("Speak To Me / Breathe");
p.add("On The Run");
p.add("Time");
p.add("The Great Gig In The Sky");
p.add("Money");
p.add("Us and Them");
p.add("Any Colour You Like");
p.add("Brain Damage");
p.add("Eclipse");
f.add(p);
f.setVisible(true);
}
}

View File

@ -0,0 +1,26 @@
public class Observer implements MouseMotionListener, MouseListener {
public Observer() {}
void mouseClicked(MouseEvent evenement) {}
void mouseEntered(MouseEvent evenement) {}
void mouseExited(MouseEvent evenement) {}
void mouseMoved(MouseEvent evenement) {}
void mousePressed(MouseEvent evenement) {
Rectangle rect = (Rectangle)evenement.getSource();
rect.SetX(evenement.getX());
rect.SetY(evenement.getY());
rect.ShouldDraw(true);
}
void mouseReleased(MouseEvent evenement) {
Rectangle rect = (Rectangle)evenement.getSource();
rect.ShouldDraw(false);
}
void mouseDragged(MouseEvent evenement) {
Rectangle rect = (Rectangle)evenement.getSource();
rect.SetSX(evenement.getX());
rect.SetSY(evenement.getY());
}
}

View File

@ -0,0 +1,62 @@
public class Rectangle extends JComponent {
private int X;
private int Y;
private int SX;
private int SY;
private boolean shouldDraw;
public Rectangle() {
shouldDraw = false;
X = 0;
Y = 0;
SX = 0;
SY = 0;
}
public void SetX(int X) {
this.X = X;
}
public void SetY(int Y) {
this.Y = Y;
}
public void SetSX(int SX) {
this.SX = SX;
}
public void SetSY(int SY) {
this.SY = SY;
}
public void ShouldDraw(boolean shouldDraw) {
this.shouldDraw = shouldDraw;
}
@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());
}
if (this.shouldDraw) {
newBrush.setColor(new Color(0, 220, 255));
if (X < SX) {
int buff = X;
X = SX;
SX = buff;
}
if (Y < SY) {
int buff = Y;
Y = SY;
SY = buff;
}
}
}
}

View File

@ -0,0 +1,12 @@
public class TestRectangle {
public static void main(String[] args) {
JFrame f = new JFrame("Fond");
f.setSize(700, 200);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rectangle r = new Rectangle();
r.addMouseMotionListener(new Observer());
f.setVisible(true);
}
}

Binary file not shown.

View File

@ -0,0 +1,62 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Observer implements ActionListener {
public Observer() {
}
public void actionPerformed(ActionEvent evt) {
String name = evt.getActionCommand();
JPanel f = (JPanel)((JButton)evt.getSource()).getParent();
if (name == "Jaune") {
f.setBackground(Color.YELLOW);
} else if (name == "Cyan") {
f.setBackground(Color.CYAN);
} else if (name == "Magenta") {
f.setBackground(Color.MAGENTA);
}
}
}
public class Apparences {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("toto");
} catch (ClassNotFoundException e) {
System.err.println("CONCENTRE TOI NOM DE DIEU !!");
} catch (ClassCastException e) {
System.err.println("Tu pourrais te concentrer et coder bien ?");
} catch (Exception e) {
System.err.println("Prout !");
}
JFrame f = new JFrame("Fond");
f.setSize(200, 200);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1, 3));
JButton b1 = new JButton("Jaune");
JButton b2 = new JButton("Cyan");
JButton b3 = new JButton("Magenta");
Observer observer = new Observer();
b1.addActionListener(observer);
b2.addActionListener(observer);
b3.addActionListener(observer);
f.add(b1);
f.add(b2);
f.add(b3);
f.setVisible(true);
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,41 @@
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();
}
}

Binary file not shown.

View File

@ -0,0 +1,13 @@
public class Arithmetic {
public static void crash() {
System.out.println(1 / 0);
}
public static void main(String[] args) {
try {
crash();
} catch (ArithmeticException e) {
System.err.println(e);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,44 @@
import java.awt.*;
import javax.swing.*;
public class Degres {
public static void main(String[] args) {
JFrame f = new JFrame("Degrés !");
f.setSize(300, 150);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
JTextField CC = new JTextField();
JTextField CF = new JTextField();
JLabel LC = new JLabel("°C");
JLabel LF = new JLabel("°F");
LC.setSize(20, 20);
LF.setSize(20, 20);
LC.setLocation(230, 30);
LF.setLocation(230, 60);
CC.setSize(200, 20);
CF.setSize(200, 20);
CC.setLocation(20, 30);
CF.setLocation(20, 60);
Observer obs1 = new Observer(false);
Observer obs2 = new Observer(true);
obs1.setOutputField(CF);
obs2.setOutputField(CC);
CC.addActionListener(obs1);
CF.addActionListener(obs2);
f.add(LC);
f.add(LF);
f.add(CF);
f.add(CC);
f.setVisible(true);
}
}

Binary file not shown.

View File

@ -0,0 +1,35 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Observer implements ActionListener {
private JTextField outputField;
private boolean toDeg;
public Observer(boolean toDeg) {
this.toDeg = toDeg;
}
public void setOutputField(JTextField f) {
outputField = f;
}
public void actionPerformed(ActionEvent ev) {
try {
float temp = Float.parseFloat(ev.getActionCommand());
if (toDeg) {
temp -= 32f;
temp *= 5f/9f;
} else {
temp *= 9f/5f;
temp += 32f;
}
outputField.setText(Float.toString(temp));
} catch (NumberFormatException ex) {
outputField.setText("???");
}
}
}

Binary file not shown.

View File

@ -0,0 +1,13 @@
import java.io.*;
public class Incomplet {
public static void main(String[] args) {
try {
byte[] txt = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x0D, 0x0A};
System.out.write(txt);
} catch (IOException e) {
System.err.println(e);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
public class Arithmetic {
public static void main(String[] args) {
System.out.println( 1 / 0 );
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
public class ArrayIndex {
public static void main(String[] args) {
System.out.println(args[args.length]);
}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
public class NullPointer {
public static void main(String[] args) {
String s = null;
s.charAt(1);
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
public class NumberFormat {
public static void main(String[] args) {
Integer.parseInt("owo");
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
public class Runtime {
public static void main(String[] args) {
throw new RuntimeException("OwO");
}
}