Выполненная усложненная лабораторная работа №3
This commit is contained in:
parent
b3746ca983
commit
d61b6cf6c5
@ -26,14 +26,4 @@ public class CreaterGeneric<T extends EntityWarship, U extends IDrawingObjectBlo
|
|||||||
return -1;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,10 +8,10 @@ public class DrawingAdvancedWarship extends DrawingWarship {
|
|||||||
Blocks= GetFormOfBlock(blockForm);
|
Blocks= GetFormOfBlock(blockForm);
|
||||||
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
|
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
|
||||||
}
|
}
|
||||||
|
public DrawingAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile,IDrawingObjectBlock blockForm) {
|
||||||
public DrawingAdvancedWarship(EntityWarship selectedWarship, IDrawingObjectBlock selectedBlock) {
|
super(speed, weight, bodyColor, 120, 50);
|
||||||
super(selectedWarship,selectedBlock);
|
Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile);
|
||||||
Warship=selectedWarship;
|
Blocks= blockForm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
@ -57,7 +57,4 @@ public class DrawingField extends JPanel{
|
|||||||
return _warship;
|
return _warship;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetSelected() {
|
|
||||||
_warship=null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,10 @@ public class DrawingWarship {
|
|||||||
Blocks= GetFormOfBlock(blockForm);
|
Blocks= GetFormOfBlock(blockForm);
|
||||||
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
|
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
|
||||||
}
|
}
|
||||||
public DrawingWarship(EntityWarship selectedWarship, IDrawingObjectBlock selectedBlock) {
|
public DrawingWarship(int speed, float weight, Color bodyColor, IDrawingObjectBlock blockForm)
|
||||||
Warship=selectedWarship;
|
{
|
||||||
Blocks=selectedBlock;
|
Warship = new EntityWarship(speed, weight, bodyColor);
|
||||||
|
Blocks= blockForm;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){
|
public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class FormCreater extends JDialog {
|
public class FormCreater extends JDialog{
|
||||||
private JLabel SetSpeedLabel;
|
private JLabel SetSpeedLabel;
|
||||||
private JPanel PropertiesPanel;
|
private JPanel PropertiesPanel;
|
||||||
private JTextField SpeedTextField;
|
private JTextField SpeedTextField;
|
||||||
@ -23,72 +24,106 @@ public class FormCreater extends JDialog {
|
|||||||
private JButton ShowWarshipButton;
|
private JButton ShowWarshipButton;
|
||||||
private JButton ChooseButton;
|
private JButton ChooseButton;
|
||||||
private JPanel PictureBox;
|
private JPanel PictureBox;
|
||||||
CreaterGenericField field=new CreaterGenericField(this);
|
|
||||||
BlockCount block=null;
|
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){
|
public FormCreater(){
|
||||||
super(frame,"Создание корабля");
|
setTitle("Военный корабль");
|
||||||
setContentPane(PictureBox);
|
setContentPane(PictureBox);
|
||||||
setModal(true);
|
ShowWindow();
|
||||||
setSize(1200,700);
|
setSize(1200,700);
|
||||||
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
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(){
|
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 -> {
|
TwoRadioButton.addActionListener(e -> {
|
||||||
block=field.TwoRadioButtonAction();
|
block=BlockCount.TwoBlocks;
|
||||||
ReDraw();
|
|
||||||
});
|
});
|
||||||
FourRadioButton.addActionListener(e -> {
|
FourRadioButton.addActionListener(e -> {
|
||||||
block=field.FourRadioButtonAction();
|
block=BlockCount.FourBlocks;
|
||||||
ReDraw();
|
|
||||||
});
|
});
|
||||||
SixRadioButton.addActionListener(e -> {
|
SixRadioButton.addActionListener(e -> {
|
||||||
block=field.SixRadioButtonAction();
|
block=BlockCount.SixBlocks;
|
||||||
ReDraw();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
RectangleFormRadioButton.addActionListener(e -> {
|
RectangleFormRadioButton.addActionListener(e -> {
|
||||||
field.RectangleFormRadioButtonAction(block);
|
if(block==null){
|
||||||
ReDraw();
|
return;
|
||||||
|
}
|
||||||
|
fblock=new DrawingBlock(block);
|
||||||
|
if(fblock==null)
|
||||||
|
return;
|
||||||
|
fblock.SetBlockCount(block.GetBlockCount());
|
||||||
|
createrGeneric.AddBlock(fblock);
|
||||||
});
|
});
|
||||||
|
|
||||||
RoundFormRadioButton.addActionListener(e -> {
|
RoundFormRadioButton.addActionListener(e -> {
|
||||||
field.RoundFormRadioButtonAction(block);
|
if(block==null){
|
||||||
ReDraw();
|
return;
|
||||||
|
}
|
||||||
|
fblock=new DrawingRoundBlocks(block);
|
||||||
|
if(fblock==null)
|
||||||
|
return;
|
||||||
|
fblock.SetBlockCount(block.GetBlockCount());
|
||||||
|
createrGeneric.AddBlock(fblock);
|
||||||
});
|
});
|
||||||
|
|
||||||
TriangleFormRadioButton.addActionListener(e -> {
|
TriangleFormRadioButton.addActionListener(e -> {
|
||||||
field.TriangleFormRadioButtonAction(block);
|
if(block==null){
|
||||||
ReDraw();
|
return;
|
||||||
|
}
|
||||||
|
fblock=new DrawingTriangleBlocks(block);
|
||||||
|
if(fblock==null)
|
||||||
|
return;
|
||||||
|
fblock.SetBlockCount(block.GetBlockCount());
|
||||||
|
createrGeneric.AddBlock(fblock);
|
||||||
});
|
});
|
||||||
|
|
||||||
ShowWarshipButton.addActionListener(e -> {
|
ShowWarshipButton.addActionListener(e -> {
|
||||||
field.ShowWarshipButtonAction();
|
Random rand=new Random();
|
||||||
ReDraw();
|
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 -> {
|
ChooseButton.addActionListener(e -> {
|
||||||
Close();
|
selectedWarship=_warship;
|
||||||
|
dispose();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
public void Close(){
|
public DrawingWarship getSelectedWarship() {
|
||||||
setVisible(false);
|
return selectedWarship;
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.awt.event.ComponentAdapter;
|
|
||||||
import java.awt.event.ComponentEvent;
|
|
||||||
|
|
||||||
public class FormMapWithSetWarships extends JFrame{
|
public class FormMapWithSetWarships extends JFrame{
|
||||||
DrawingDocks dock = new DrawingDocks(this);
|
|
||||||
private JComboBox СomboBoxSelectorMap;
|
private JComboBox СomboBoxSelectorMap;
|
||||||
private JButton ButtonAddWarship;
|
private JButton ButtonAddWarship;
|
||||||
private JTextField TextBoxPosition;
|
private JTextField TextBoxPosition;
|
||||||
@ -18,67 +14,152 @@ public class FormMapWithSetWarships extends JFrame{
|
|||||||
private JButton ButtonUp;
|
private JButton ButtonUp;
|
||||||
private JButton ButtonLeft;
|
private JButton ButtonLeft;
|
||||||
private JButton ButtonRight;
|
private JButton ButtonRight;
|
||||||
private JPanel GroupBoxTools;
|
|
||||||
private JPanel PictureBox;
|
private JPanel PictureBox;
|
||||||
private JPanel MainPanel;
|
private JPanel MainPanel;
|
||||||
|
private JPanel GroupBoxTools;
|
||||||
|
private MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> _mapWarshipsCollectionGeneric;
|
||||||
|
private Image bufferedImage;
|
||||||
|
|
||||||
public FormMapWithSetWarships(){
|
public FormMapWithSetWarships(){
|
||||||
super("Военный корабль");
|
setTitle("Военный корабль");
|
||||||
setContentPane(MainPanel);
|
setContentPane(MainPanel);
|
||||||
setResizable(false);
|
setResizable(false);
|
||||||
setSize(1000,700);
|
setSize(1000,685);
|
||||||
ShowWindow();
|
ShowWindow();
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setVisible(true);
|
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(){
|
private void ShowWindow(){
|
||||||
|
|
||||||
ButtonShowOnMap.addActionListener(e -> {
|
ButtonShowOnMap.addActionListener(e -> {
|
||||||
dock.ShowOnMapButtonAction();
|
if (_mapWarshipsCollectionGeneric == null)
|
||||||
ReDraw();
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
|
||||||
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
ButtonShowStorage.addActionListener(e -> {
|
ButtonShowStorage.addActionListener(e -> {
|
||||||
dock.ShowStorageButtonAction();
|
if (_mapWarshipsCollectionGeneric == null)
|
||||||
ReDraw();
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
|
||||||
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
ButtonAddWarship.addActionListener(e -> {
|
ButtonAddWarship.addActionListener(e -> {
|
||||||
dock.AddWarshipButtonAction();
|
if (_mapWarshipsCollectionGeneric == null)
|
||||||
ReDraw();
|
{
|
||||||
|
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 -> {
|
ButtonRemoveWarship.addActionListener(e -> {
|
||||||
dock.DeleteWarshipButtonAction(TextBoxPosition.getText(),PictureBox);
|
String txt=TextBoxPosition.getText();
|
||||||
ReDraw();
|
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 -> {
|
СomboBoxSelectorMap.addActionListener(e -> {
|
||||||
dock.ComboBoxAction(СomboBoxSelectorMap.getSelectedItem().toString(),PictureBox);
|
AbstractMap map = null;
|
||||||
ReDraw();
|
|
||||||
|
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 -> {
|
ButtonUp.addActionListener(e -> {
|
||||||
dock.DirectionButtonAction(Direction.Up);
|
if (_mapWarshipsCollectionGeneric != null) {
|
||||||
ReDraw();
|
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up);
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ButtonDown.addActionListener(e -> {
|
ButtonDown.addActionListener(e -> {
|
||||||
dock.DirectionButtonAction(Direction.Down);
|
if (_mapWarshipsCollectionGeneric != null) {
|
||||||
ReDraw();
|
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down);
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ButtonRight.addActionListener(e -> {
|
ButtonRight.addActionListener(e -> {
|
||||||
dock.DirectionButtonAction(Direction.Right);
|
if (_mapWarshipsCollectionGeneric != null) {
|
||||||
ReDraw();
|
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right);
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ButtonLeft.addActionListener(e -> {
|
ButtonLeft.addActionListener(e -> {
|
||||||
dock.DirectionButtonAction(Direction.Left);
|
if (_mapWarshipsCollectionGeneric != null) {
|
||||||
ReDraw();
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -29,16 +29,16 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
|
|||||||
return _setWarship.Remove(position);
|
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();
|
Graphics gr = bmp.getGraphics();
|
||||||
DrawBackground(gr);
|
DrawBackground(gr);
|
||||||
DrawWarship(gr);
|
DrawWarship(gr);
|
||||||
return bmp;
|
return bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage ShowOnMap()
|
public Image ShowOnMap()
|
||||||
{
|
{
|
||||||
Shaking();
|
Shaking();
|
||||||
for (int i = 0; i < _setWarship.Count(); i++)
|
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);
|
return new BufferedImage(_pictureWidth, _pictureHeight,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage MoveObject(Direction direction)
|
public Image MoveObject(Direction direction)
|
||||||
{
|
{
|
||||||
if (_map != null)
|
if (_map != null)
|
||||||
{
|
{
|
||||||
@ -89,17 +89,22 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
|
|||||||
private void DrawBackground(Graphics gr)
|
private void DrawBackground(Graphics gr)
|
||||||
{
|
{
|
||||||
Graphics2D g=(Graphics2D)gr;
|
Graphics2D g=(Graphics2D)gr;
|
||||||
g.setColor(Color.BLACK);
|
|
||||||
BasicStroke pen = new BasicStroke(5);
|
Color brush=Color.BLACK;
|
||||||
g.setStroke(pen);
|
Stroke penWide = new BasicStroke(5);
|
||||||
|
Stroke penThin = new BasicStroke(1);
|
||||||
|
|
||||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
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+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 + 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 + (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.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
|
||||||
|
g.setStroke(penThin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user