Лабораторная работа №1 без отрисовки

This commit is contained in:
xom9k 2024-04-16 19:36:10 +04:00
parent edf9293d1a
commit 9dd4fc3827
10 changed files with 512 additions and 1 deletions

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
ProjectAntiAircraftGun/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,15 @@
import javax.swing.*;
import java.awt.*;
public class CanvasAntiAircraftGun extends JComponent {
public DrawningAntiAircraftGun _drawingAntiAircraftGun;
public CanvasAntiAircraftGun(){}
public void paintComponent(Graphics g) {
if (_drawingAntiAircraftGun == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingAntiAircraftGun.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,5 @@
public enum CountRollers {
Four,
Five,
Six;
}

View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,143 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningAntiAircraftGun extends JPanel{
public EntityAntiAircraftGun EntityAntiAircraftGun;
public DrawningRollers _rollers = null;
private Integer picture_width;
private Integer picture_height;
private Integer _StartPosX;
private Integer _StartPosY;
private int drawningCarWidth = 185;
private int drawningCarHeight = 100;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean tower, boolean radar) {
EntityAntiAircraftGun = new EntityAntiAircraftGun();
EntityAntiAircraftGun.Init(speed, weight, bodycolor, additionalcolor, tower, radar);
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
_rollers = new DrawningRollers();
Random rand = new Random();
int randomNum = rand.nextInt(4);
_rollers.SetCount(randomNum);
}
public boolean SetPictureSize(int width, int height) {
if (width < drawningCarWidth || height < drawningCarHeight) return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
if (_StartPosX + drawningCarWidth > picture_width)
{
_StartPosX = _StartPosX - (_StartPosX + drawningCarWidth - picture_width);
}
else if (_StartPosX < 0) _StartPosX = 0;
if (_StartPosY + drawningCarHeight > picture_height)
{
_StartPosY = _StartPosY - (_StartPosY + drawningCarHeight - picture_height);
}
else if (_StartPosY < 0) _StartPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(picture_width != null && picture_height != null)) return;
if (x + drawningCarWidth > picture_width)
{
_StartPosX = x - (x + drawningCarWidth - picture_width);
}
else if (x < 0) _StartPosX = 0;
else _StartPosX = x;
if (y + drawningCarHeight > picture_height)
{
_StartPosY = y - (y + drawningCarHeight - picture_height);
}
else if (y < 0) _StartPosY = 0;
else _StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityAntiAircraftGun == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case Left:
if (_StartPosX - EntityAntiAircraftGun.Step > 0) {
_StartPosX -= (int)EntityAntiAircraftGun.Step;
}
return true;
case Up:
if (_StartPosY - EntityAntiAircraftGun.Step > 0)
{
_StartPosY -= (int)EntityAntiAircraftGun.Step;
}
return true;
case Right:
if (_StartPosX + drawningCarWidth + (int)EntityAntiAircraftGun.Step < picture_width - EntityAntiAircraftGun.Step)
{
_StartPosX += (int)EntityAntiAircraftGun.Step;
}
return true;
case Down:
if (_StartPosY + drawningCarHeight + (int)EntityAntiAircraftGun.Step < picture_height - EntityAntiAircraftGun.Step)
{
_StartPosY += (int)EntityAntiAircraftGun.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityAntiAircraftGun == null) {
return;
}
// иллюминаторы
_rollers.Draw(g, _StartPosX+30, _StartPosY+34);
g.setColor(Color.BLACK);
//Тело
g.drawRect(_StartPosX + 5, _StartPosY + 50, 170, 30);
g.drawArc(_StartPosX - 5, _StartPosY + 50, 20, 30, 90, 180);
//Заднее крыло
g.drawLine( _StartPosX, _StartPosY, _StartPosX + 50, _StartPosY + 50);
g.drawLine( _StartPosX, _StartPosY, _StartPosX, _StartPosY + 52);
//Заднее боковые крылья
g.drawOval(_StartPosX - 7, _StartPosY + 45, 30, 8);
g.fillOval(_StartPosX - 7, _StartPosY + 45, 30, 8);
//Нос
g.drawLine( _StartPosX + 175, _StartPosY + 50, _StartPosX + 200, _StartPosY + 65);
g.drawLine( _StartPosX + 200, _StartPosY + 65, _StartPosX + 175, _StartPosY + 80);
g.drawLine( _StartPosX + 175, _StartPosY + 50, _StartPosX + 175, _StartPosY + 80);
g.drawLine( _StartPosX + 175, _StartPosY + 65, _StartPosX + 200, _StartPosY + 65);
//Крылья
g.drawArc(_StartPosX + 49, _StartPosY + 62, 5, 5, 90, 180);
g.drawLine( _StartPosX + 51, _StartPosY + 62, _StartPosX + 140, _StartPosY + 62);
g.drawArc( _StartPosX + 137, _StartPosY + 62, 5, 5, 2790, 180);
g.drawLine( _StartPosX + 51, _StartPosY + 67, _StartPosX + 140, _StartPosY + 67);
//Задние шасси
g.drawLine(_StartPosX + 55, _StartPosY + 80, _StartPosX + 55, _StartPosY + 90);
g.drawOval(_StartPosX + 47, _StartPosY + 90, 5, 5);
g.drawOval( _StartPosX + 57, _StartPosY + 90, 5, 5);
//Передние шасси
g.drawLine( _StartPosX + 165, _StartPosY + 80, _StartPosX + 165, _StartPosY + 90);
g.drawOval( _StartPosX + 163, _StartPosY + 91, 5, 5);
//Пассажирсакий доп. отсек
if (EntityAntiAircraftGun.getIsTower())
{
g.setColor(EntityAntiAircraftGun.getAdditionalColor());
g.drawArc(_StartPosX + 60, _StartPosY + 28, 115, 45, 0, 180);
g.fillArc(_StartPosX + 60, _StartPosY + 28, 115, 45, 0, 180);
}
// Доп. двигатели
if (EntityAntiAircraftGun.getIsRadar())
{
g.drawLine(_StartPosX + 95, _StartPosY + 68, _StartPosX + 95, _StartPosY + 75);
g.setColor(EntityAntiAircraftGun.getAdditionalColor());
int[] xPolygon = { _StartPosX + 83, _StartPosX + 103, _StartPosX + 103, _StartPosX + 83, _StartPosX + 83};
int[] yPolygon = {_StartPosY + 78, _StartPosY + 73, _StartPosY + 93, _StartPosY + 88, _StartPosY + 78};
g.drawPolygon(xPolygon, yPolygon, xPolygon.length);
g.fillPolygon(xPolygon, yPolygon, xPolygon.length);
}
}
}

View File

@ -0,0 +1,62 @@
import java.awt.*;
public class DrawningRollers {
private CountRollers _rollers;
public CountRollers getCount() {
return _rollers;
}
public void SetCount(int count) {
switch (count) {
case 1: // 4
_rollers = CountRollers.Four;
break;
case 2: // 5
_rollers = CountRollers.Five;
break;
case 3: // 6
_rollers = CountRollers.Six;
break;
default:
_rollers = CountRollers.Four;
break;
}
}
public void Draw(Graphics2D g, int _startPosx, int _startPoxY) {
g.setColor(Color.BLACK);
if (_rollers == null) {
return;
}
for (int i = 0; i < 10; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx + 19 + i * 8, _startPoxY + 21, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 19 + i * 8, _startPoxY + 21, 3, 3);
}
if (_rollers != CountRollers.Four) {
for (int i = 0; i < 5; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx - 15 + i * 5, _startPoxY + 28, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx - 15 + i * 5, _startPoxY + 28, 3, 3);
}
for (int i = 0; i < 5; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx + 115 + i * 5, _startPoxY + 28, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 115 + i * 5, _startPoxY + 28, 3, 3);
}
}
if (_rollers == CountRollers.Six) {
for (int i = 0; i < 10; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx + 19 + i * 8, _startPoxY + 37, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 19 + i * 8, _startPoxY + 37, 3, 3);
}
}
}
}

View File

@ -0,0 +1,24 @@
import java.awt.*;
public class EntityAntiAircraftGun {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
private Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean IsTower;
public boolean getIsTower() {return IsTower;}
private boolean IsRadar;
public boolean getIsRadar() {return IsRadar;}
public double Step;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isTower, boolean isRadar)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
IsTower = isTower;
IsRadar = isRadar;
Step = Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,123 @@
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.util.Random;
public class FormAntiAircraftGun extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasAntiAircraftGun canvasAntiAircraftGun = new CanvasAntiAircraftGun();
private JButton CreateButton = new JButton("Create");;
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
public FormAntiAircraftGun(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
CreateButton.setName("CREATE");
Icon iconUp = new ImageIcon("src\\up.jpg");
UpButton.setIcon(iconUp);
UpButton.setName("UP");
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("src\\down.jpg");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("src\\left.jpg");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("src\\right.jpg");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean tower = new Random().nextBoolean();
boolean radar = new Random().nextBoolean();;
canvasAntiAircraftGun._drawingAntiAircraftGun = new DrawningAntiAircraftGun();
canvasAntiAircraftGun._drawingAntiAircraftGun.Init(speed, weight, bodyColor, additionalColor, tower, radar);
canvasAntiAircraftGun._drawingAntiAircraftGun.SetPictureSize(Width, Height);
canvasAntiAircraftGun._drawingAntiAircraftGun.SetPosition( StartPositionX, StartPositionY);
canvasAntiAircraftGun.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasAntiAircraftGun._drawingAntiAircraftGun == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "UP":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasAntiAircraftGun.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasAntiAircraftGun.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasAntiAircraftGun);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasAntiAircraftGun._drawingAntiAircraftGun != null)
canvasAntiAircraftGun._drawingAntiAircraftGun.SetPictureSize(Width, Height);
canvasAntiAircraftGun.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
}
});
}
}

View File

@ -1,5 +1,8 @@
import java.awt.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello and welcome!");
FormAntiAircraftGun form = new FormAntiAircraftGun("Зенитная установка", new Dimension(500, 500));
form.Init();
}
}