129 lines
5.0 KiB
Java
129 lines
5.0 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.Random;
|
|
|
|
public class FormRoadTrain {
|
|
private DrawingRoadTrain _drawingRoadTrain;
|
|
Canvas canv;
|
|
public void Draw(){
|
|
canv.repaint();
|
|
}
|
|
public FormRoadTrain(){
|
|
JFrame w=new JFrame ("RoadTrain");
|
|
JButton buttonCreate = new JButton("Create");
|
|
buttonCreate.setBorderPainted(false);
|
|
buttonCreate.setContentAreaFilled(false);
|
|
buttonCreate.setFont(new Font("Arial", Font.PLAIN, 20));
|
|
JButton up = new JButton();
|
|
up.setBorderPainted(false);
|
|
up.setContentAreaFilled(false);
|
|
up.setName("up");
|
|
Image img_up = new ImageIcon("images/up.png").getImage();
|
|
Image newImg_up = img_up.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
|
up.setIcon(new ImageIcon(newImg_up));
|
|
|
|
JButton down = new JButton();
|
|
down.setBorderPainted(false);
|
|
down.setContentAreaFilled(false);
|
|
down.setName("down");
|
|
Image img_down = new ImageIcon("images/down.png").getImage();
|
|
Image newImg_down = img_down.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
|
down.setIcon(new ImageIcon(newImg_down));
|
|
|
|
JButton left = new JButton();
|
|
left.setBorderPainted(false);
|
|
left.setContentAreaFilled(false);
|
|
left.setName("left");
|
|
Image img_left = new ImageIcon("images/left.png").getImage();
|
|
Image newImg_left = img_left.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
|
left.setIcon(new ImageIcon(newImg_left));
|
|
|
|
JButton right = new JButton();
|
|
right.setBorderPainted(false);
|
|
right.setContentAreaFilled(false);
|
|
right.setName("right");
|
|
Image img_right = new ImageIcon("images/right.png").getImage();
|
|
Image newImg_right = img_right.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
|
right.setIcon(new ImageIcon(newImg_right));
|
|
|
|
buttonCreate.addActionListener(
|
|
new ActionListener() {
|
|
public void actionPerformed(ActionEvent e){
|
|
Random random = new Random();
|
|
_drawingRoadTrain = new DrawingRoadTrain();
|
|
_drawingRoadTrain.Init(random.nextInt(100, 300), random.nextInt(1000, 3000),
|
|
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
|
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
|
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,random.nextInt(0, 2) == 1,
|
|
random.nextInt(1, 4)*10, 1000, 560);
|
|
_drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
|
canv._drawingRoadTrain = _drawingRoadTrain;
|
|
Draw();
|
|
}
|
|
}
|
|
);
|
|
ActionListener actioListener = new ActionListener() {
|
|
public void actionPerformed(ActionEvent e){
|
|
if (_drawingRoadTrain == null)
|
|
{
|
|
return;
|
|
}
|
|
switch(((JButton)(e.getSource())).getName()){
|
|
case "up":
|
|
_drawingRoadTrain.MoveTransport(Direction.Up);
|
|
break;
|
|
case "down":
|
|
_drawingRoadTrain.MoveTransport(Direction.Down);
|
|
break;
|
|
case "left":
|
|
_drawingRoadTrain.MoveTransport(Direction.Left);
|
|
break;
|
|
case "right":
|
|
_drawingRoadTrain.MoveTransport(Direction.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
};
|
|
up.addActionListener(actioListener);
|
|
down.addActionListener(actioListener);
|
|
left.addActionListener(actioListener);
|
|
right.addActionListener(actioListener);
|
|
|
|
w.setSize (1000, 600);
|
|
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
|
w.setLayout(null);
|
|
canv = new Canvas();
|
|
canv.setBounds(0, 0, 1000, 600);
|
|
buttonCreate.setBounds(30, 500, 100, 30);
|
|
up.setBounds(880, 450, 20, 20);
|
|
down.setBounds(880, 510, 20, 20);
|
|
left.setBounds(850, 480, 20, 20);
|
|
right.setBounds(910, 480, 20, 20);
|
|
w.add(canv);
|
|
w.add(buttonCreate);
|
|
w.add(up);
|
|
w.add(down);
|
|
w.add(left);
|
|
w.add(right);
|
|
w.setVisible (true);
|
|
}
|
|
}
|
|
|
|
class Canvas extends JComponent{
|
|
public DrawingRoadTrain _drawingRoadTrain;
|
|
public Canvas(){
|
|
}
|
|
public void paintComponent (Graphics g){
|
|
if (_drawingRoadTrain == null){
|
|
return;
|
|
}
|
|
super.paintComponents (g) ;
|
|
Graphics2D g2d = (Graphics2D)g;
|
|
_drawingRoadTrain.DrawTransport(g2d);
|
|
super.repaint();
|
|
}
|
|
}
|