mbl | Merci bien pour toutes ces indications. Au final un pote javiateur m'a aidé et donc finalement on est passé par le java. ça marche pas trop mal même si je pense qu'il faudrait faire 2 - 3 retouches et qu'il lui arrive de planter de temps en temps... Si ça intéresse des gens :
Code :
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.table.AbstractTableModel;
- public class BringItOn {
- private static final class M extends AbstractTableModel {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private final int[] counts = new int[NAMES.size()];
- private final float[] value = new float[NAMES.size()];
- private final JLabel lbl;
- private final int[] w;
- {
- value[NAMES.indexOf(SO)] = 0;
- value[NAMES.indexOf(BI)] = 4;
- value[NAMES.indexOf(BO)] = 20;
- value[NAMES.indexOf(CHP)] = 12.5f;
- }
- public M(JLabel lbl, int[] w) {
- this.lbl = lbl;
- this.w = w;
- }
- @Override
- public Class<?> getColumnClass(int columnIndex) {
- return String.class;
- }
- @Override
- public Object getValueAt(int rowIndex, int columnIndex) {
- if (columnIndex == 0) {
- return NAMES.get(rowIndex);
- } else if (columnIndex == 1) {
- return counts[rowIndex];
- } else if (columnIndex == 2) {
- return value[rowIndex];
- }
- return null;
- }
- @Override
- public String getColumnName(int columnIndex) {
- if (columnIndex == 0) {
- return "Type";
- } else if (columnIndex == 1) {
- return "Nombre";
- } else if (columnIndex == 2) {
- return "%";
- }
- return "";
- }
- @Override
- public int getRowCount() {
- return NAMES.size();
- }
- @Override
- public int getColumnCount() {
- return 3;
- }
- @Override
- public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
- if (columnIndex == 1) {
- counts[rowIndex] = Integer.parseInt(aValue.toString());
- if (rowIndex == NAMES.indexOf(BI)) {
- if (counts[rowIndex] % 6 != 0) {
- JOptionPane.showMessageDialog(null, "Tocard, les " + BI
- + " ca va toujours par 6 !" );
- counts[rowIndex] = 0;
- }
- }
- } else if (columnIndex == 2) {
- value[rowIndex] = Float.parseFloat(aValue.toString());
- }
- updateResult();
- }
- public void updateResult() {
- float computeValue = compute();
- boolean isOK = isOk(computeValue);
- if (isOK) {
- lbl.setForeground(Color.GREEN);
- lbl.setText("Suit up ! Tu pèses maintenant" + computeValue );
- } else {
- lbl.setForeground(Color.RED);
- lbl.setText("Bring more (" + computeValue + " < " + w[0] + " )" );
- }
- System.err.println(computeValue);
- }
- public boolean isOk(float computeValue) {
- boolean isOK = computeValue >= w[0];
- return isOK;
- }
- public float compute() {
- float bi = counts[NAMES.indexOf(BI)] * value[NAMES.indexOf(BI)];
- float bo = counts[NAMES.indexOf(BO)] * value[NAMES.indexOf(BO)];
- float chp = counts[NAMES.indexOf(CHP)] * value[NAMES.indexOf(CHP)]
- * 2;
- float so = counts[NAMES.indexOf(SO)] * 10;
- float computeValue = bi + bo + chp + so;
- return computeValue;
- }
- @Override
- public boolean isCellEditable(int rowIndex, int columnIndex) {
- if (columnIndex == 0)
- return false;
- else if (columnIndex == 1)
- return true;
- else if (columnIndex == 2
- && (getValueAt(0, columnIndex).equals(SO)))
- return false;
- return true;
- }
- }
- private static final String CHP = "Champagne";
- private static final String SO = "Soft Drink";
- private static final String BI = "Binouze";
- private static final String BO = "Alcool";
- private static final List<String> NAMES = new ArrayList<String>();
- static {
- NAMES.addAll(Arrays.asList(CHP, BI, BO, SO));
- }
- public static void main(String[] args) {
- final int[] w = new int[1];
- final JFrame f = new JFrame();
- final JLabel lbl = new JLabel();
- final M m = new M(lbl, w);
- final JTextField weight = new JTextField("0" );
- weight.setHorizontalAlignment(JLabel.CENTER);
- weight.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- w[0] = Integer.parseInt(weight.getText());
- for(int i =0; i < NAMES.size();i++){
- m.setValueAt(0, i, 1);
- }
- if (w[0] <= 65) {
- m.setValueAt(1, NAMES.indexOf(BO), 1);
- m.setValueAt(20, NAMES.indexOf(BO), 2);
- } else {
- m.setValueAt(1, NAMES.indexOf(BO), 1);
- m.setValueAt(40, NAMES.indexOf(BO), 2);
- }
- while (!m.isOk(m.compute())) {
- float computeValue = m.compute();
- if (w[0] - computeValue > 25) {
- if (Math.random() < 0.12) {
- m.setValueAt(1 + Integer.parseInt(m
- .getValueAt(NAMES.indexOf(CHP), 1)
- .toString()), NAMES.indexOf(CHP), 1);
- } else if (Math.random() < 0.66) {
- m.setValueAt(1 + Integer.parseInt(m
- .getValueAt(NAMES.indexOf(BO), 1)
- .toString()), NAMES.indexOf(BO), 1);
- if (w[0] > 65) {
- m.setValueAt(40, NAMES.indexOf(BO), 2);
- } else
- m.setValueAt(20, NAMES.indexOf(BO), 2);
- } else {
- m.setValueAt(6 + Integer.parseInt(m
- .getValueAt(NAMES.indexOf(BI), 1)
- .toString()), NAMES.indexOf(BI), 1);
- }
- } else {
- m.setValueAt(1 + Integer.parseInt(m.getValueAt(
- NAMES.indexOf(SO), 1).toString()),
- NAMES.indexOf(SO), 1);
- }
- }
- m.updateResult();
- } catch (Exception ex) {
- ex.printStackTrace();
- JOptionPane.showMessageDialog(f,
- "Tocard, c'est ton poid en kg qu'il faut rentrer" );
- weight.setText("75" );
- }
- m.fireTableDataChanged();
- }
- });
- weight.setText("0" );
- f.add(weight, BorderLayout.NORTH);
- f.add(lbl, BorderLayout.SOUTH);
- lbl.setHorizontalAlignment(JLabel.CENTER);
- JTable t = new JTable(m);
- f.add(new JScrollPane(t), BorderLayout.CENTER);
- t.setBackground(Color.WHITE);
- m.updateResult();
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setSize(new Dimension(500, 500));
- f.setVisible(true);
- }
- }
|
---------------
-3dB
|