lab3 with an additional task
This commit is contained in:
parent
b0b474077f
commit
96a9477a8f
@ -1,8 +1,9 @@
|
||||
import frames.FrameBattleship;
|
||||
import frames.FrameShipsCollection;
|
||||
import frames.HardFrame;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException { new FrameShipsCollection(); }
|
||||
public static void main(String[] args) throws IOException { new HardFrame(); }
|
||||
}
|
||||
|
@ -5,6 +5,17 @@ import java.awt.*;
|
||||
public class DrawingBlocks implements IDrawBlocks{
|
||||
private BlocksNumber number;
|
||||
@Override
|
||||
public int getNumber() {
|
||||
int x = 0;
|
||||
if(number == BlocksNumber.TWO)
|
||||
x = 2;
|
||||
else if(number == BlocksNumber.FOUR)
|
||||
x = 4;
|
||||
else if(number == BlocksNumber.SIX)
|
||||
x = 6;
|
||||
return x;
|
||||
}
|
||||
@Override
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = BlocksNumber.TWO;
|
||||
@ -14,6 +25,10 @@ public class DrawingBlocks implements IDrawBlocks{
|
||||
number = BlocksNumber.SIX;
|
||||
}
|
||||
@Override
|
||||
public int getType() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
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);
|
||||
|
@ -5,6 +5,17 @@ import java.awt.*;
|
||||
public class DrawingCrossBlocks implements IDrawBlocks{
|
||||
private BlocksNumber number;
|
||||
@Override
|
||||
public int getNumber() {
|
||||
int x = 0;
|
||||
if(number == BlocksNumber.TWO)
|
||||
x = 2;
|
||||
else if(number == BlocksNumber.FOUR)
|
||||
x = 4;
|
||||
else if(number == BlocksNumber.SIX)
|
||||
x = 6;
|
||||
return x;
|
||||
}
|
||||
@Override
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = BlocksNumber.TWO;
|
||||
@ -14,6 +25,8 @@ public class DrawingCrossBlocks implements IDrawBlocks{
|
||||
number = BlocksNumber.SIX;
|
||||
}
|
||||
@Override
|
||||
public int getType() {return 2;}
|
||||
@Override
|
||||
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillRect(_startX+54, _startY+12, 2, 6);
|
||||
graphics2D.fillRect(_startX+52, _startY+14, 6, 2);
|
||||
|
@ -5,6 +5,17 @@ import java.awt.*;
|
||||
public class DrawingRoundBlocks implements IDrawBlocks{
|
||||
private BlocksNumber number;
|
||||
@Override
|
||||
public int getNumber() {
|
||||
int x = 0;
|
||||
if(number == BlocksNumber.TWO)
|
||||
x = 2;
|
||||
else if(number == BlocksNumber.FOUR)
|
||||
x = 4;
|
||||
else if(number == BlocksNumber.SIX)
|
||||
x = 6;
|
||||
return x;
|
||||
}
|
||||
@Override
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = BlocksNumber.TWO;
|
||||
@ -14,6 +25,10 @@ public class DrawingRoundBlocks implements IDrawBlocks{
|
||||
number = BlocksNumber.SIX;
|
||||
}
|
||||
@Override
|
||||
public int getType() {
|
||||
return 1;
|
||||
}
|
||||
@Override
|
||||
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillOval(_startX+50, _startY+11, 8, 8);
|
||||
graphics2D.fillOval(_startX+50, _startY+31, 8, 8);
|
||||
|
@ -3,6 +3,8 @@ package drawing_objects;
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawBlocks {
|
||||
int getNumber();
|
||||
void setNumber(int x);
|
||||
int getType();
|
||||
void drawBlocks(Graphics2D graphics2D, int _startX, int _startY);
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ public class FrameShipsCollection extends JFrame {
|
||||
TextField textFieldNumber;
|
||||
public FrameShipsCollection(){
|
||||
super("Набор кораблей");
|
||||
setSize(new Dimension(900,500));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
createGui();
|
||||
ships = new ShipsGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
@ -30,7 +29,7 @@ public class FrameShipsCollection extends JFrame {
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
pictureBoxCollection.setSize(new Dimension(700, 450));
|
||||
pictureBoxCollection.setPreferredSize(new Dimension(700, 450));
|
||||
JButton buttonAddShip = new JButton("Добавить корабль");
|
||||
textFieldNumber = new TextField();
|
||||
JButton buttonRemoveShip = new JButton("Удалить корабль");
|
||||
@ -60,6 +59,7 @@ public class FrameShipsCollection extends JFrame {
|
||||
setLayout(new BorderLayout());
|
||||
add(panelCollection, BorderLayout.EAST);
|
||||
add(pictureBoxCollection, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
private void buttonAddShipClick() {
|
||||
FrameBattleship form;
|
||||
|
75
src/frames/HardFrame.java
Normal file
75
src/frames/HardFrame.java
Normal file
@ -0,0 +1,75 @@
|
||||
package frames;
|
||||
|
||||
import drawing_objects.*;
|
||||
import entities.EntityBattleship;
|
||||
import entities.EntityShip;
|
||||
import generics.HardGeneric;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class HardFrame extends JFrame {
|
||||
HardGeneric<EntityShip, IDrawBlocks> generic;
|
||||
DrawingShip drawing;
|
||||
private JComponent pictureBox;
|
||||
private final int pictureBoxWidth = 200;
|
||||
private final int pictureBoxHeight = 100;
|
||||
public HardFrame(){
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
pictureBox = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawing != null) drawing.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight));
|
||||
JButton buttonMakeObject = new JButton("Создать новый объект");
|
||||
Random rand = new Random();
|
||||
int size = rand.nextInt(1, 10);
|
||||
generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
|
||||
for(int i = 0; i < size; i++){
|
||||
generic.insertShip(makeRandomShip());
|
||||
generic.insertBlock(makeRandomBlock());
|
||||
}
|
||||
buttonMakeObject.addActionListener(e -> {
|
||||
DrawingShip drawingShip = generic.makeObject();
|
||||
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth()/2, pictureBoxHeight / 2 - drawingShip.getHeight()/2);
|
||||
drawing = drawingShip;
|
||||
draw();
|
||||
});
|
||||
setLayout(new BorderLayout());
|
||||
add(pictureBox, BorderLayout.CENTER);
|
||||
add(buttonMakeObject, BorderLayout.SOUTH);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
public EntityShip makeRandomShip(){
|
||||
Random random = new Random();
|
||||
EntityShip ship;
|
||||
switch (random.nextInt(2)){
|
||||
case 0 -> ship = new EntityShip(random.nextInt(100, 300), random.nextDouble(1000,3000),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
||||
default -> ship = new EntityBattleship(random.nextInt(100, 300), random.nextDouble(1000,3000),
|
||||
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());
|
||||
}
|
||||
return ship;
|
||||
}
|
||||
public IDrawBlocks makeRandomBlock(){
|
||||
Random random = new Random();
|
||||
IDrawBlocks block;
|
||||
switch (random.nextInt(3)){
|
||||
case 1 -> block = new DrawingRoundBlocks();
|
||||
case 2 -> block = new DrawingCrossBlocks();
|
||||
default -> block = new DrawingBlocks();
|
||||
}
|
||||
block.setNumber((random.nextInt(3) + 1) * 2);
|
||||
return block;
|
||||
}
|
||||
void draw(){pictureBox.repaint();}
|
||||
}
|
55
src/generics/HardGeneric.java
Normal file
55
src/generics/HardGeneric.java
Normal file
@ -0,0 +1,55 @@
|
||||
package generics;
|
||||
|
||||
import drawing_objects.DrawingBattleship;
|
||||
import drawing_objects.DrawingShip;
|
||||
import drawing_objects.IDrawBlocks;
|
||||
import entities.EntityBattleship;
|
||||
import entities.EntityShip;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class HardGeneric<T extends EntityShip, U extends IDrawBlocks>{
|
||||
T[] ships;
|
||||
U[] blocks;
|
||||
private int shipsNumber;
|
||||
private int blocksNumber;
|
||||
private int pictureBoxWidth;
|
||||
private int pictureBoxHeight;
|
||||
public HardGeneric(int shipsCount, int blocksCount, int width, int height){
|
||||
shipsNumber = 0;
|
||||
blocksNumber = 0;
|
||||
ships = (T[]) new EntityShip[shipsCount];
|
||||
blocks = (U[]) new IDrawBlocks[blocksCount];
|
||||
pictureBoxHeight = height;
|
||||
pictureBoxWidth = width;
|
||||
}
|
||||
public int insertShip(T entityShip){
|
||||
if(ships[ships.length-1] != null)
|
||||
return -1;
|
||||
for(int i = shipsNumber -1; i>= 0; i--) {
|
||||
ships[i + 1] = ships[i];
|
||||
}
|
||||
shipsNumber++;
|
||||
ships[0] = entityShip;
|
||||
return 0;
|
||||
}
|
||||
public int insertBlock(U block){
|
||||
if(blocks[blocks.length-1] != null)
|
||||
return -1;
|
||||
for(int i = blocksNumber -1; i>= 0; i--) {
|
||||
blocks[i + 1] = blocks[i];
|
||||
}
|
||||
blocksNumber++;
|
||||
blocks[0] = block;
|
||||
return 0;
|
||||
}
|
||||
public DrawingShip makeObject(){
|
||||
Random rand = new Random();
|
||||
EntityShip entity = ships[rand.nextInt(0, shipsNumber)];
|
||||
IDrawBlocks block = blocks[rand.nextInt(0, blocksNumber)];
|
||||
if (entity instanceof EntityBattleship)
|
||||
return new DrawingBattleship(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), ((EntityBattleship) entity).getAdditionalColor(),
|
||||
((EntityBattleship) entity).getTurret(), ((EntityBattleship) entity).getRocketLauncher(), pictureBoxWidth, pictureBoxHeight, block.getType(), block.getNumber());
|
||||
return new DrawingShip(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), pictureBoxWidth, pictureBoxHeight, block.getType(), block.getNumber());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user