PIbd-22_Kashin_M.I_Gasoline.../src/FormGasolineTanker.java

132 lines
5.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormGasolineTanker extends JFrame{
private int Width;
private int Height;
DrawingGasolineTanker SelectedGasolineTanker;
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
public JLabel SpeedLabel;
public JLabel WeightLabel;
public JLabel BodyColorLabel;
private JPanel PictureBox;
private JButton ButtonSelect;
private DrawingGasolineTanker _gasolineTanker=null;
public FormGasolineTanker(){
setTitle("Gasoline Tanker");
setContentPane(PictureBox);
setSize(1000,500);
setResizable(false);
ShowWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteLeft =new ImageIcon((new ImageIcon("Material\\KeyLeft.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
private void SetData() {
Random rand=new Random();
_gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
SpeedLabel.setText("Скорость: "+_gasolineTanker.getGasolineTanker().getSpeed());
WeightLabel.setText("Вес: "+_gasolineTanker.getGasolineTanker().getWeight());
BodyColorLabel.setText("Цвет: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2));
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_gasolineTanker != null) {
PictureBox.paintComponents(PictureBox.getGraphics());
_gasolineTanker.DrawTransport(g);
}
}
private void ShowWindow(){
ButtonCreate.addActionListener(e -> {
Random rand=new Random();
_gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3));
SetData();
repaint();
});
ButtonCreateModif.addActionListener(e -> {
Random rand=new Random();
_gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3));
SetData();
repaint();
});
ButtonUp.setIcon(spriteUp);
ButtonUp.addActionListener(e -> {
if(_gasolineTanker == null)
return;
_gasolineTanker.MoveTransport(Direction.Up);
repaint();
});
ButtonLeft.setIcon(spriteLeft);
ButtonLeft.addActionListener(e -> {
if(_gasolineTanker == null)
return;
_gasolineTanker.MoveTransport(Direction.Left);
repaint();;
});
ButtonRight.setIcon(spriteRight);
ButtonRight.addActionListener(e -> {
if(_gasolineTanker == null)
return;
_gasolineTanker.MoveTransport(Direction.Right);
repaint();
});
ButtonDown.setIcon(spriteDown);
ButtonDown.addActionListener(e -> {
if(_gasolineTanker == null)
return;
_gasolineTanker.MoveTransport(Direction.Down);
repaint();
});
ButtonSelect.addActionListener(e -> {
SelectedGasolineTanker=GetDrawingGasolineTanker();
JOptionPane.showMessageDialog(PictureBox, "Gasoline tanker added.");
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e){
super.componentResized(e);
Width=getWidth();
Height=getHeight();
if (_gasolineTanker!=null)
_gasolineTanker.ChangeBorders(getWidth(),getHeight());
else return;
repaint();
setBounds(0,0,Width,Height);
}
});
}
public DrawingGasolineTanker GetDrawingGasolineTanker() {
return _gasolineTanker;
}
public void SetDrawingGasolineTanker(DrawingGasolineTanker gasolineTanker) {
_gasolineTanker=gasolineTanker;
SetData();
repaint();
}
}