Усложненная работа.

This commit is contained in:
2022-11-03 15:29:19 +04:00
parent d1601148f1
commit f174057b28
8 changed files with 244 additions and 296 deletions

58
src/DrawingEntities.java Normal file
View File

@@ -0,0 +1,58 @@
import java.util.Random;
public class DrawingEntities <T extends EntityShip,U extends IAdditionalDrawingObject>{
public EntityShip[] _entities;
public IAdditionalDrawingObject[] _decks;
int entitiesCount = 0;
int decksCount = 0;
String indx;
String indy;
public DrawingEntities(int countE,int countD)
{
_entities=new EntityShip[countE];
_decks=new IAdditionalDrawingObject[countD];
}
public int Insert(T ship)
{
if(entitiesCount<_entities.length){
_entities[entitiesCount] = ship;
entitiesCount++;
return entitiesCount-1;
}
return -1;
}
public int Insert(U deck)
{
if(decksCount<_decks.length){
_decks[decksCount] = deck;
decksCount++;
return decksCount-1;
}
return -1;
}
public void SetIndexs(int ind1, int ind2)
{
indx=Integer.toString(ind1);
indy=Integer.toString(ind2);
}
public DrawingShip CreateTractor()
{
Random rnd = new Random();
int indEnt=0;
int indDeck=0;
if(entitiesCount-1!=0 & decksCount-1!=0)
{
indDeck=rnd.nextInt(0,decksCount-1);
indEnt=rnd.nextInt(0,entitiesCount-1);
}
T ship=(T)_entities[indEnt];
U deck=(U)_decks[indDeck];
SetIndexs(indEnt,indDeck);
if(ship instanceof EntityLiner)
{
return new DrawingLiner(ship,deck);
}
return new DrawingShip(ship,deck);
}
}

View File

@@ -7,6 +7,11 @@ public class DrawingLiner extends DrawingShip
super(speed,weight,bodyColor,130,45);
Ship = new EntityLiner(speed, weight, bodyColor, dopColor, dopDeck, pool);
}
protected DrawingLiner(EntityShip ship,IAdditionalDrawingObject deck)
{
super(ship,deck);
Ship=ship;
}
@Override
public void DrawTransport(Graphics gr) {
Graphics2D g2d = (Graphics2D) gr;

View File

@@ -43,6 +43,11 @@ public class DrawingShip extends JPanel {
_ShipWidth = shipWidth;
_ShipHeight = shipHeight;
}
protected DrawingShip(EntityShip ship,IAdditionalDrawingObject deck)
{
Ship=ship;
Deck=deck;
}
public void SetPosition(int x, int y, int width, int height) {
if (width <= _ShipWidth + x || height <= _ShipHeight + y || x < 0 || y < 0) {
_pictureWidth = null;

View File

@@ -1,118 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="4c171" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<toolbar id="7b4c1" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
<component id="d2f33" class="javax.swing.JButton" binding="ButtonCreateModif">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<component id="15603" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="3510" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="dd08b" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="37083" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<hspacer id="7977">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="4a1" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта море"/>
</model>
</properties>
</component>
<hspacer id="ed54d">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="87b9e" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -1,177 +0,0 @@
import javax.imageio.ImageIO;
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;
import java.security.PublicKey;
import java.util.Random;
public class FormMap extends JFrame{
private JButton ButtonCreateModif;
private JButton ButtonLeft;
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonUp;
private JButton ButtonCreate;
private JComboBox ComboBoxSelectorMap;
private AbstractMap _abstractMap;
private JPanel pictureBoxShip;
private JToolBar StatusStrip;
public JPanel MainPanel;
private Random random = new Random();
protected DrawingShip _ship;
private BufferedImage bufferImg = null;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
public void Draw() {
pictureBoxShip.removeAll();
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
if (_ship != null) {
_ship.DrawTransport(gr);
JLabel imageOfLoco = new JLabel();
imageOfLoco.setPreferredSize(pictureBoxShip.getSize());
imageOfLoco.setMinimumSize(new Dimension(1, 1));
imageOfLoco.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfLoco,BorderLayout.CENTER);
}
validate();
}
public void SetData(DrawingShip _ship)
{
pictureBoxShip.removeAll();
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(), new DrawingObjectShip(_ship))));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Draw();
}
public FormMap() {
_abstractMap = new SimpleMap();
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
try {
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
_ship=new DrawingShip (random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
var _ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
_ship.SetPosition(random.nextInt(100, 500), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
SetData(_ship);
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Up)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Down)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Right)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Left)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_ship=new DrawingLiner(random.nextInt(100, 300), random.nextInt(1000, 2000), 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());
_ship.SetPosition(random.nextInt(100, 500), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
SetData(_ship);
}
});
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch (item) {
case "Простая карта": {
_abstractMap = new SimpleMap();
break;
}
case "Карта море": {
_abstractMap = new SeaMap();
break;
}
}
}
});
}
}

62
src/FormParam.form Normal file
View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormParam">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="7700e" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="b960b" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="a44b0" class="javax.swing.JButton" binding="ButtonCreateModif">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<hspacer id="56304">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="31e01" class="javax.swing.JLabel" binding="LabelInfo">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<toolbar id="7a0ba" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
</children>
</grid>
</form>

113
src/FormParam.java Normal file
View File

@@ -0,0 +1,113 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Set;
public class FormParam extends JFrame{
public JPanel MainPanel;
private JPanel pictureBoxShip;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
private JToolBar StatusStrip;
private JLabel LabelInfo;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private DrawingEntities<EntityShip,IAdditionalDrawingObject> _drawingEntities;
private IAdditionalDrawingObject SetData()
{
Random random=new Random();
int r = random.nextInt(3);
if(r==0)
{
return new DrawingTriangleDeck();
}
if(r==1)
{
return new DrawingDeck();
}
else
{
return new DrawingOvalDeck();
}
}
private void Draw(DrawingShip _ship) {
pictureBoxShip.removeAll();
Random random = new Random();
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
if (_ship != null) {
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100),
pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
_ship.DrawTransport(gr);
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
JLabel imageOfLoco = new JLabel();
imageOfLoco.setPreferredSize(pictureBoxShip.getSize());
imageOfLoco.setMinimumSize(new Dimension(1, 1));
imageOfLoco.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfLoco,BorderLayout.CENTER);
}
validate();
}
public FormParam()
{
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
_drawingEntities=new DrawingEntities<>(10,10);
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
EntityShip ship=new EntityShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
IAdditionalDrawingObject deck = SetData();
int DecksCount=random.nextInt(1,4);
deck.SetAddEnum(DecksCount);
if((_drawingEntities.Insert(ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawingEntities.CreateTractor());
LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
EntityLiner _ship=new EntityLiner(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
IAdditionalDrawingObject deck = SetData();
int DecksCount=random.nextInt(1,4);
deck.SetAddEnum(DecksCount);
if((_drawingEntities.Insert(_ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawingEntities.CreateTractor());
LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
}
}

View File

@@ -3,7 +3,7 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Лайнер");
frame.setContentPane(new FormMapWithSetShipsGeneric().Mainpanel);
frame.setContentPane(new FormParam().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();