PIbd-21_KozyrevSS_GasolineT.../ComboFrame.java

75 lines
2.2 KiB
Java
Raw Normal View History

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
class DrawRandomTanker extends JComponent {
private DrawTanker _drawTanker;
private final ComboGenericCollection<BaseTanker, IWheelDraw> GeneratorTanker;
public DrawRandomTanker() {
GeneratorTanker = new ComboGenericCollection<>(25, 1000, 600);
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (_drawTanker == null)
return;
_drawTanker.DrawTransport(g);
super.repaint();
}
protected void GenerateTanker_Click()
{
_drawTanker = GeneratorTanker.CreateDraw();
super.repaint();
}
}
class ComboFrame extends JFrame {
public ComboFrame()
{
initUI();
}
protected static final int Width = 1000;
protected static final int Height = 600;
DrawRandomTanker Tanker;
private void initUI()
{
setSize(Width, Height);
setTitle("TankGasoline");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Tanker = new DrawRandomTanker();
Tanker.setBounds(0, 0, Width, Height);
add(Tanker);
JButton UpdateButton = new JButton("Update tanker");
UpdateButton.setLayout(null);
UpdateButton.setBounds(Width-200, Height- 200, 100, 100);
add(UpdateButton);
UpdateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tanker.GenerateTanker_Click();
}
});
}
}