1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import javax.swing.*;
import javax.swing.border.Border;
import Mobile.*;
public class Hmi extends JPanel{
/**
*
*/
//private static final long serialVersionUID = 1L;
private final static int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
private final static int HEIGHT = (int) (Toolkit.getDefaultToolkit().getScreenSize().height * 0.9);
public final static int Nbr_Ligne = 40;
public final static int Nbr_Cologne = 95;
public final static int Nbr_Rail = 14;
public final static int Decalage_RailTour =21;
static Timer t;
//connexion a la base de donnée
static String url = "jdbc:mysql://localhost:3306/magmatic";// URL de connexion
static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
static SGBD db = new SGBD(driver);
static Connection con = null;
public Hmi(){
con = db.open(url, "root", "admcira");
System.out.println("bonne connexion");
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
int x = 0;
int y = 0;
for(int i=0; i< Nbr_Rail ; i++){
for(int j=0; j< Nbr_Cologne ; j++){
if(j == Decalage_RailTour)
g.setColor(Color.RED);
else
g.setColor(Color.WHITE);
g.fillRect(x,y,WIDTH / Nbr_Cologne, HEIGHT / Nbr_Ligne);
// g.setColor(Color.MAGENTA);
// g.drawString("" + (j - 210), x + ((WIDTH / Nbr_Cologne) / 2) - 5, y + ((HEIGHT / Nbr_Ligne) / 2) + 3);
g.setColor(Color.black);
g.drawLine(x, 0, x, HEIGHT + (HEIGHT / Nbr_Ligne));
g.setColor(Color.WHITE);
x += WIDTH / Nbr_Cologne;
}
g.setColor(Color.black);
g.drawLine(0, y, WIDTH, y);
g.setColor(Color.WHITE);
y += HEIGHT / Nbr_Ligne;
x = 0;
for(int j=0; j< Nbr_Cologne ; j++){
if(j == Decalage_RailTour)
g.setColor(Color.RED);
else
g.setColor(Color.GRAY);
g.fillRect(x,y,WIDTH / Nbr_Cologne, HEIGHT / Nbr_Ligne);
// g.setColor(Color.MAGENTA);
// g.drawString("" + (j - 210), x + ((WIDTH / Nbr_Cologne) / 2) - 5, y + ((HEIGHT / Nbr_Ligne) / 2) + 3);
g.setColor(Color.black);
g.drawLine(x, 0, x, HEIGHT + (HEIGHT / Nbr_Ligne));
g.setColor(Color.WHITE);
x += WIDTH / Nbr_Cologne;
}
g.setColor(Color.black);
g.drawLine(0, y, WIDTH, y);
g.setColor(Color.WHITE);
y += HEIGHT / Nbr_Ligne;
x = 0;
for(int j=0; j< Nbr_Cologne ; j++){
if(j == Decalage_RailTour)
g.setColor(Color.RED);
else
g.setColor(Color.WHITE);
g.fillRect(x,y,WIDTH / Nbr_Cologne, HEIGHT / Nbr_Ligne);
// g.setColor(Color.MAGENTA);
// g.drawString("" + (j - 210), x + ((WIDTH / Nbr_Cologne) / 2) - 5, y + ((HEIGHT / Nbr_Ligne) / 2) + 3);
g.setColor(Color.black);
g.drawLine(x, 0, x, HEIGHT + (HEIGHT / Nbr_Ligne));
g.setColor(Color.WHITE);
x += WIDTH / Nbr_Cologne;
}
g.setColor(Color.black);
g.drawLine(0, y, WIDTH, y);
g.setColor(Color.WHITE);
y += HEIGHT / Nbr_Ligne;
x = 0;
}
g.setColor(Color.black);
g.drawLine(0, y, WIDTH, y);
}
public static double OffsetX(double l){
int TailMobil_Exel = 10;
l = (((l/TailMobil_Exel) + Decalage_RailTour) * (WIDTH / Nbr_Cologne));
return l;
}
public static double OffsetY(double l){
int TailMobil_Exel = 10;
l = (l/TailMobil_Exel)* (HEIGHT / Nbr_Ligne);
return l;
}
/*public static ResultSet getData_DB(){
ResultSet rs;
Statement stmt = null;
try {
stmt = con.createStatement();
rs = stmt.executeQuery("select Id,cfg_Type,Pos_X,Pos_Y,Pos_Z from tbl_api_mobil");
while (rs.next()) {
System.out.println(rs.getString(1));
}
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// ************* creer le JPanel et les chariots *************
final JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final Hmi jc = new Hmi();
jc.setLayout(null);
final Chariot Chariot1 = new Chariot((int)OffsetX(0),(int)OffsetY(0),WIDTH / Nbr_Cologne,HEIGHT / Nbr_Ligne);
jc.add(Chariot1);
final Chariot Chariot2 = new Chariot((int)OffsetX(0),(int)OffsetY(0),WIDTH / Nbr_Cologne,HEIGHT / Nbr_Ligne);
jc.add(Chariot1);
jc.setBackground(Color.WHITE);
jc.setPreferredSize(new Dimension(WIDTH, (int) (HEIGHT)));
frame.setContentPane(jc);
frame.pack();
frame.setVisible(true);
//********** intérroger la base de donnée
// ResultSet rt;
// rt = getData_DB();
int period = 1000; // Répéter toutes les secondes
ActionListener taskPerformer = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
test();
}
public void test() {
jc.setLayout(null);
Chariot1.setPos_X(Chariot1.pos_X + 10);
Chariot1.setPos_Y(Chariot1.pos_Y + 10);
Chariot1.setLocation((int)Chariot1.getPos_X(), (int)Chariot1.getPos_Y());
//Chariot1.setBounds(Chariot1.getPos_X(), Chariot1.getPos_Y(), 30, 30);
System.out.println(" X : " + Chariot1.getPos_X() + " y : " + Chariot1.getPos_Y());
Chariot1.repaint();
jc.add(Chariot1);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(jc);
frame.pack();
frame.setVisible(true);
}
};
Timer timer = new Timer(period, taskPerformer);
timer.start();
}
} |
Partager