This commit is contained in:
Галина Федоренко 2023-11-05 20:09:14 +04:00
parent 4874beae78
commit bf32a6b99a
5 changed files with 290 additions and 27 deletions

View File

@ -7,8 +7,11 @@ import javax.swing.Timer;
import java.awt.event.*;
public class FormHydroplane{
private DrawingHydroplane _drawingHydroplane;
private DrawingPlane _drawingPlane;
private AbstractStrategy abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 580;
public void Draw(){
canv.repaint();
@ -16,7 +19,14 @@ public class FormHydroplane{
public FormHydroplane(){
JFrame w=new JFrame ("Hydroplane");
JButton buttonCreate = new JButton("create");
JButton buttonCreatePlane = new JButton("createPlane");
JButton buttonCreateHydroplane = new JButton("createHydroplane");
JButton buttonStrategysStep = new JButton("strategys step");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"к центру",
"к краю",
});
JButton up = new JButton();
up.setBorderPainted(false);
up.setFocusPainted(false);
@ -42,47 +52,113 @@ public class FormHydroplane{
right.setName("right");
right.setIcon(new ImageIcon("C:\\RPP HARD\\PIbd-22_Fedorenko_G.Y._Hydroplane_HARD\\Hydroplane\\src\\main\\java\\org\\example\\images\\right.png"));
buttonCreate.addActionListener(
buttonCreatePlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
//System.out.println(e.getActionCommand());
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingHydroplane = new DrawingHydroplane();
_drawingHydroplane.Init(random.nextInt(100, 300), random.nextInt(1000, 3000),
_drawingPlane = new DrawingPlane(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
random.nextInt(1, 4)*10, 1000, 560);
_drawingHydroplane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingHydroplane = _drawingHydroplane;
random.nextInt(2, 5),
pictureBoxWidth,
pictureBoxHeight);
_drawingPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingPlane = _drawingPlane;
Draw();
}
}
);
buttonCreateHydroplane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingPlane = new DrawingHydroplane(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(0, 2) == 1,
random.nextInt(0, 2) == 1,
random.nextInt(2, 5),
pictureBoxWidth,
pictureBoxHeight);
_drawingPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingPlane = _drawingPlane;
Draw();
}
}
);
buttonStrategysStep.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_drawingPlane == null)
{
return;
}
if (comboBoxStrategy.isEnabled())
{
switch (comboBoxStrategy.getSelectedIndex())
{
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.SetData(new DrawningObjectPlane(_drawingPlane), pictureBoxWidth, pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null)
{
return;
}
abstractStrategy.MakeStep();
Draw();
if (abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
//System.out.println(((JButton)(e.getSource())).getName());
if (_drawingHydroplane == null)
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingPlane == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingHydroplane.MoveTransport(Direction.Up);
_drawingPlane.MoveTransport(Direction.Up);
break;
case "down":
_drawingHydroplane.MoveTransport(Direction.Down);
_drawingPlane.MoveTransport(Direction.Down);
break;
case "left":
_drawingHydroplane.MoveTransport(Direction.Left);
_drawingPlane.MoveTransport(Direction.Left);
break;
case "right":
_drawingHydroplane.MoveTransport(Direction.Right);
_drawingPlane.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
@ -92,33 +168,38 @@ public class FormHydroplane{
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 1000, 600);
buttonCreate.setBounds(2, 540, 100, 20);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
buttonCreatePlane.setBounds(2, 540, 100, 20);
buttonCreateHydroplane.setBounds(105, 540, 150, 20);
up.setBounds(900, 480, 40, 40);
down.setBounds(900, 520, 40, 40);
left.setBounds(860, 520, 40, 40);
right.setBounds(940, 520, 40, 40);
comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
w.add(canv);
w.add(buttonCreate);
w.add(buttonCreatePlane);
w.add(buttonCreateHydroplane);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.setVisible (true);
w.add(comboBoxStrategy);
w.add(buttonStrategysStep);
w.setVisible(true);
}
}
class Canvas extends JComponent{
public DrawingHydroplane _drawingHydroplane;
public DrawingPlane _drawingPlane;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingHydroplane == null){
if (_drawingPlane == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingHydroplane.DrawTransport(g2d);
_drawingPlane.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -1,7 +1,7 @@
package org.example;
public class Main {
public class Main{
public static void main(String[] args) {
new FormHydroplane();
FormHydroplane formHydroplane = new FormHydroplane();
}
}

View File

@ -0,0 +1,49 @@
package org.example;
public class MoveToBorder extends AbstractStrategy{
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder() <= FieldWidth &&
objParams.RightBorder() + GetStep() >= FieldWidth &&
objParams.DownBorder() <= FieldHeight &&
objParams.DownBorder() + GetStep() >= FieldHeight;
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder() - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,49 @@
package org.example;
public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return
Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep()
&&
Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep();
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,84 @@
package org.example;
import java.awt.*;
public class WindowDrawingTringle implements IWindowDrawing{
private NumWindow numWindow;
public NumWindow getNumWindow() {
return numWindow;
}
public void setNumWindow(int kWindow){
switch(kWindow){
case 10:
numWindow = NumWindow.tenWindows;
break;
case 20:
numWindow = NumWindow.twentyWindows;
break;
case 30:
numWindow = NumWindow.thirtyWindows;
break;
default:
numWindow = NumWindow.tenWindows;
System.out.println("Произошел косяк с количеством, но давайте их будет 10, вообще было " + kWindow);
break;
}
}
public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
g.setColor(color);
switch (numWindow) {
case tenWindows -> {
for (int i = 0; i < 10; i++) {
//g.fillOval(_startPosX + 35 + i * 8, _startPosY + 30, 4, 4);
g.fillPolygon(
new int[]{ _startPosX + 35 + i * 8, _startPosX + 35 + i * 8 + 2, _startPosX + 35 + i * 8 + 4 },
new int[]{ _startPosY + 30, _startPosY + 30 + 4, _startPosY + 30 },
3);
}
}
case twentyWindows -> {
for (int i = 0; i < 10; i++) {
//g.fillOval(_startPosX + 35 + i * 8, _startPosY + 30, 4, 4);
g.fillPolygon(
new int[]{ _startPosX + 35 + i * 8, _startPosX + 35 + i * 8 + 2, _startPosX + 35 + i * 8 + 4 },
new int[]{ _startPosY + 30, _startPosY + 30 + 4, _startPosY + 30 },
3);
}
for (int i = 0; i < 10; i++) {
//g.fillOval(_startPosX + 35 + i * 8, _startPosY + 30 + 4, 4, 4);
g.fillPolygon(
new int[]{ _startPosX + 35 + i * 8, _startPosX + 35 + i * 8 + 2, _startPosX + 35 + i * 8 + 4 },
new int[]{ _startPosY + 30 + 4, _startPosY + 30 + 4 + 4, _startPosY + 30 + 4 },
3);
}
}
case thirtyWindows -> {
for (int i = 0; i < 10; i++) {
//g.fillOval(_startPosX + 35 + i * 8, _startPosY + 30, 4, 4);
g.fillPolygon(
new int[]{ _startPosX + 35 + i * 8, _startPosX + 35 + i * 8 + 2, _startPosX + 35 + i * 8 + 4 },
new int[]{ _startPosY + 30, _startPosY + 30 + 4, _startPosY + 30 },
3);
}
for (int i = 0; i < 10; i++) {
//g.fillOval(_startPosX + 35 + i * 8, _startPosY + 30 + 4, 4, 4);
g.fillPolygon(
new int[]{ _startPosX + 35 + i * 8, _startPosX + 35 + i * 8 + 2, _startPosX + 35 + i * 8 + 4 },
new int[]{ _startPosY + 30 + 4, _startPosY + 30 + 4 + 4, _startPosY + 30 + 4 },
3);
}
for (int i = 0; i < 10; i++) {
//g.fillOval(_startPosX + 35 + i * 8, _startPosY + 30 + 8, 4, 4);
g.fillPolygon(
new int[]{ _startPosX + 35 + i * 8, _startPosX + 35 + i * 8 + 2, _startPosX + 35 + i * 8 + 4 },
new int[]{ _startPosY + 30 + 8, _startPosY + 30 + 4 + 8, _startPosY + 30 + 8 },
3);
}
}
}
}
}