import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Panel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.event.ChangeEvent;
//devlopper par mzili toufik
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
public class dis extends JFrame{
dis(){
this.setBounds(100, 100, 450, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan panel = new pan();
getContentPane().add(panel, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Sens");
menuBar.add(mnNewMenu);
JMenuItem mntmADroite = new JMenuItem("A droite");
mntmADroite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.Sens="dr";
}
});
mnNewMenu.add(mntmADroite);
JMenuItem mntmAgouche = new JMenuItem("Agouche");
mntmAgouche.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.Sens="gch";
}
});
mnNewMenu.add(mntmAgouche);
JMenu mnAction = new JMenu("Action");
menuBar.add(mnAction);
JMenuItem mntmPause = new JMenuItem("Pause");
mntmPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.stop();
}
});
mnAction.add(mntmPause);
JMenuItem mntmStart = new JMenuItem("Start");
mntmStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.restart();
}
});
mnAction.add(mntmStart);
JMenu mnForme = new JMenu("Forme");
menuBar.add(mnForme);
JMenuItem mntmRectangle = new JMenuItem("Rectangle");
mnForme.add(mntmRectangle);
JMenuItem mntmCercle = new JMenuItem("cercle");
//devlopper par mzili toufik
mntmCercle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.type="Cercle";
}
});
mnForme.add(mntmCercle);
JMenu mnVitesse = new JMenu("Vitesse");
menuBar.add(mnVitesse);
JMenuItem mntmLente = new JMenuItem("lente");
//devlopper par mzili toufik
mntmLente.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.setDelay(50);;
}
});
mnVitesse.add(mntmLente);
JMenuItem mntmMoyenne = new JMenuItem("moyenne");
//devlopper par mzili toufik
mntmMoyenne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.setDelay(30);;
}
});
mnVitesse.add(mntmMoyenne);
JMenuItem mntmRapide = new JMenuItem("rapide");
//devlopper par mzili toufik
mntmRapide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.setDelay(3);;
}
});
mnVitesse.add(mntmRapide);
mntmRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.type="rect";
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new dis();
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Panel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.event.ChangeEvent;
//devlopper par mzili toufik
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
public class dis extends JFrame{
dis(){
this.setBounds(100, 100, 450, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan panel = new pan();
getContentPane().add(panel, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Sens");
menuBar.add(mnNewMenu);
JMenuItem mntmADroite = new JMenuItem("A droite");
mntmADroite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.Sens="dr";
}
});
mnNewMenu.add(mntmADroite);
JMenuItem mntmAgouche = new JMenuItem("Agouche");
mntmAgouche.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.Sens="gch";
}
});
mnNewMenu.add(mntmAgouche);
JMenu mnAction = new JMenu("Action");
menuBar.add(mnAction);
JMenuItem mntmPause = new JMenuItem("Pause");
mntmPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.stop();
}
});
mnAction.add(mntmPause);
JMenuItem mntmStart = new JMenuItem("Start");
mntmStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.restart();
}
});
mnAction.add(mntmStart);
JMenu mnForme = new JMenu("Forme");
menuBar.add(mnForme);
JMenuItem mntmRectangle = new JMenuItem("Rectangle");
mnForme.add(mntmRectangle);
JMenuItem mntmCercle = new JMenuItem("cercle");
//devlopper par mzili toufik
mntmCercle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.type="Cercle";
}
});
mnForme.add(mntmCercle);
JMenu mnVitesse = new JMenu("Vitesse");
menuBar.add(mnVitesse);
JMenuItem mntmLente = new JMenuItem("lente");
//devlopper par mzili toufik
mntmLente.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.setDelay(50);;
}
});
mnVitesse.add(mntmLente);
JMenuItem mntmMoyenne = new JMenuItem("moyenne");
//devlopper par mzili toufik
mntmMoyenne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.setDelay(30);;
}
});
mnVitesse.add(mntmMoyenne);
JMenuItem mntmRapide = new JMenuItem("rapide");
//devlopper par mzili toufik
mntmRapide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.timer.setDelay(3);;
}
});
mnVitesse.add(mntmRapide);
mntmRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.type="rect";
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new dis();
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
//devlopper par mzili toufik
//devlopper par mzili toufik
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class pan extends JPanel {
int x=0,y=0,wi=20,he=30;
boolean t=true;
String type="Cercle";
Timer timer;
String Sens="dr";
private float degrees = 0;
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public pan() {
timer = new Timer(13, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Sens=="dr")
degrees+=2f;
if(Sens=="gch")
degrees-=2f;
if(wi>getWidth() ||he>getHeight()) t=false;
if(wi==0||he==0) t=true;
if(t==true) {
wi++;
he++;}
if(t==false) {
wi--;
he--;}
x =(getWidth()-wi)/2;
y=(getHeight()-he)/2;
repaint();
}
});
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
if(type=="Cercle") g2d.fillOval(x,y,Math.min(wi, he),Math.min(wi, he));
else g2d.fillRect(x,y,Math.min(wi, he),Math.min(wi, he));
g2d.setColor(Color.RED);
float innerDiameter = 17;
Point p = getPointOnCircle(degrees, Math.min(wi, he)/ 2f );
g2d.fillOval( p.x-x-(int) innerDiameter+4,p.y-y-(int) innerDiameter-5, (int) innerDiameter, (int) innerDiameter);
g2d.dispose();
}
protected Point getPointOnCircle(float degress, float radius) {
int x = Math.round(getWidth()-Math.min(wi, he) / 2);
int y = Math.round(getHeight()-Math.min(wi, he)/ 2);
double rads = Math.toRadians(degress - 190); // 0 becomes the top
// Calculate the outter point of the line
int xPosy = Math.round((float) (x + Math.cos(rads) * radius));
int yPosy = Math.round((float) (y + Math.sin(rads) * radius));
return new Point(xPosy, yPosy);
}
}
Commentaires
Enregistrer un commentaire