PIbd-23_Panina_A.D.Cruiser..../GameFrame.java
2023-12-09 15:09:27 +04:00

198 lines
8.1 KiB
Java

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 {
GameFrame() {
this.setSize(710, 535);
this.setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
GamePanel panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
public class GamePanel extends JPanel {
static final int SCREEN_W = 700;
static final int SCREEN_H = 500;
int xx = 0;
int yy = 0;
private DrawingAdvancedCruiser _drawningAdvancedCruiser;
private DrawingCruiser _drawningCruiser;
private AbstractStrategy _abstractStrategy;
GamePanel() {
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
GridBagConstraints layers = new GridBagConstraints();
JButton buttonCruiser = new JButton("Создать простой объект");
buttonCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCruiser = 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);
_drawningCruiser.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCruiser.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonCruiser.setBounds(20 + 150, 390, 120, 30);
this.add(buttonCruiser);
JButton buttonAdvancedCruiser = new JButton("Создать продвинутый объект");
buttonAdvancedCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCruiser = 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
_drawningCruiser.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCruiser.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonAdvancedCruiser.setBounds( 20, 390, 120, 30);
this.add(buttonAdvancedCruiser);
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 (_drawningCruiser == 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(_drawningCruiser), SCREEN_W, SCREEN_H);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
repaint();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
_abstractStrategy = null;
}
}
});
buttonStep.setBounds(550, 60, 70, 30);
this.add(buttonStep);
JPanel panel = new JPanel(new GridBagLayout());
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Up);
repaint();
}
});
JButton left = new BasicArrowButton(BasicArrowButton.WEST);
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Left);
repaint();
}
});
JButton down = new BasicArrowButton(BasicArrowButton.SOUTH);
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Down);
repaint();
}
});
JButton right = new BasicArrowButton(BasicArrowButton.EAST);
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Right);
repaint();
}
});
up.setBounds(570, 350, 30, 30);
this.add(up);
down.setBounds(570, 350 + 40, 30, 30);
this.add(down);
left.setBounds(570 - 40, 350 + 40, 30, 30);
this.add(left);
right.setBounds(570 + 40, 350 + 40, 30, 30);
this.add(right);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (_drawningCruiser != null) {
_drawningCruiser.DrawTransport(g);
}
}
}
}