Выполненная усложненная лабораторная работа №3

This commit is contained in:
AnnZhimol 2022-10-28 15:29:54 +04:00
parent b3746ca983
commit d61b6cf6c5
9 changed files with 213 additions and 325 deletions

View File

@ -26,14 +26,4 @@ public class CreaterGeneric<T extends EntityWarship, U extends IDrawingObjectBlo
return -1;
}
public DrawingWarship NewWarshipCreate(){
Random rand= new Random();
DrawingWarship finalVersionWarship;
T selectedWarship =(T) Warships[rand.nextInt(WarshipsCount+1)];
U selectedBlock = (U)Blocks[rand.nextInt(BlocksCount+1)];
if(selectedWarship instanceof EntityAdvancedWarship){
return new DrawingAdvancedWarship(selectedWarship,selectedBlock);
}
return new DrawingWarship(selectedWarship,selectedBlock);
}
}

View File

@ -1,102 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class CreaterGenericField extends JPanel {
private final FormCreater formCreater;
private final CreaterGeneric<EntityWarship,IDrawingObjectBlock> createrGeneric;
private DrawingObjectWarship _warship;
public CreaterGenericField(FormCreater form){
formCreater=form;
createrGeneric=new CreaterGeneric<>(40,40);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(_warship!=null){
_warship.DrawingObject(g);
}
}
public void Draw(Graphics g){
if(_warship!=null){
_warship.DrawingObject(g);
}
else return;
}
public void BasicRadioButtonAction(int speed,int weight){
Color color=JColorChooser.showDialog(formCreater,"Выберите цвет корпуса корабля",Color.WHITE);
if(speed==0 || weight==0 || color==null){
return;
}
createrGeneric.AddWarship(new EntityWarship(speed,weight,color));
Draw(formCreater.getGraphics());
}
public void AdvancedRadioButtonAction(int speed,int weight, Boolean antenna, Boolean missile, Boolean helipad){
Color color1=JColorChooser.showDialog(formCreater,"Выберите цвет корпуса корабля",Color.WHITE);
if(speed==0 || weight==0 || color1==null){
return;
}
Color color2=JColorChooser.showDialog(formCreater,"Выберите цвет модификаций корабля",Color.WHITE);
if(color2==null){
return;
}
createrGeneric.AddWarship(new EntityAdvancedWarship(speed,weight,color1,color2,helipad,antenna,missile));
Draw(formCreater.getGraphics());
}
public BlockCount TwoRadioButtonAction(){
return BlockCount.TwoBlocks;
}
public BlockCount FourRadioButtonAction(){
return BlockCount.FourBlocks;
}
public BlockCount SixRadioButtonAction(){
return BlockCount.SixBlocks;
}
public void RectangleFormRadioButtonAction(BlockCount count){
if(count==null){
return;
}
IDrawingObjectBlock block=new DrawingBlock(count);
if(block==null)
return;
block.SetBlockCount(count.GetBlockCount());
createrGeneric.AddBlock(block);
Draw(formCreater.getGraphics());
}
public void RoundFormRadioButtonAction(BlockCount count){
if(count==null){
return;
}
IDrawingObjectBlock block=new DrawingRoundBlocks(count);
if(block==null)
return;
block.SetBlockCount(count.GetBlockCount());
createrGeneric.AddBlock(block);
Draw(formCreater.getGraphics());
}
public void TriangleFormRadioButtonAction(BlockCount count){
if(count==null){
return;
}
IDrawingObjectBlock block=new DrawingTriangleBlocks(count);
if(block==null)
return;
block.SetBlockCount(count.GetBlockCount());
createrGeneric.AddBlock(block);
Draw(formCreater.getGraphics());
}
public void ShowWarshipButtonAction(){
Random rand=new Random();
_warship=new DrawingObjectWarship(createrGeneric.NewWarshipCreate());
_warship.SetObject(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight());
Draw(formCreater.getGraphics());
}
public void RemoveSelect(){
_warship=null;
}
public DrawingObjectWarship GetWarship(){
return _warship;
}
}

View File

@ -8,10 +8,10 @@ public class DrawingAdvancedWarship extends DrawingWarship {
Blocks= GetFormOfBlock(blockForm);
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
}
public DrawingAdvancedWarship(EntityWarship selectedWarship, IDrawingObjectBlock selectedBlock) {
super(selectedWarship,selectedBlock);
Warship=selectedWarship;
public DrawingAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile,IDrawingObjectBlock blockForm) {
super(speed, weight, bodyColor, 120, 50);
Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile);
Blocks= blockForm;
}
@Override

View File

@ -1,119 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class DrawingDocks extends JPanel {
private final FormMapWithSetWarships Dock;
private FormCreater formCreater;
private MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> _mapWarshipsCollectionGeneric;
BufferedImage bufferedImage;
public DrawingDocks(FormMapWithSetWarships dock) {
Dock = dock;
formCreater=new FormCreater(Dock);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.drawImage(bufferedImage,0,0,null);
}
public void DrawDock(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.drawImage(bufferedImage,0,0,null);
}
public void AddWarshipButtonAction(){
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
formCreater.Open();
DrawingObjectWarship warship = formCreater.GetField().GetWarship();
if (_mapWarshipsCollectionGeneric.plus(warship)>=0)
{
JOptionPane.showMessageDialog(Dock,"Объект добавлен");
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
DrawDock(Dock.getGraphics());
}
else
{
JOptionPane.showMessageDialog(Dock,"Не удалось добавить объект");
}
}
public void DeleteWarshipButtonAction(String count, JPanel panel){
if (count==""||_mapWarshipsCollectionGeneric==null)
{
return;
}
int result = JOptionPane.showConfirmDialog(
panel,
"Удалить объект?",
"Удаление",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(count);
if (_mapWarshipsCollectionGeneric.minus(pos) !=null)
{
JOptionPane.showMessageDialog(Dock,"Объект удален");
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
}
else
{
JOptionPane.showMessageDialog(Dock,"Не удалось удалить объект");
}
}
public void ShowStorageButtonAction()
{
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
DrawDock(Dock.getGraphics());
}
public void ShowOnMapButtonAction()
{
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
DrawDock(Dock.getGraphics());
}
public void ComboBoxAction(String name,JPanel panel){
AbstractMap map = null;
switch (name)
{
case "Первая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
}
if (map != null)
{
_mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(
panel.getWidth(), panel.getHeight(), map);
}
else
{
_mapWarshipsCollectionGeneric = null;
}
}
public void DirectionButtonAction(Direction side){
if(_mapWarshipsCollectionGeneric != null){
bufferedImage=_mapWarshipsCollectionGeneric.MoveObject(side);
}
DrawDock(Dock.getGraphics());
}
}

View File

@ -57,7 +57,4 @@ public class DrawingField extends JPanel{
return _warship;
}
public void ResetSelected() {
_warship=null;
}
}

View File

@ -20,9 +20,10 @@ public class DrawingWarship {
Blocks= GetFormOfBlock(blockForm);
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
}
public DrawingWarship(EntityWarship selectedWarship, IDrawingObjectBlock selectedBlock) {
Warship=selectedWarship;
Blocks=selectedBlock;
public DrawingWarship(int speed, float weight, Color bodyColor, IDrawingObjectBlock blockForm)
{
Warship = new EntityWarship(speed, weight, bodyColor);
Blocks= blockForm;
}
public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){

View File

@ -1,7 +1,8 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormCreater extends JDialog {
public class FormCreater extends JDialog{
private JLabel SetSpeedLabel;
private JPanel PropertiesPanel;
private JTextField SpeedTextField;
@ -23,72 +24,106 @@ public class FormCreater extends JDialog {
private JButton ShowWarshipButton;
private JButton ChooseButton;
private JPanel PictureBox;
CreaterGenericField field=new CreaterGenericField(this);
BlockCount block=null;
IDrawingObjectBlock fblock=null;
private final CreaterGeneric<EntityWarship,IDrawingObjectBlock> createrGeneric=new CreaterGeneric<>(40,40);
private DrawingWarship _warship;
private DrawingWarship selectedWarship;
public FormCreater(JFrame frame){
super(frame,"Создание корабля");
public FormCreater(){
setTitle("Военный корабль");
setContentPane(PictureBox);
setModal(true);
ShowWindow();
setSize(1200,700);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
ShowWindow();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) PictureBox.getGraphics();
if (_warship != null) {
_warship.DrawTransport(g2d);
}
}
private void ShowWindow(){
BasicRadioButton.addActionListener(e -> {
field.BasicRadioButtonAction(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()));
ReDraw();
});
AdvancedRadioButton.addActionListener(e -> {
field.AdvancedRadioButtonAction(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected(),HelipadCheckBox.isSelected());
ReDraw();
});
TwoRadioButton.addActionListener(e -> {
block=field.TwoRadioButtonAction();
ReDraw();
block=BlockCount.TwoBlocks;
});
FourRadioButton.addActionListener(e -> {
block=field.FourRadioButtonAction();
ReDraw();
block=BlockCount.FourBlocks;
});
SixRadioButton.addActionListener(e -> {
block=field.SixRadioButtonAction();
ReDraw();
block=BlockCount.SixBlocks;
});
RectangleFormRadioButton.addActionListener(e -> {
field.RectangleFormRadioButtonAction(block);
ReDraw();
if(block==null){
return;
}
fblock=new DrawingBlock(block);
if(fblock==null)
return;
fblock.SetBlockCount(block.GetBlockCount());
createrGeneric.AddBlock(fblock);
});
RoundFormRadioButton.addActionListener(e -> {
field.RoundFormRadioButtonAction(block);
ReDraw();
if(block==null){
return;
}
fblock=new DrawingRoundBlocks(block);
if(fblock==null)
return;
fblock.SetBlockCount(block.GetBlockCount());
createrGeneric.AddBlock(fblock);
});
TriangleFormRadioButton.addActionListener(e -> {
field.TriangleFormRadioButtonAction(block);
ReDraw();
if(block==null){
return;
}
fblock=new DrawingTriangleBlocks(block);
if(fblock==null)
return;
fblock.SetBlockCount(block.GetBlockCount());
createrGeneric.AddBlock(fblock);
});
ShowWarshipButton.addActionListener(e -> {
field.ShowWarshipButtonAction();
ReDraw();
Random rand=new Random();
if (BasicRadioButton.isSelected()) {
Color color=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE);
if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){
return;
}
createrGeneric.AddWarship(new EntityWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color));
_warship=new DrawingWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color, fblock);
}
if (AdvancedRadioButton.isSelected()){
Color color1=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE);
if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color1==null){
return;
}
Color color2=JColorChooser.showDialog(this,"Выберите цвет модификаций корабля",Color.WHITE);
if(color2==null){
return;
}
createrGeneric.AddWarship(new EntityAdvancedWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),
color1,color2,HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected()));
_warship=new DrawingAdvancedWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color1,color2,
HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected(),fblock);
}
_warship.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight());
repaint();
});
ChooseButton.addActionListener(e -> {
Close();
selectedWarship=_warship;
dispose();
});
}
public void Close(){
setVisible(false);
}
public void Open(){
field.RemoveSelect();
setVisible(true);
}
public CreaterGenericField GetField(){
return field;
}
public void ReDraw(){
Graphics graphics = PictureBox.getGraphics();
graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.paintComponents(graphics);
field.Draw(graphics);
public DrawingWarship getSelectedWarship() {
return selectedWarship;
}
}

View File

@ -1,13 +1,9 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
public class FormMapWithSetWarships extends JFrame{
DrawingDocks dock = new DrawingDocks(this);
private JComboBox СomboBoxSelectorMap;
private JButton ButtonAddWarship;
private JTextField TextBoxPosition;
@ -18,67 +14,152 @@ public class FormMapWithSetWarships extends JFrame{
private JButton ButtonUp;
private JButton ButtonLeft;
private JButton ButtonRight;
private JPanel GroupBoxTools;
private JPanel PictureBox;
private JPanel MainPanel;
private JPanel GroupBoxTools;
private MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> _mapWarshipsCollectionGeneric;
private Image bufferedImage;
public FormMapWithSetWarships(){
super("Военный корабль");
setTitle("Военный корабль");
setContentPane(MainPanel);
setResizable(false);
setSize(1000,700);
setSize(1000,685);
ShowWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
PictureBox.paintComponents(bufferedImage.getGraphics());
PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
private void ShowWindow(){
ButtonShowOnMap.addActionListener(e -> {
dock.ShowOnMapButtonAction();
ReDraw();
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
repaint();
});
ButtonShowStorage.addActionListener(e -> {
dock.ShowStorageButtonAction();
ReDraw();
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
});
ButtonAddWarship.addActionListener(e -> {
dock.AddWarshipButtonAction();
ReDraw();
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
FormCreater dialog=new FormCreater();
dialog.setSize(1200,700);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.getSelectedWarship()!=null) {
DrawingObjectWarship warship = new DrawingObjectWarship(dialog.getSelectedWarship());
if (_mapWarshipsCollectionGeneric.plus(warship) >= 0) {
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE);
}
}
});
ButtonRemoveWarship.addActionListener(e -> {
dock.DeleteWarshipButtonAction(TextBoxPosition.getText(),PictureBox);
ReDraw();
String txt=TextBoxPosition.getText();
if (txt==null||_mapWarshipsCollectionGeneric==null||txt.isEmpty())
{
return;
}
int result = JOptionPane.showConfirmDialog(
this,
"Удалить объект?",
"Удаление",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(txt);
if (_mapWarshipsCollectionGeneric.minus(pos) !=null)
{
JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE);
}
});
СomboBoxSelectorMap.addActionListener(e -> {
dock.ComboBoxAction(СomboBoxSelectorMap.getSelectedItem().toString(),PictureBox);
ReDraw();
AbstractMap map = null;
switch (СomboBoxSelectorMap.getSelectedItem().toString())
{
case "Первая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
}
if (map != null)
{
_mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<>(
PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
{
_mapWarshipsCollectionGeneric = null;
}
});
ButtonUp.addActionListener(e -> {
dock.DirectionButtonAction(Direction.Up);
ReDraw();
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up);
repaint();
}
});
ButtonDown.addActionListener(e -> {
dock.DirectionButtonAction(Direction.Down);
ReDraw();
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down);
repaint();
}
});
ButtonRight.addActionListener(e -> {
dock.DirectionButtonAction(Direction.Right);
ReDraw();
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right);
repaint();
}
});
ButtonLeft.addActionListener(e -> {
dock.DirectionButtonAction(Direction.Left);
ReDraw();
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left);
repaint();
}
});
}
public void ReDraw(){
Graphics graphics = MainPanel.getGraphics();
graphics.clearRect(0, 0, MainPanel.getWidth(), MainPanel.getHeight());
MainPanel.paintComponents(graphics);
dock.DrawDock(graphics);
}
}

View File

@ -29,16 +29,16 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
return _setWarship.Remove(position);
}
public BufferedImage ShowSet()
public Image ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,1);
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_ARGB);
Graphics gr = bmp.getGraphics();
DrawBackground(gr);
DrawWarship(gr);
return bmp;
}
public BufferedImage ShowOnMap()
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setWarship.Count(); i++)
@ -52,7 +52,7 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
return new BufferedImage(_pictureWidth, _pictureHeight,1);
}
public BufferedImage MoveObject(Direction direction)
public Image MoveObject(Direction direction)
{
if (_map != null)
{
@ -89,17 +89,22 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
private void DrawBackground(Graphics gr)
{
Graphics2D g=(Graphics2D)gr;
g.setColor(Color.BLACK);
BasicStroke pen = new BasicStroke(5);
g.setStroke(pen);
Color brush=Color.BLACK;
Stroke penWide = new BasicStroke(5);
Stroke penThin = new BasicStroke(1);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.setColor(brush);
g.setStroke(penWide);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight/2+2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight/2+2);
g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2);
g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
g.setStroke(penThin);
}
}
}