Compare commits

...

1 Commits

Author SHA1 Message Date
933b4f7fa0 готовая первая лаба 2023-12-08 22:46:45 +04:00
7 changed files with 353 additions and 13 deletions

6
src/Direction.java Normal file
View File

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

71
src/DrawingOar.java Normal file
View File

@ -0,0 +1,71 @@
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class DrawingOar {
private NumOars numOars;
public NumOars getSomeProperty() {
return numOars;
}
public void setNumOars(int oars){
switch(oars){
case 1:
numOars = NumOars.OneOar;
break;
case 2:
numOars = NumOars.TwoOars;
break;
case 3:
numOars = NumOars.ThreeOars;
break;
default:
numOars = NumOars.OneOar;
System.out.println("Неверное значение");
break;
}
}
void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 110, _startPosY + 145, _startPosX + 95, _startPosY + 165);
g.setColor(color);
g.fillPolygon(new int[]{_startPosX + 95, _startPosX + 95, _startPosX + 80, _startPosX + 65, _startPosX + 80},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 95, _startPosX + 95, _startPosX + 80, _startPosX + 65, _startPosX + 80},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
if (numOars == NumOars.TwoOars){
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 78, _startPosY + 145, _startPosX + 63, _startPosY + 165);
g.setColor(color);
g.fillPolygon(new int[]{_startPosX + 63, _startPosX + 63, _startPosX + 48, _startPosX + 33, _startPosX + 48},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 63, _startPosX + 63, _startPosX + 48, _startPosX + 33, _startPosX + 48},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
}
if (numOars == NumOars.ThreeOars){
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 78, _startPosY + 145, _startPosX + 63, _startPosY + 165);
g.setColor(color);
g.fillPolygon(new int[]{_startPosX + 63, _startPosX + 63, _startPosX + 48, _startPosX + 33, _startPosX + 48},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 63, _startPosX + 63, _startPosX + 48, _startPosX + 33, _startPosX + 48},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 46, _startPosY + 145, _startPosX + 31, _startPosY + 165);
g.setColor(color);
g.fillPolygon(new int[]{_startPosX + 31, _startPosX + 31, _startPosX + 16, _startPosX + 1, _startPosX + 16},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 31, _startPosX + 31, _startPosX + 16, _startPosX + 1, _startPosX + 16},
new int[]{_startPosY + 165, _startPosY + 180, _startPosY + 195, _startPosY + 180, _startPosY + 165}, 5);
}
}
}

126
src/DrawingSailboat.java Normal file
View File

@ -0,0 +1,126 @@
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class DrawingSailboat {
private DrawingOar drawingOar;
private EntitySailboat EntitySailboat;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _boatWidth = 160;
private int _boatHeight = 195;
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean hull, boolean sail, int oarsNum, int width, int height)
{
if (width < _boatWidth || height < _boatHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntitySailboat = new EntitySailboat();
EntitySailboat.Init(speed, weight, bodyColor, additionalColor, hull, sail, oarsNum);
drawingOar = new DrawingOar();
drawingOar.setNumOars(oarsNum);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _boatWidth > _pictureWidth)
{
x = 0;
}
if (y < 0 || y + _boatHeight > _pictureHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
{
if (EntitySailboat == null)
{
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntitySailboat.Step > 0)
{
_startPosX -= (int)EntitySailboat.Step;
}
break;
case Up:
if (_startPosY - EntitySailboat.Step > 0)
{
_startPosY -= (int)EntitySailboat.Step;
}
break;
case Right:
if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth)
{
_startPosX += (int)EntitySailboat.Step;
}
break;
case Down:
if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight)
{
_startPosY += (int)EntitySailboat.Step;
}
break;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntitySailboat == null)
{
return;
}
g.setColor(EntitySailboat.AdditionalColor);
//усиленный корпус парусника
if (EntitySailboat.Hull)
{
g.fillPolygon(new int[]{_startPosX, _startPosX + 120, _startPosX + 160, _startPosX + 120, _startPosX},
new int[]{_startPosY + 80, _startPosY + 80, _startPosY + 120, _startPosY + 160, _startPosY + 160}, 5);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX, _startPosX + 120, _startPosX + 160, _startPosX + 120, _startPosX},
new int[]{_startPosY + 80, _startPosY + 80, _startPosY + 120, _startPosY + 160, _startPosY + 160}, 5);
}
//основной корпус парусника
g.setColor(EntitySailboat.BodyColor);
g.fillPolygon(new int[]{_startPosX + 10, _startPosX + 110, _startPosX + 140, _startPosX + 110, _startPosX + 10},
new int[]{_startPosY + 90, _startPosY + 90, _startPosY + 120, _startPosY + 150, _startPosY + 150}, 5);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 10, _startPosX + 110, _startPosX + 140, _startPosX + 110, _startPosX + 10},
new int[]{_startPosY + 90, _startPosY + 90, _startPosY + 120, _startPosY + 150, _startPosY + 150}, 5);
g.setColor(Color.green);
g.fillOval(_startPosX + 20, _startPosY + 100, 90, 40);
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 100, 90, 40);
//парус
if (EntitySailboat.Sail)
{
g.setColor(EntitySailboat.AdditionalColor);
g.fillPolygon(new int[]{_startPosX + 65, _startPosX + 130, _startPosX + 15},
new int[]{_startPosY, _startPosY + 120, _startPosY + 120}, 3);
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 65, _startPosX + 130, _startPosX + 15},
new int[]{_startPosY, _startPosY + 120, _startPosY + 120}, 3);
g.drawLine(_startPosX + 65, _startPosY + 120, _startPosX + 65, _startPosY);
}
//весла
drawingOar.Draw(_startPosX, _startPosY, new Color(128, 64, 48), g);
}
}

25
src/EntitySailboat.java Normal file
View File

@ -0,0 +1,25 @@
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class EntitySailboat {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean Hull;
public boolean Sail;
public double Step;
public int OarsNum;
public void Init (int speed, double weight, Color bodyColor, Color additionalColor, boolean hull, boolean sail, int oarsNum){
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Hull = hull;
Sail = sail;
Step = (double)Speed * 100 / Weight;
OarsNum = oarsNum;
}
}

119
src/FormSailboat.java Normal file
View File

@ -0,0 +1,119 @@
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class FormSailboat{
private DrawingSailboat _drawingSailboat;
Canvas canv;
public void Draw(){
canv.repaint();
}
public FormSailboat(){
JFrame w=new JFrame ("Парусник");
JButton buttonCreate = new JButton("Создать");
JButton up = new JButton();
up.setBorderPainted(true);
up.setFocusPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
up.setIcon(new ImageIcon(((new ImageIcon("C:\\storage\\university\\2 course\\rpp\\up.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton down = new JButton();
down.setBorderPainted(true);
down.setFocusPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
down.setIcon(new ImageIcon(((new ImageIcon("C:\\storage\\university\\2 course\\rpp\\down.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton left = new JButton();
left.setBorderPainted(true);
left.setFocusPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
left.setIcon(new ImageIcon(((new ImageIcon("C:\\storage\\university\\2 course\\rpp\\left.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton right = new JButton();
right.setBorderPainted(true);
right.setFocusPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
right.setIcon(new ImageIcon(((new ImageIcon("C:\\storage\\university\\2 course\\rpp\\right.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingSailboat = new DrawingSailboat();
_drawingSailboat.Init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextInt(2) == 1, random.nextInt(2) == 1, random.nextInt(3) + 1, 1000, 560);
_drawingSailboat.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
canv._drawingSailboat = _drawingSailboat;
Draw();
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingSailboat == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingSailboat.MoveTransport(Direction.Up);
break;
case "down":
_drawingSailboat.MoveTransport(Direction.Down);
break;
case "left":
_drawingSailboat.MoveTransport(Direction.Left);
break;
case "right":
_drawingSailboat.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 1000, 600);
buttonCreate.setBounds(5, 500, 100, 40);
up.setBounds(900, 440, 40, 40);
down.setBounds(900, 480, 40, 40);
left.setBounds(860, 480, 40, 40);
right.setBounds(940, 480, 40, 40);
w.add(canv);
w.add(buttonCreate);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.setVisible (true);
}
}
class Canvas extends JComponent{
public DrawingSailboat _drawingSailboat;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingSailboat == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingSailboat.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -1,17 +1,5 @@
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) {
// Press Alt+Enter with your caret at the highlighted text to see how
// IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
for (int i = 1; i <= 5; i++) {
// Press Shift+F9 to start debugging your code. We have set one breakpoint
// for you, but you can always add more by pressing Ctrl+F8.
System.out.println("i = " + i);
}
FormSailboat formSailboat = new FormSailboat();
}
}

5
src/NumOars.java Normal file
View File

@ -0,0 +1,5 @@
public enum NumOars {
OneOar,
TwoOars,
ThreeOars
}