commit 2d3cd08372017db33b690eb12314712950f0a0b0 Author: antoine.gagneux Date: Mon Feb 4 15:55:29 2019 +0100 Thread diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e461bea --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..5098060 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ExempleThread + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/ClassePrincipale.java b/src/ClassePrincipale.java new file mode 100644 index 0000000..860b472 --- /dev/null +++ b/src/ClassePrincipale.java @@ -0,0 +1,12 @@ +import ihm.MonInterface; + + +public class ClassePrincipale { + + + public static void main(String[] args) { + MonInterface mjf = new MonInterface(); + mjf.setVisible(true); + } + +} diff --git a/src/calculs/Simulateur.java b/src/calculs/Simulateur.java new file mode 100644 index 0000000..2ad53fb --- /dev/null +++ b/src/calculs/Simulateur.java @@ -0,0 +1,58 @@ +package calculs; +import ihm.MonInterface; + +import java.util.Random; + +public class Simulateur extends Thread { + + private int iPosX; + private int iPosY; + private MonInterface mjf; + private boolean stop; + + public Simulateur(MonInterface mjfParam) { + stop=false; + iPosX = 50; + iPosY = 50; + mjf = mjfParam; + } + + public int getX() { + return iPosX; + } + + public int getY() { + return iPosY; + } + + public void run() { + int iCpt=0; + while(!stop) { + iCpt++; + faireUneEtape(); + + mjf.setBlabla("Etape : "+ iCpt); + // Demande de rafraichissement de la fenetre + mjf.repaint(); + + try { + // Mise en pause pendant 150ms + Thread.sleep(150); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } + + public void faireUneEtape() { + Random r = new Random(); + iPosX = r.nextInt(mjf.getPanelDessin().getWidth()); + iPosY = r.nextInt(mjf.getPanelDessin().getHeight()); + } + + public void arret() { + stop=true; + } + +} diff --git a/src/ihm/JPanelDessin.java b/src/ihm/JPanelDessin.java new file mode 100644 index 0000000..4ae816d --- /dev/null +++ b/src/ihm/JPanelDessin.java @@ -0,0 +1,37 @@ +package ihm; + +import java.awt.Color; +import java.awt.Graphics; + +import javax.swing.JPanel; + +import calculs.Simulateur; + +public class JPanelDessin extends JPanel { + + private static final long serialVersionUID = 1L; + private Simulateur monCalc; + + public JPanelDessin() { + super(); + monCalc = null; + } + + public void setCalcul(Simulateur monCalcParam) { + monCalc = monCalcParam; + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + this.setBackground(Color.white); + // si monCalc n'a pas été affecté (avant le set) il ne faut rien afficher + if (monCalc != null) { + // Ecriture du rectangle en position x,y de la classe calcul + g.drawRect(monCalc.getX(), monCalc.getY(), 10, 10); + + } + + } + +} diff --git a/src/ihm/MonInterface.java b/src/ihm/MonInterface.java new file mode 100644 index 0000000..7857b4a --- /dev/null +++ b/src/ihm/MonInterface.java @@ -0,0 +1,85 @@ +package ihm; +import java.awt.BorderLayout; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JButton; +import javax.swing.JLabel; + +import calculs.Simulateur; + +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +public class MonInterface extends JFrame { + + private static final long serialVersionUID = -6840815447618468846L; + private JPanel contentPane; + private JLabel lblNewLabel; + private JPanelDessin panel_1; + private Simulateur monCalc=null; + private JButton btnStop; + + /** + * Create the frame. + */ + public MonInterface() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 300); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + contentPane.setLayout(new BorderLayout(0, 0)); + setContentPane(contentPane); + + JPanel panel = new JPanel(); + contentPane.add(panel, BorderLayout.NORTH); + + JButton btnGo = new JButton("Go"); + btnGo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + clicBoutonGo(); + } + }); + panel.add(btnGo); + + lblNewLabel = new JLabel(" "); + panel.add(lblNewLabel); + + btnStop = new JButton("Stop"); + btnStop.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + clicBoutonStop(); + } + }); + panel.add(btnStop); + + panel_1 = new JPanelDessin(); + contentPane.add(panel_1, BorderLayout.CENTER); + } + + public void setBlabla(String s) { + lblNewLabel.setText(s); + } + + public JPanelDessin getPanelDessin() { + return panel_1; + } + + public void clicBoutonGo() { + if (monCalc==null) { + monCalc = new Simulateur(this); + panel_1.setCalcul(monCalc); + monCalc.start(); + } + } + public void clicBoutonStop() { + if (monCalc!=null) { + panel_1.setCalcul(null); + monCalc.arret(); + monCalc=null; + panel_1.repaint(); + } + } + +}