Thread
This commit is contained in:
commit
2d3cd08372
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
@ -0,0 +1 @@
|
|||
/bin/
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>ExempleThread</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import ihm.MonInterface;
|
||||
|
||||
|
||||
public class ClassePrincipale {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
MonInterface mjf = new MonInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue