PIbd-23_Panina_A.D.Cruiser..../GameFrame.java

240 lines
8.6 KiB
Java
Raw Normal View History

2023-11-24 23:51:47 +04:00
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GameFrame extends JFrame {
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
GameFrame() {
this.setSize(710, 535);
2023-12-08 23:20:53 +04:00
this.setTitle("DumpTruck");
2023-11-24 23:51:47 +04:00
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
GamePanel panel = new GamePanel();
this.add(panel);
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
this.setVisible(true);
}
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
private boolean intToBoolean(int input) {
return input != 0;
}
2023-12-08 23:20:53 +04:00
public class GamePanel extends JPanel {
2023-11-24 23:51:47 +04:00
static final int SCREEN_W = 700;
static final int SCREEN_H = 500;
int xx = 0;
int yy = 0;
2023-12-08 23:20:53 +04:00
private DrawingAdvancedCruiser _drawningDumpTruck;
private DrawingCruiser _drawningCar;
private AbstractStrategy _abstractStrategy;
2023-11-24 23:51:47 +04:00
GamePanel() {
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
GridBagConstraints layers = new GridBagConstraints();
2023-12-08 23:20:53 +04:00
JButton buttonCar = new JButton("Создать грузовик");
buttonCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCar = new DrawingCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCar.wheels = new DopClassTriangle(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonCar.setBounds(20 + 150, 390, 120, 30);
this.add(buttonCar);
JButton buttonDumpCar = new JButton("Создать самосвал");
buttonDumpCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
System.out.println("F");
_drawningCar = new DrawingAdvancedCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), random.nextBoolean(),random.nextBoolean(),random.nextBoolean(),
GamePanel.SCREEN_W, GamePanel.SCREEN_H); //TODO
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCar.wheels = new DopClassTriangle(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonDumpCar.setBounds( 20, 390, 120, 30);
this.add(buttonDumpCar);
JTextField textStrategy = new JTextField();
textStrategy.setBounds(550, 20, 120, 30);
this.add(textStrategy);
JButton buttonStep = new JButton("Шаг");
buttonStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningCar == null)
{
return;
}
if (textStrategy.getText() != null)
{
switch(textStrategy.getText())
{
case "center":
_abstractStrategy = new MoveToCenter();
break;
case "border":
_abstractStrategy = new MoveToBorder();
break;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectCruiser(_drawningCar), SCREEN_W, SCREEN_H);
//textStrategy.setText();
}
if (_abstractStrategy == null)
{
return;
}
System.out.println("step");
_abstractStrategy.MakeStep();
repaint();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
//comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
});
buttonStep.setBounds(550, 60, 70, 30);
this.add(buttonStep);
2023-11-24 23:51:47 +04:00
JPanel panel = new JPanel(new GridBagLayout());
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-08 23:20:53 +04:00
_drawningCar.MoveTransport(Direction.Up);
2023-11-24 23:51:47 +04:00
repaint();
}
});
JButton left = new BasicArrowButton(BasicArrowButton.WEST);
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-08 23:20:53 +04:00
_drawningCar.MoveTransport(Direction.Left);
2023-11-24 23:51:47 +04:00
repaint();
}
});
JButton down = new BasicArrowButton(BasicArrowButton.SOUTH);
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-08 23:20:53 +04:00
_drawningCar.MoveTransport(Direction.Down);
2023-11-24 23:51:47 +04:00
repaint();
}
});
JButton right = new BasicArrowButton(BasicArrowButton.EAST);
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-08 23:20:53 +04:00
_drawningCar.MoveTransport(Direction.Right);
2023-11-24 23:51:47 +04:00
repaint();
}
});
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
up.setBounds(570, 350, 30, 30);
this.add(up);
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
down.setBounds(570, 350 + 40, 30, 30);
this.add(down);
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
left.setBounds(570 - 40, 350 + 40, 30, 30);
this.add(left);
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
right.setBounds(570 + 40, 350 + 40, 30, 30);
this.add(right);
}
2023-12-08 23:20:53 +04:00
void setTruck(DrawingAdvancedCruiser _truck) {
_drawningDumpTruck = _truck;
}
private void Draw()
{
if (_drawningCar == null)
{
return;
}
Graphics gr =new DebugGraphics();
_drawningCar.DrawTransport(gr);
}
2023-11-24 23:51:47 +04:00
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
2023-12-08 23:20:53 +04:00
public void draw(Graphics g) {
if (_drawningCar != null) {
_drawningCar.DrawTransport(g);
2023-11-24 23:51:47 +04:00
}
}
2023-12-08 23:20:53 +04:00
2023-11-24 23:51:47 +04:00
}
2023-12-08 23:20:53 +04:00
}