lab1
This commit is contained in:
parent
45882e168e
commit
afef85366f
BIN
images/down.png
Normal file
BIN
images/down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 256 B |
BIN
images/left.png
Normal file
BIN
images/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 269 B |
BIN
images/right.png
Normal file
BIN
images/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 257 B |
BIN
images/up.png
Normal file
BIN
images/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 250 B |
5
src/BlocksNumber.java
Normal file
5
src/BlocksNumber.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum BlocksNumber {
|
||||
TWO,
|
||||
FOUR,
|
||||
SIX
|
||||
}
|
6
src/DirectionType.java
Normal file
6
src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
115
src/DrawingBattleship.java
Normal file
115
src/DrawingBattleship.java
Normal file
@ -0,0 +1,115 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingBattleship {
|
||||
private EntityBattleship entityBattleship;
|
||||
public EntityBattleship getEntityBattleship() {
|
||||
return entityBattleship;
|
||||
}
|
||||
private int pictureWidth;
|
||||
private int pictureHeight;
|
||||
private int startPosX;
|
||||
private int startPosY;
|
||||
private final int shipWidth = 150;
|
||||
public int getShipWidth() {return shipWidth;}
|
||||
private final int shipHeight = 50;
|
||||
public int getShipHeight() {return shipHeight;}
|
||||
private DrawingBlocks drawingBlocks;
|
||||
public boolean init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean turret, boolean rocketLauncher, int width, int height, int blocksNumber) {
|
||||
if (width < shipWidth || height < shipHeight)
|
||||
return false;
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
entityBattleship = new EntityBattleship();
|
||||
entityBattleship.init(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
|
||||
drawingBlocks = new DrawingBlocks();
|
||||
drawingBlocks.setNumber(blocksNumber);
|
||||
return true;
|
||||
}
|
||||
public void setPosition(int x, int y) {
|
||||
if (x < 0 || y < 0 || x + shipWidth > pictureWidth || y + shipHeight > pictureHeight) {
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
startPosX = x;
|
||||
startPosY = y;
|
||||
}
|
||||
public void moveTransport(DirectionType direction) {
|
||||
if (entityBattleship == null)
|
||||
return;
|
||||
switch (direction) {
|
||||
//влево
|
||||
case LEFT:
|
||||
if (startPosX - entityBattleship.step.get().intValue() > 0)
|
||||
startPosX -= entityBattleship.step.get().intValue();
|
||||
break;
|
||||
//вверх
|
||||
case UP:
|
||||
if (startPosY - entityBattleship.step.get().intValue() > 0)
|
||||
startPosY -= entityBattleship.step.get().intValue();
|
||||
break;
|
||||
// вправо
|
||||
case RIGHT:
|
||||
if (startPosX + shipWidth + entityBattleship.step.get().intValue() < pictureWidth)
|
||||
startPosX += entityBattleship.step.get().intValue();
|
||||
break;
|
||||
//вниз
|
||||
case DOWN:
|
||||
if (startPosY + shipHeight + entityBattleship.step.get().intValue() < pictureHeight)
|
||||
startPosY += entityBattleship.step.get().intValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void drawTransport(Graphics2D graphics2D) {
|
||||
if (entityBattleship == null)
|
||||
return;
|
||||
BasicStroke pen = new BasicStroke(2);
|
||||
graphics2D.setStroke(pen);
|
||||
Color bodyColor = entityBattleship.getBodyColor();
|
||||
Color additionalColor = entityBattleship.getAdditionalColor();
|
||||
//корпус
|
||||
int[] hullX = new int[] {startPosX + 5, startPosX + 100, startPosX + 150, startPosX + 100, startPosX + 5};
|
||||
int[] hullY = new int[] {startPosY, startPosY, startPosY + 25, startPosY + 50, startPosY + 50};
|
||||
graphics2D.setPaint(bodyColor);
|
||||
graphics2D.fillPolygon(hullX, hullY, 5);
|
||||
graphics2D.setPaint(Color.BLACK);
|
||||
graphics2D.drawPolygon(hullX, hullY, 5);
|
||||
graphics2D.fillRect(startPosX, startPosY + 6, 5, 13);
|
||||
graphics2D.fillRect(startPosX, startPosY + 31, 5, 13);
|
||||
//надстройки
|
||||
graphics2D.setPaint(Color.DARK_GRAY);
|
||||
graphics2D.fillRect(startPosX + 40, startPosY + 20, 30, 10);
|
||||
graphics2D.fillRect(startPosX + 70, startPosY + 12, 18, 26);
|
||||
graphics2D.fillOval(startPosX + 94, startPosY + 19, 12, 12);
|
||||
graphics2D.setPaint(Color.BLACK);
|
||||
graphics2D.drawRect(startPosX + 40, startPosY + 20, 30, 10);
|
||||
graphics2D.drawRect(startPosX + 70, startPosY + 12, 18, 26);
|
||||
graphics2D.drawOval(startPosX + 94, startPosY + 19, 12, 12);
|
||||
//блоки
|
||||
drawingBlocks.drawBlocks(graphics2D, startPosX, startPosY);
|
||||
//орудийная башня
|
||||
if (entityBattleship.getTurret()) {
|
||||
int[] shieldX = new int[] {startPosX + 112, startPosX + 112, startPosX + 119, startPosX + 119, };
|
||||
int[] shieldY = new int[] {startPosY + 19, startPosY + 31, startPosY + 28, startPosY + 22};
|
||||
graphics2D.setPaint(additionalColor);
|
||||
graphics2D.fillPolygon(shieldX, shieldY, 4);
|
||||
graphics2D.fillRect(startPosX + 119, startPosY + 24, 12, 2);
|
||||
graphics2D.setPaint(Color.BLACK);
|
||||
graphics2D.drawPolygon(shieldX, shieldY, 4);
|
||||
graphics2D.drawRect(startPosX + 119, startPosY + 24, 12, 2);
|
||||
}
|
||||
//ячейки для ракет
|
||||
if (entityBattleship.getRocketLauncher()) {
|
||||
graphics2D.setPaint(additionalColor);
|
||||
graphics2D.fillRect(startPosX + 14, startPosY + 14, 10, 10);
|
||||
graphics2D.fillRect(startPosX + 26, startPosY + 14, 10, 10);
|
||||
graphics2D.fillRect(startPosX + 14, startPosY + 26, 10, 10);
|
||||
graphics2D.fillRect(startPosX + 26, startPosY + 26, 10, 10);
|
||||
graphics2D.setPaint(Color.BLACK);
|
||||
graphics2D.drawRect(startPosX + 14, startPosY + 14, 10, 10);
|
||||
graphics2D.drawRect(startPosX + 26, startPosY + 14, 10, 10);
|
||||
graphics2D.drawRect(startPosX + 14, startPosY + 26, 10, 10);
|
||||
graphics2D.drawRect(startPosX + 26, startPosY + 26, 10, 10);
|
||||
}
|
||||
}
|
||||
}
|
25
src/DrawingBlocks.java
Normal file
25
src/DrawingBlocks.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingBlocks {
|
||||
private BlocksNumber number;
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = BlocksNumber.TWO;
|
||||
if(x == 4)
|
||||
number = BlocksNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = BlocksNumber.SIX;
|
||||
}
|
||||
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillRect(_startX+52, _startY+12, 6, 6);
|
||||
graphics2D.fillRect(_startX+52, _startY+32, 6, 6);
|
||||
if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){
|
||||
graphics2D.fillRect(_startX+62, _startY+12, 6, 6);
|
||||
graphics2D.fillRect(_startX+62, _startY+32, 6, 6);
|
||||
}
|
||||
if (number == BlocksNumber.SIX){
|
||||
graphics2D.fillRect(_startX+42, _startY+12, 6, 6);
|
||||
graphics2D.fillRect(_startX+42, _startY+32, 6, 6);
|
||||
}
|
||||
}
|
||||
}
|
39
src/EntityBattleship.java
Normal file
39
src/EntityBattleship.java
Normal file
@ -0,0 +1,39 @@
|
||||
import java.awt.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class EntityBattleship {
|
||||
private int speed;
|
||||
public int getSpeed(){
|
||||
return speed;
|
||||
}
|
||||
private double weight;
|
||||
public double getWeight(){
|
||||
return weight;
|
||||
}
|
||||
private Color bodyColor;
|
||||
public Color getBodyColor(){
|
||||
return bodyColor;
|
||||
}
|
||||
private Color additionalColor;
|
||||
public Color getAdditionalColor(){
|
||||
return additionalColor;
|
||||
}
|
||||
private boolean turret;
|
||||
public boolean getTurret() {
|
||||
return turret;
|
||||
}
|
||||
private boolean rocketLauncher;
|
||||
public boolean getRocketLauncher() {
|
||||
return rocketLauncher;
|
||||
}
|
||||
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
||||
public void init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean turret, boolean rocketLauncher) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.bodyColor = bodyColor;
|
||||
this.additionalColor = additionalColor;
|
||||
this.turret = turret;
|
||||
this.rocketLauncher = rocketLauncher;
|
||||
}
|
||||
}
|
109
src/FrameBattleship.java
Normal file
109
src/FrameBattleship.java
Normal file
@ -0,0 +1,109 @@
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
public class FrameBattleship extends JFrame {
|
||||
private DrawingBattleship drawingBattleship;
|
||||
private final JComponent pictureBox;
|
||||
public FrameBattleship() throws IOException {
|
||||
super("Линкор");
|
||||
setSize(new Dimension(900,500));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
//components initialisation
|
||||
pictureBox = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
JButton createButton = new JButton("Создать");
|
||||
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
|
||||
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
|
||||
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
|
||||
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
|
||||
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
||||
//ActionListeners and ActionCommand addition
|
||||
createButton.addActionListener(e -> buttonCreateClick());
|
||||
rightButton.setActionCommand("right");
|
||||
rightButton.addActionListener(this::buttonMoveClick);
|
||||
leftButton.setActionCommand("left");
|
||||
leftButton.addActionListener(this::buttonMoveClick);
|
||||
upButton.setActionCommand("up");
|
||||
upButton.addActionListener(this::buttonMoveClick);
|
||||
downButton.setActionCommand("down");
|
||||
downButton.addActionListener(this::buttonMoveClick);
|
||||
//component addition
|
||||
setLayout(new BorderLayout());
|
||||
JPanel panelBattleship = new JPanel(new BorderLayout());
|
||||
JPanel createPanel = new JPanel(new BorderLayout());
|
||||
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
||||
createPanel.add(createButton, BorderLayout.SOUTH);
|
||||
JPanel movementPanel = new JPanel(new GridBagLayout());
|
||||
JPanel rightPanel = new JPanel(new BorderLayout());
|
||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||
rightButton.setPreferredSize(new Dimension(30,30));
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.gridx = 2;
|
||||
constraints.gridy = 1;
|
||||
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
||||
movementPanel.add(rightButton, constraints);
|
||||
leftButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(leftButton, constraints);
|
||||
upButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 0;
|
||||
movementPanel.add(upButton, constraints);
|
||||
downButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(downButton, constraints);
|
||||
add(pictureBox);
|
||||
panelBattleship.add(rightPanel, BorderLayout.EAST);
|
||||
panelBattleship.add(createPanel, BorderLayout.WEST);
|
||||
add(panelBattleship,BorderLayout.CENTER);
|
||||
setVisible(true);
|
||||
}
|
||||
private void buttonCreateClick() {
|
||||
Random random = new Random();
|
||||
drawingBattleship = new DrawingBattleship();
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
drawingBattleship.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), (random.nextInt(3)+1)*2);
|
||||
drawingBattleship.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
draw();
|
||||
}
|
||||
private void buttonMoveClick(ActionEvent event) {
|
||||
if(drawingBattleship == null || drawingBattleship.getEntityBattleship() == null)
|
||||
return;
|
||||
switch (event.getActionCommand())
|
||||
{
|
||||
case "left":
|
||||
drawingBattleship.moveTransport(DirectionType.LEFT);
|
||||
break;
|
||||
case "right":
|
||||
drawingBattleship.moveTransport(DirectionType.RIGHT);
|
||||
break;
|
||||
case "up":
|
||||
drawingBattleship.moveTransport(DirectionType.UP);
|
||||
break;
|
||||
case "down":
|
||||
drawingBattleship.moveTransport(DirectionType.DOWN);
|
||||
break;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
private void draw() {
|
||||
if (drawingBattleship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBox.repaint();
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
public static void main(String[] args) throws IOException { new FrameBattleship(); }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user