Commit 3c9d20adcdd08ce9b126d9c663f31fdf8003f4af
1 parent
af80f4c2
nearly finished tp2
Showing
10 changed files
with
472 additions
and
13 deletions
Show diff stats
... | ... | @@ -0,0 +1,138 @@ |
1 | +package tp2.ArdoiseMagique; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.*; | |
5 | + | |
6 | +public class Ardoise extends JPanel { | |
7 | + private final int defaultWidth = 1000; | |
8 | + private final int defaultHeight = 600; | |
9 | + protected static final int BLACK = 1; | |
10 | + protected static final int WHITE = 0; | |
11 | + | |
12 | + private int oldx = -1; | |
13 | + private int oldy = -1; | |
14 | + | |
15 | + private final int[][] pixelArray = new int[defaultWidth][defaultHeight]; | |
16 | + | |
17 | + public Ardoise() { | |
18 | + super(); | |
19 | + this.setSize(defaultWidth, defaultHeight); | |
20 | + | |
21 | + wipe(); | |
22 | + } | |
23 | + | |
24 | + public void wipe() { | |
25 | + for (int i = 0; i < defaultWidth; ++i){ | |
26 | + for (int j = 0; j < defaultHeight; ++j){ | |
27 | + pixelArray[i][j] = WHITE; | |
28 | + } | |
29 | + } | |
30 | + | |
31 | + repaint(); | |
32 | + } | |
33 | + | |
34 | + public void setPixel(int x, int y, int color){ | |
35 | + if (x < 0 || y < 0 || x >= defaultWidth || y >= defaultHeight){ | |
36 | + return; | |
37 | + } | |
38 | + | |
39 | + if (oldx == -1 || oldy == -1){ | |
40 | + oldx = x; | |
41 | + oldy = y; | |
42 | + } | |
43 | + | |
44 | + plotLine(oldx, oldy, x, y); | |
45 | + | |
46 | + oldx = x; | |
47 | + oldy = y; | |
48 | + | |
49 | + System.out.println("tried to put a "+(color==BLACK?"black":"white")+" pixel at "+Integer.toString(x)+" "+Integer.toString(x)); | |
50 | + pixelArray[x][y] = color; | |
51 | + repaint(); | |
52 | + } | |
53 | + | |
54 | + public void plotLineLow(int x0, int y0, int x1, int y1){ | |
55 | + int dx = x1 - x0; | |
56 | + int dy = y1 - y0; | |
57 | + int yi = 1; | |
58 | + if (dy < 0) { | |
59 | + yi = -1; | |
60 | + dy = -dy; | |
61 | + } | |
62 | + int D = (2 * dy) - dx; | |
63 | + int ay = y0; | |
64 | + | |
65 | + for (int ax = x0; ax < x1; ++ax) { | |
66 | + pixelArray[ax][ay] = BLACK; | |
67 | + if (D > 0) { | |
68 | + ay = ay + yi; | |
69 | + D = D + (2 * (dy - dx)); | |
70 | + } else D = D + 2 * dy; | |
71 | + } | |
72 | + } | |
73 | + | |
74 | + public void plotLineHigh(int x0, int y0, int x1, int y1){ | |
75 | + int dx = x1 - x0; | |
76 | + int dy = y1 - y0; | |
77 | + int xi = 1; | |
78 | + if (dx < 0) { | |
79 | + xi = -1; | |
80 | + dx = -dx; | |
81 | + } | |
82 | + int D = (2 * dx) - dy; | |
83 | + int ax = x0; | |
84 | + | |
85 | + for (int ay = y0; ay < y1; ++ay) { | |
86 | + pixelArray[ax][ay] = BLACK; | |
87 | + if (D > 0) { | |
88 | + ax = ax + xi; | |
89 | + D = D + (2 * (dx - dy)); | |
90 | + } else D = D + 2 * dx; | |
91 | + } | |
92 | + } | |
93 | + | |
94 | + public void plotLine(int x0, int y0, int x1, int y1) { | |
95 | + if (Math.abs(y1 - y0) < Math.abs(x1 - x0)) { | |
96 | + if (x0 > x1) { | |
97 | + plotLineLow(x1, y1, x0, y0); | |
98 | + } else { | |
99 | + plotLineLow(x0, y0, x1, y1); | |
100 | + } | |
101 | + } else { | |
102 | + if (y0 > y1) { | |
103 | + plotLineHigh(x1, y1, x0, y0); | |
104 | + } else { | |
105 | + plotLineHigh(x0, y0, x1, y1); | |
106 | + } | |
107 | + } | |
108 | + } | |
109 | + | |
110 | + public void resetOld() { | |
111 | + oldx = -1; | |
112 | + oldy = -1; | |
113 | + } | |
114 | + | |
115 | + @Override | |
116 | + public void paintComponent(Graphics g){ | |
117 | + super.paintComponent(g); | |
118 | + | |
119 | + g.setColor(Color.WHITE); | |
120 | + for (int i = 0; i < defaultWidth; ++i){ | |
121 | + for (int j = 0; j < defaultHeight; ++j){ | |
122 | + /* | |
123 | + since most of the image will be white and few pixels will be black, | |
124 | + we set the color to white and when we encounter a black pixel, | |
125 | + we temporarily change the color to black, paint the pixel and switch | |
126 | + it back to white. | |
127 | + */ | |
128 | + if (pixelArray[i][j] == BLACK){ | |
129 | + g.setColor(Color.BLACK); | |
130 | + g.drawLine(i, j, i, j); | |
131 | + g.setColor(Color.WHITE); | |
132 | + } else if (pixelArray[i][j] == WHITE){ | |
133 | + g.drawLine(i, j, i, j); | |
134 | + } | |
135 | + } | |
136 | + } | |
137 | + } | |
138 | + } | ... | ... |
... | ... | @@ -0,0 +1,56 @@ |
1 | +package tp2.ArdoiseMagique; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.*; | |
5 | +import java.awt.event.*; | |
6 | + | |
7 | +public class ArdoiseMagique extends JFrame { | |
8 | + | |
9 | + public ArdoiseMagique() { | |
10 | + super(); | |
11 | + this.setLocationRelativeTo(null); | |
12 | + | |
13 | + Ardoise ard = new Ardoise(); | |
14 | + ard.addMouseMotionListener(new MouseMotionAdapter() { | |
15 | + @Override | |
16 | + public void mouseDragged(MouseEvent e) { | |
17 | + super.mouseDragged(e); | |
18 | + Ardoise a = (Ardoise) e.getSource(); | |
19 | + if (SwingUtilities.isLeftMouseButton(e)) { | |
20 | + a.setPixel( | |
21 | + e.getX(), | |
22 | + e.getY(), | |
23 | + Ardoise.BLACK | |
24 | + ); | |
25 | + } else if (SwingUtilities.isRightMouseButton(e)) { | |
26 | + a.wipe(); | |
27 | + } | |
28 | + } | |
29 | + }); | |
30 | + | |
31 | + ard.addMouseListener(new MouseAdapter() { | |
32 | + @Override | |
33 | + public void mouseReleased(MouseEvent e) { | |
34 | + Ardoise src = (Ardoise) e.getSource(); | |
35 | + if (SwingUtilities.isLeftMouseButton(e)) { | |
36 | + src.resetOld(); | |
37 | + } else if (SwingUtilities.isRightMouseButton(e)){ | |
38 | + src.wipe(); | |
39 | + } | |
40 | + } | |
41 | + }); | |
42 | + ard.setPreferredSize(new Dimension(1000,600)); | |
43 | + this.add(ard); | |
44 | + | |
45 | + this.setResizable(false); | |
46 | + this.pack(); | |
47 | + | |
48 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
49 | + this.setVisible(true); | |
50 | + } | |
51 | + | |
52 | + public static void main(String[] args) { | |
53 | + new ArdoiseMagique(); | |
54 | + } | |
55 | + | |
56 | +} | ... | ... |
src/tp2/ArdoiseMagique/CustomMouseMotionListener.java
0 โ 100644
... | ... | @@ -0,0 +1,32 @@ |
1 | +package tp2.ArdoiseMagique; | |
2 | + | |
3 | +import java.awt.event.MouseAdapter; | |
4 | +import java.awt.event.MouseEvent; | |
5 | +import java.awt.event.MouseListener; | |
6 | + | |
7 | +public class CustomMouseMotionListener extends MouseAdapter { | |
8 | + @Override | |
9 | + public void mouseClicked(MouseEvent e) { | |
10 | + | |
11 | + } | |
12 | + | |
13 | + @Override | |
14 | + public void mousePressed(MouseEvent e) { | |
15 | + | |
16 | + } | |
17 | + | |
18 | + @Override | |
19 | + public void mouseReleased(MouseEvent e) { | |
20 | + | |
21 | + } | |
22 | + | |
23 | + @Override | |
24 | + public void mouseEntered(MouseEvent e) { | |
25 | + | |
26 | + } | |
27 | + | |
28 | + @Override | |
29 | + public void mouseExited(MouseEvent e) { | |
30 | + | |
31 | + } | |
32 | +} | ... | ... |
... | ... | @@ -0,0 +1,48 @@ |
1 | +package tp2.ColorPicker; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.*; | |
5 | + | |
6 | +public class Main { | |
7 | + | |
8 | + public static void main(String[] args) { | |
9 | + JFrame win = new JFrame(); | |
10 | + BoxLayout mainLayout = new BoxLayout(win.getContentPane(), BoxLayout.X_AXIS); | |
11 | + win.setLayout(mainLayout); | |
12 | + | |
13 | + JPanel leftPan = new JPanel(); | |
14 | + BoxLayout leftPanLayout = new BoxLayout(leftPan, BoxLayout.Y_AXIS); | |
15 | + leftPan.setLayout(leftPanLayout); | |
16 | + | |
17 | + win.add(leftPan); | |
18 | + | |
19 | + for (int i = 0; i < 3; i++) { | |
20 | + JPanel row = new JPanel(); | |
21 | + JSlider slider = new JSlider(); | |
22 | + JTextField field = new JTextField(3); | |
23 | + row.add(slider); | |
24 | + row.add(field); | |
25 | + leftPan.add(row); | |
26 | + } | |
27 | + | |
28 | + JPanel rightPane = new JPanel(); | |
29 | + BoxLayout rightPaneLayout = new BoxLayout(rightPane, BoxLayout.Y_AXIS); | |
30 | + rightPane.setLayout(rightPaneLayout); | |
31 | + | |
32 | + JTextField hexaColor = new JTextField(); | |
33 | + rightPane.add(hexaColor); | |
34 | + | |
35 | + JPanel color = new JPanel(); | |
36 | + color.setBackground(Color.RED); | |
37 | + rightPane.add(color); | |
38 | + | |
39 | + win.add(rightPane); | |
40 | + | |
41 | + win.setLocationRelativeTo(null); | |
42 | + win.setResizable(false); | |
43 | + win.pack(); | |
44 | + | |
45 | + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
46 | + win.setVisible(true); | |
47 | + } | |
48 | +} | ... | ... |
... | ... | @@ -0,0 +1,64 @@ |
1 | +package tp2.evenements; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.*; | |
5 | +import java.awt.event.ActionEvent; | |
6 | +import java.awt.event.ActionListener; | |
7 | + | |
8 | +public class FenetreIncrementer extends JFrame{ | |
9 | + private final JLabel label; | |
10 | + | |
11 | + public FenetreIncrementer() throws HeadlessException { | |
12 | + super(); | |
13 | + this.setLocationRelativeTo(null); | |
14 | + BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS); | |
15 | + this.setLayout(layout); | |
16 | + | |
17 | + this.label = new JLabel("0"); | |
18 | + label.setFont(new Font("SansSerif", Font.PLAIN, 25)); | |
19 | + | |
20 | + JPanel topPanel = new JPanel(); | |
21 | + topPanel.add(label); | |
22 | + | |
23 | + JButton incbutt = new JButton("incrรฉmenter"); | |
24 | + //question 4 | |
25 | + //utilisation des sous-classes internes | |
26 | + //incbutt.addActionListener(new ReponseAuClic()); | |
27 | + | |
28 | + incbutt.addActionListener(new ActionListener() { | |
29 | + @Override | |
30 | + public void actionPerformed(ActionEvent e) { | |
31 | + String old = label.getText(); | |
32 | + label.setText(Integer.toString(Integer.parseInt(old)+1)); | |
33 | + } | |
34 | + }); | |
35 | + | |
36 | + JPanel botPanel = new JPanel(); | |
37 | + botPanel.add(incbutt); | |
38 | + | |
39 | + this.add(topPanel); | |
40 | + this.add(botPanel); | |
41 | + | |
42 | + this.setResizable(false); | |
43 | + this.pack(); | |
44 | + | |
45 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
46 | + this.setVisible(true); | |
47 | + } | |
48 | + | |
49 | + private class ReponseAuClic implements ActionListener { | |
50 | + | |
51 | + public ReponseAuClic() { | |
52 | + } | |
53 | + | |
54 | + @Override | |
55 | + public void actionPerformed(ActionEvent e) { | |
56 | + String old = label.getText(); | |
57 | + label.setText(Integer.toString(Integer.parseInt(old)+1)); | |
58 | + } | |
59 | + } | |
60 | + | |
61 | + public static void main(String[] args) { | |
62 | + new FenetreIncrementer(); | |
63 | + } | |
64 | +} | ... | ... |
... | ... | @@ -0,0 +1,29 @@ |
1 | +package tp2.evenements; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.event.WindowAdapter; | |
5 | +import java.awt.event.WindowEvent; | |
6 | + | |
7 | +public class FermetureFenetre extends WindowAdapter { | |
8 | + @Override | |
9 | + public void windowClosing(WindowEvent e) { | |
10 | + super.windowClosing(e); | |
11 | + | |
12 | + System.out.println("Fenetre en cours de fermeture"); | |
13 | + System.exit(0); | |
14 | + } | |
15 | + | |
16 | + public static void main(String[] args) { | |
17 | + JFrame win = new JFrame(); | |
18 | + | |
19 | + win.addWindowListener(new FermetureFenetre()); | |
20 | + | |
21 | + | |
22 | + win.setLocationRelativeTo(null); | |
23 | + win.setResizable(false); | |
24 | + win.pack(); | |
25 | + | |
26 | + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
27 | + win.setVisible(true); | |
28 | + } | |
29 | +} | ... | ... |
src/tp2/exo1/MainClass.java renamed to src/tp2/evenements/MainClass.java
1 | -package tp2.exo1; | |
1 | +package tp2.evenements; | |
2 | 2 | |
3 | 3 | |
4 | 4 | import javax.swing.*; |
... | ... | @@ -6,26 +6,32 @@ import java.awt.*; |
6 | 6 | |
7 | 7 | public class MainClass { |
8 | 8 | public static void main(String[] args) { |
9 | - JFrame win = new JFrame(); | |
10 | - | |
11 | - win.setSize( 500, 400); | |
12 | - win.setLocationRelativeTo(null); | |
13 | - | |
14 | 9 | JLabel label = new JLabel("0"); |
10 | + label.setFont(new Font("SansSerif", Font.PLAIN, 25)); | |
11 | + | |
12 | + JPanel topPanel = new JPanel(); | |
13 | + topPanel.add(label); | |
15 | 14 | |
16 | - win.add(label, BorderLayout.NORTH); | |
17 | 15 | |
18 | 16 | JButton incbutt = new JButton("incrรฉmenter"); |
19 | - incbutt.addActionListener(e -> { | |
20 | - int t = Integer.parseInt(label.getText()); | |
21 | - label.setText(Integer.toString(t+1)); | |
22 | - }); | |
17 | + incbutt.addActionListener(new ReponseAuClic(label)); | |
23 | 18 | |
24 | - win.add(incbutt, BorderLayout.SOUTH); | |
19 | + JPanel botPanel = new JPanel(); | |
20 | + botPanel.add(incbutt); | |
25 | 21 | |
26 | - win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
22 | + | |
23 | + JFrame win = new JFrame(); | |
24 | + win.setLocationRelativeTo(null); | |
25 | + BoxLayout layout = new BoxLayout(win.getContentPane(), BoxLayout.Y_AXIS); | |
26 | + win.setLayout(layout); | |
27 | + | |
28 | + win.add(topPanel); | |
29 | + win.add(botPanel); | |
27 | 30 | |
28 | 31 | win.setResizable(false); |
32 | + win.pack(); | |
33 | + | |
34 | + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
29 | 35 | win.setVisible(true); |
30 | 36 | } |
31 | 37 | } | ... | ... |
... | ... | @@ -0,0 +1,23 @@ |
1 | +package tp2.evenements; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.event.ActionEvent; | |
5 | +import java.awt.event.ActionListener; | |
6 | + | |
7 | +public class ReponseAuClic implements ActionListener { | |
8 | + | |
9 | + private JLabel label; | |
10 | + | |
11 | + public ReponseAuClic(JLabel c) { | |
12 | + label = c; | |
13 | + } | |
14 | + | |
15 | + @Override | |
16 | + public void actionPerformed(ActionEvent e) { | |
17 | + //question 2 | |
18 | + //System.out.println("Clic sur le bouton"); | |
19 | + | |
20 | + String old = label.getText(); | |
21 | + label.setText(Integer.toString(Integer.parseInt(old)+1)); | |
22 | + } | |
23 | +} | ... | ... |
... | ... | @@ -0,0 +1,57 @@ |
1 | +package tp2.getSource; | |
2 | + | |
3 | +import javax.swing.*; | |
4 | +import java.awt.*; | |
5 | +import java.awt.event.ActionEvent; | |
6 | +import java.awt.event.ActionListener; | |
7 | + | |
8 | +public class MultiButt extends JFrame { | |
9 | + | |
10 | + private final JLabel label; | |
11 | + private final int nbButtons = 3; | |
12 | + | |
13 | + public MultiButt() throws HeadlessException { | |
14 | + super(); | |
15 | + this.setLocationRelativeTo(null); | |
16 | + BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS); | |
17 | + this.setLayout(layout); | |
18 | + | |
19 | + this.label = new JLabel("Bouton 0"); | |
20 | + label.setFont(new Font("SansSerif", Font.PLAIN, 25)); | |
21 | + | |
22 | + JPanel topPanel = new JPanel(); | |
23 | + topPanel.add(label); | |
24 | + this.add(topPanel); | |
25 | + | |
26 | + ActionListener listener = new ReponseAuClic2(); | |
27 | + | |
28 | + for (int i = 0;i < nbButtons; ++i){ | |
29 | + JPanel tmp = new JPanel(); | |
30 | + JButton butt = new JButton("Bouton "+Integer.toString(i)); | |
31 | + butt.addActionListener(listener); | |
32 | + tmp.add(butt); | |
33 | + this.add(tmp); | |
34 | + } | |
35 | + | |
36 | + this.setResizable(false); | |
37 | + this.pack(); | |
38 | + | |
39 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
40 | + this.setVisible(true); | |
41 | + } | |
42 | + | |
43 | + private class ReponseAuClic2 implements ActionListener { | |
44 | + | |
45 | + public ReponseAuClic2() { | |
46 | + } | |
47 | + | |
48 | + @Override | |
49 | + public void actionPerformed(ActionEvent e) { | |
50 | + label.setText(((JButton) e.getSource()).getText()); | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + public static void main(String[] args) { | |
55 | + new MultiButt(); | |
56 | + } | |
57 | +} | ... | ... |