Commit 52d5e85d authored by Lucas PAGANO's avatar Lucas PAGANO
Browse files

correction ds 2016 java

parent ad6a79d2
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mouseMotion</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>
position = position
init = initialisation
helpbutton = initialisation de la position à (0,0)
title = PolyMouvement
plus = Plus
minus = Moins
menu = Action
\ No newline at end of file
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.AbstractAction;
import javax.swing.Action;
public class Controler {
public Vue vue;
private Model model;
public Controler(Model model){
this.model = model;
this.vue = new Vue();
ActionInit actionInit = new ActionInit();
UpdatePosition updatePosition = new UpdatePosition();
MotionListener mouseListener = new MotionListener();
this.vue.buttonInit.setAction(actionInit);
this.model.addObserver(updatePosition);
this.vue.panelMove.addMouseMotionListener(mouseListener);
}
public class ActionInit extends AbstractAction {
public ActionInit(){
super(I18n.INIT);
this.putValue(Action.SHORT_DESCRIPTION, I18n.resourceBundle.getString(I18n.HELPBUTTON));
}
@Override
public void actionPerformed(ActionEvent arg0) {
model.setPoint(new Point(0,0));
}
}
public class UpdatePosition implements Observer {
@Override
public void update(Observable arg0, Object arg1) {
vue.textField.setText(model.toString());
}
}
public class MotionListener implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
model.setPoint(arg0.getPoint());
}
}
}
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.AbstractAction;
public class ControlerGenerale {
private List<Model> models;
private VueGenerale vueGenerale;
public ControlerGenerale(List<Model> models){
this.models = models;
this.vueGenerale = new VueGenerale();
Controler controler;
for(Model model : this.models){
controler = new Controler(model);
this.vueGenerale.addVue(controler.vue);
}
ActionPlus actionPlus = new ActionPlus();
ActionMoins actionMoins = new ActionMoins();
UpdatePosition updatePosition = new UpdatePosition();
this.vueGenerale.menu.add(actionPlus);
this.vueGenerale.menu.add(actionMoins);
this.vueGenerale.buttonPlus.setAction(actionPlus);
this.vueGenerale.buttonMinus.setAction(actionMoins);
this.vueGenerale.frame.pack();
for(Model model: this.models){
model.addObserver(updatePosition);
model.init();
}
}
public class ActionPlus extends AbstractAction {
public ActionPlus(){
super(I18n.resourceBundle.getString(I18n.PLUS));
//this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK));
}
@Override
public void actionPerformed(ActionEvent arg0) {
for(Model model: models){
model.setPoint(new Point(model.getPoint().x+10, model.getPoint().y+10));
}
}
}
public class ActionMoins extends AbstractAction {
public ActionMoins(){
super(I18n.resourceBundle.getString(I18n.MINUS));
//this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK));
}
@Override
public void actionPerformed(ActionEvent arg0) {
for(Model model: models){
model.setPoint(new Point(model.getPoint().x-10, model.getPoint().y-10));
}
}
}
public class UpdatePosition implements Observer {
@Override
public void update(Observable o, Object arg) {
Point average = new Point();
Point pointModel = null;
for(Model model : models){
pointModel = model.getPoint();
average.x += pointModel.x;
average.y += pointModel.y;
}
average.x = (int)((float)average.x) / models.size();
average.y = (int)((float)average.y) / models.size();
vueGenerale.textField.setText("("+average.x+", "+average.y+")");
}
}
}
import java.util.Locale;
import java.util.ResourceBundle;
public interface I18n {
String POSITION = "position";
String INIT = "init";
String HELPBUTTON = "helpbutton";
String TITLE = "title";
String PLUS = "plus";
String MINUS = "minus";
String MENU = "menu";
ResourceBundle resourceBundle = ResourceBundle.getBundle("correction.examTP1516.mouseMotion.message");
}
import java.awt.Point;
import java.util.Observable;
public class Model extends Observable {
private Point point;
public Model(){
this.point = new Point();
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
if(this.point.x != point.x || this.point.y != point.y){
this.point = point;
this.setChanged();
}
this.notifyObservers();
}
@Override
public String toString() {
return "("+this.point.x+", "+this.point.y+")";
}
public void init(){
this.setChanged();
this.notifyObservers();
}
}
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Model> models = new ArrayList<>();
for(int i = 0; i< 4; i++){
models.add(new Model());
}
new ControlerGenerale(models);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Vue {
public JPanel panelContainer;
public JPanel panelMove;
public JLabel labelPosition;
public JTextField textField;
public JButton buttonInit;
public Vue(){
this.panelContainer = new JPanel();
this.panelMove = new JPanel();
this.labelPosition = new JLabel(I18n.resourceBundle.getString(I18n.POSITION));
this.textField = new JTextField();
this.buttonInit = new JButton();
this.panelContainer.setLayout(new BoxLayout(this.panelContainer, BoxLayout.Y_AXIS));
this.panelContainer.setBorder(BorderFactory.createEtchedBorder());
this.textField.setColumns(10);
this.panelMove.setPreferredSize(new Dimension(100, 100));
this.panelMove.setBackground(Color.gray);
this.panelContainer.add(panelMove);
JPanel panel = new JPanel();
panel.add(labelPosition);
panel.add(textField);
this.panelContainer.add(panel);
this.panelContainer.add(buttonInit);
}
}
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class VueGenerale {
public JFrame frame;
public JPanel panelCenter;
public JMenu menu;
public JPanel panelLeft;
public JButton buttonPlus;
public JButton buttonMinus;
public JTextField textField;
public JLabel label;
public VueGenerale(){
this.frame = new JFrame();
this.frame.setTitle(I18n.resourceBundle.getString(I18n.TITLE));
this.panelCenter = new JPanel();
this.panelLeft = new JPanel();
this.buttonMinus = new JButton();
this.buttonPlus = new JButton();
this.label = new JLabel(I18n.resourceBundle.getString(I18n.POSITION));
this.textField = new JTextField();
this.panelLeft.setLayout(new BoxLayout(this.panelLeft, BoxLayout.Y_AXIS));
this.panelLeft.setBorder(BorderFactory.createEtchedBorder());
this.textField.setColumns(10);
JPanel panel = new JPanel();
panel.add(this.buttonPlus);
panel.add(this.buttonMinus);
JPanel panel2 = new JPanel();
panel2.add(this.label);
panel2.add(this.textField);
this.panelLeft.add(panel);
this.panelLeft.add(panel2);
this.panelCenter.add(this.panelLeft);
this.frame.add(this.panelCenter);
JMenuBar menuBar = new JMenuBar();
this.menu = new JMenu(I18n.resourceBundle.getString(I18n.MENU));
menuBar.add(this.menu);
this.frame.setJMenuBar(menuBar);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setVisible(true);
}
public void addVue(Vue vue){
this.panelCenter.add(vue.panelContainer);
}
}
position = position
init = initialization
helpbutton = initialization of the position to (0,0)
title = PolyMove
plus = Plus
minus = Minus
menu = Action
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment