diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d2aea5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +out/* +*.class +*.tar +*.iml +.idea/* +.idea \ No newline at end of file diff --git a/src/tp2/ArdoiseMagique/Ardoise.java b/src/tp2/ArdoiseMagique/Ardoise.java new file mode 100644 index 0000000..1492247 --- /dev/null +++ b/src/tp2/ArdoiseMagique/Ardoise.java @@ -0,0 +1,138 @@ +package tp2.ArdoiseMagique; + +import javax.swing.*; +import java.awt.*; + +public class Ardoise extends JPanel { + private final int defaultWidth = 1000; + private final int defaultHeight = 600; + protected static final int BLACK = 1; + protected static final int WHITE = 0; + + private int oldx = -1; + private int oldy = -1; + + private final int[][] pixelArray = new int[defaultWidth][defaultHeight]; + + public Ardoise() { + super(); + this.setSize(defaultWidth, defaultHeight); + + wipe(); + } + + public void wipe() { + for (int i = 0; i < defaultWidth; ++i){ + for (int j = 0; j < defaultHeight; ++j){ + pixelArray[i][j] = WHITE; + } + } + + repaint(); + } + + public void setPixel(int x, int y, int color){ + if (x < 0 || y < 0 || x >= defaultWidth || y >= defaultHeight){ + return; + } + + if (oldx == -1 || oldy == -1){ + oldx = x; + oldy = y; + } + + plotLine(oldx, oldy, x, y); + + oldx = x; + oldy = y; + + System.out.println("tried to put a "+(color==BLACK?"black":"white")+" pixel at "+Integer.toString(x)+" "+Integer.toString(x)); + pixelArray[x][y] = color; + repaint(); + } + + public void plotLineLow(int x0, int y0, int x1, int y1){ + int dx = x1 - x0; + int dy = y1 - y0; + int yi = 1; + if (dy < 0) { + yi = -1; + dy = -dy; + } + int D = (2 * dy) - dx; + int ay = y0; + + for (int ax = x0; ax < x1; ++ax) { + pixelArray[ax][ay] = BLACK; + if (D > 0) { + ay = ay + yi; + D = D + (2 * (dy - dx)); + } else D = D + 2 * dy; + } + } + + public void plotLineHigh(int x0, int y0, int x1, int y1){ + int dx = x1 - x0; + int dy = y1 - y0; + int xi = 1; + if (dx < 0) { + xi = -1; + dx = -dx; + } + int D = (2 * dx) - dy; + int ax = x0; + + for (int ay = y0; ay < y1; ++ay) { + pixelArray[ax][ay] = BLACK; + if (D > 0) { + ax = ax + xi; + D = D + (2 * (dx - dy)); + } else D = D + 2 * dx; + } + } + + public void plotLine(int x0, int y0, int x1, int y1) { + if (Math.abs(y1 - y0) < Math.abs(x1 - x0)) { + if (x0 > x1) { + plotLineLow(x1, y1, x0, y0); + } else { + plotLineLow(x0, y0, x1, y1); + } + } else { + if (y0 > y1) { + plotLineHigh(x1, y1, x0, y0); + } else { + plotLineHigh(x0, y0, x1, y1); + } + } + } + + public void resetOld() { + oldx = -1; + oldy = -1; + } + + @Override + public void paintComponent(Graphics g){ + super.paintComponent(g); + + g.setColor(Color.WHITE); + for (int i = 0; i < defaultWidth; ++i){ + for (int j = 0; j < defaultHeight; ++j){ + /* + since most of the image will be white and few pixels will be black, + we set the color to white and when we encounter a black pixel, + we temporarily change the color to black, paint the pixel and switch + it back to white. + */ + if (pixelArray[i][j] == BLACK){ + g.setColor(Color.BLACK); + g.drawLine(i, j, i, j); + g.setColor(Color.WHITE); + } else if (pixelArray[i][j] == WHITE){ + g.drawLine(i, j, i, j); + } + } + } + } + } diff --git a/src/tp2/ArdoiseMagique/ArdoiseMagique.java b/src/tp2/ArdoiseMagique/ArdoiseMagique.java new file mode 100644 index 0000000..6306416 --- /dev/null +++ b/src/tp2/ArdoiseMagique/ArdoiseMagique.java @@ -0,0 +1,56 @@ +package tp2.ArdoiseMagique; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class ArdoiseMagique extends JFrame { + + public ArdoiseMagique() { + super(); + this.setLocationRelativeTo(null); + + Ardoise ard = new Ardoise(); + ard.addMouseMotionListener(new MouseMotionAdapter() { + @Override + public void mouseDragged(MouseEvent e) { + super.mouseDragged(e); + Ardoise a = (Ardoise) e.getSource(); + if (SwingUtilities.isLeftMouseButton(e)) { + a.setPixel( + e.getX(), + e.getY(), + Ardoise.BLACK + ); + } else if (SwingUtilities.isRightMouseButton(e)) { + a.wipe(); + } + } + }); + + ard.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + Ardoise src = (Ardoise) e.getSource(); + if (SwingUtilities.isLeftMouseButton(e)) { + src.resetOld(); + } else if (SwingUtilities.isRightMouseButton(e)){ + src.wipe(); + } + } + }); + ard.setPreferredSize(new Dimension(1000,600)); + this.add(ard); + + this.setResizable(false); + this.pack(); + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + } + + public static void main(String[] args) { + new ArdoiseMagique(); + } + +} diff --git a/src/tp2/ArdoiseMagique/CustomMouseMotionListener.java b/src/tp2/ArdoiseMagique/CustomMouseMotionListener.java new file mode 100644 index 0000000..c69f7f6 --- /dev/null +++ b/src/tp2/ArdoiseMagique/CustomMouseMotionListener.java @@ -0,0 +1,32 @@ +package tp2.ArdoiseMagique; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +public class CustomMouseMotionListener extends MouseAdapter { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } +} diff --git a/src/tp2/ColorPicker/Main.java b/src/tp2/ColorPicker/Main.java new file mode 100644 index 0000000..7b3da3e --- /dev/null +++ b/src/tp2/ColorPicker/Main.java @@ -0,0 +1,48 @@ +package tp2.ColorPicker; + +import javax.swing.*; +import java.awt.*; + +public class Main { + + public static void main(String[] args) { + JFrame win = new JFrame(); + BoxLayout mainLayout = new BoxLayout(win.getContentPane(), BoxLayout.X_AXIS); + win.setLayout(mainLayout); + + JPanel leftPan = new JPanel(); + BoxLayout leftPanLayout = new BoxLayout(leftPan, BoxLayout.Y_AXIS); + leftPan.setLayout(leftPanLayout); + + win.add(leftPan); + + for (int i = 0; i < 3; i++) { + JPanel row = new JPanel(); + JSlider slider = new JSlider(); + JTextField field = new JTextField(3); + row.add(slider); + row.add(field); + leftPan.add(row); + } + + JPanel rightPane = new JPanel(); + BoxLayout rightPaneLayout = new BoxLayout(rightPane, BoxLayout.Y_AXIS); + rightPane.setLayout(rightPaneLayout); + + JTextField hexaColor = new JTextField(); + rightPane.add(hexaColor); + + JPanel color = new JPanel(); + color.setBackground(Color.RED); + rightPane.add(color); + + win.add(rightPane); + + win.setLocationRelativeTo(null); + win.setResizable(false); + win.pack(); + + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + win.setVisible(true); + } +} diff --git a/src/tp2/evenements/FenetreIncrementer.java b/src/tp2/evenements/FenetreIncrementer.java new file mode 100644 index 0000000..c32973a --- /dev/null +++ b/src/tp2/evenements/FenetreIncrementer.java @@ -0,0 +1,64 @@ +package tp2.evenements; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class FenetreIncrementer extends JFrame{ + private final JLabel label; + + public FenetreIncrementer() throws HeadlessException { + super(); + this.setLocationRelativeTo(null); + BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS); + this.setLayout(layout); + + this.label = new JLabel("0"); + label.setFont(new Font("SansSerif", Font.PLAIN, 25)); + + JPanel topPanel = new JPanel(); + topPanel.add(label); + + JButton incbutt = new JButton("incrémenter"); + //question 4 + //utilisation des sous-classes internes + //incbutt.addActionListener(new ReponseAuClic()); + + incbutt.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String old = label.getText(); + label.setText(Integer.toString(Integer.parseInt(old)+1)); + } + }); + + JPanel botPanel = new JPanel(); + botPanel.add(incbutt); + + this.add(topPanel); + this.add(botPanel); + + this.setResizable(false); + this.pack(); + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + } + + private class ReponseAuClic implements ActionListener { + + public ReponseAuClic() { + } + + @Override + public void actionPerformed(ActionEvent e) { + String old = label.getText(); + label.setText(Integer.toString(Integer.parseInt(old)+1)); + } + } + + public static void main(String[] args) { + new FenetreIncrementer(); + } +} diff --git a/src/tp2/evenements/FermetureFenetre.java b/src/tp2/evenements/FermetureFenetre.java new file mode 100644 index 0000000..a23e377 --- /dev/null +++ b/src/tp2/evenements/FermetureFenetre.java @@ -0,0 +1,29 @@ +package tp2.evenements; + +import javax.swing.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +public class FermetureFenetre extends WindowAdapter { + @Override + public void windowClosing(WindowEvent e) { + super.windowClosing(e); + + System.out.println("Fenetre en cours de fermeture"); + System.exit(0); + } + + public static void main(String[] args) { + JFrame win = new JFrame(); + + win.addWindowListener(new FermetureFenetre()); + + + win.setLocationRelativeTo(null); + win.setResizable(false); + win.pack(); + + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + win.setVisible(true); + } +} diff --git a/src/tp2/evenements/MainClass.java b/src/tp2/evenements/MainClass.java new file mode 100644 index 0000000..df37c00 --- /dev/null +++ b/src/tp2/evenements/MainClass.java @@ -0,0 +1,37 @@ +package tp2.evenements; + + +import javax.swing.*; +import java.awt.*; + +public class MainClass { + public static void main(String[] args) { + JLabel label = new JLabel("0"); + label.setFont(new Font("SansSerif", Font.PLAIN, 25)); + + JPanel topPanel = new JPanel(); + topPanel.add(label); + + + JButton incbutt = new JButton("incrémenter"); + incbutt.addActionListener(new ReponseAuClic(label)); + + JPanel botPanel = new JPanel(); + botPanel.add(incbutt); + + + JFrame win = new JFrame(); + win.setLocationRelativeTo(null); + BoxLayout layout = new BoxLayout(win.getContentPane(), BoxLayout.Y_AXIS); + win.setLayout(layout); + + win.add(topPanel); + win.add(botPanel); + + win.setResizable(false); + win.pack(); + + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + win.setVisible(true); + } +} diff --git a/src/tp2/evenements/ReponseAuClic.java b/src/tp2/evenements/ReponseAuClic.java new file mode 100644 index 0000000..3497d86 --- /dev/null +++ b/src/tp2/evenements/ReponseAuClic.java @@ -0,0 +1,23 @@ +package tp2.evenements; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ReponseAuClic implements ActionListener { + + private JLabel label; + + public ReponseAuClic(JLabel c) { + label = c; + } + + @Override + public void actionPerformed(ActionEvent e) { + //question 2 + //System.out.println("Clic sur le bouton"); + + String old = label.getText(); + label.setText(Integer.toString(Integer.parseInt(old)+1)); + } +} diff --git a/src/tp2/exo1/MainClass.java b/src/tp2/exo1/MainClass.java deleted file mode 100644 index 55885b6..0000000 --- a/src/tp2/exo1/MainClass.java +++ /dev/null @@ -1,31 +0,0 @@ -package tp2.exo1; - - -import javax.swing.*; -import java.awt.*; - -public class MainClass { - public static void main(String[] args) { - JFrame win = new JFrame(); - - win.setSize( 500, 400); - win.setLocationRelativeTo(null); - - JLabel label = new JLabel("0"); - - win.add(label, BorderLayout.NORTH); - - JButton incbutt = new JButton("incrémenter"); - incbutt.addActionListener(e -> { - int t = Integer.parseInt(label.getText()); - label.setText(Integer.toString(t+1)); - }); - - win.add(incbutt, BorderLayout.SOUTH); - - win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - - win.setResizable(false); - win.setVisible(true); - } -} diff --git a/src/tp2/getSource/MultiButt.java b/src/tp2/getSource/MultiButt.java new file mode 100644 index 0000000..059ff14 --- /dev/null +++ b/src/tp2/getSource/MultiButt.java @@ -0,0 +1,57 @@ +package tp2.getSource; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class MultiButt extends JFrame { + + private final JLabel label; + private final int nbButtons = 3; + + public MultiButt() throws HeadlessException { + super(); + this.setLocationRelativeTo(null); + BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS); + this.setLayout(layout); + + this.label = new JLabel("Bouton 0"); + label.setFont(new Font("SansSerif", Font.PLAIN, 25)); + + JPanel topPanel = new JPanel(); + topPanel.add(label); + this.add(topPanel); + + ActionListener listener = new ReponseAuClic2(); + + for (int i = 0;i < nbButtons; ++i){ + JPanel tmp = new JPanel(); + JButton butt = new JButton("Bouton "+Integer.toString(i)); + butt.addActionListener(listener); + tmp.add(butt); + this.add(tmp); + } + + this.setResizable(false); + this.pack(); + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + } + + private class ReponseAuClic2 implements ActionListener { + + public ReponseAuClic2() { + } + + @Override + public void actionPerformed(ActionEvent e) { + label.setText(((JButton) e.getSource()).getText()); + } + } + + public static void main(String[] args) { + new MultiButt(); + } +} -- libgit2 0.21.2