commit1
This commit is contained in:
parent
b7e8f4afd6
commit
00dd2a975d
7
src/Direction.java
Normal file
7
src/Direction.java
Normal file
@ -0,0 +1,7 @@
|
||||
public enum Direction
|
||||
{
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
134
src/DrawingCatamaran.java
Normal file
134
src/DrawingCatamaran.java
Normal file
@ -0,0 +1,134 @@
|
||||
import java.awt.*;
|
||||
public class DrawingCatamaran {
|
||||
private OarDrawing oarDrawing;
|
||||
public EntityCatamaran _EntityCatamaran;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private int _catamaranWidth = 195;
|
||||
private int _catamaranHeight = 195;
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean sail, boolean floatDetail, int numOar, int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureHeight < _catamaranHeight || _pictureWidth < _catamaranWidth)
|
||||
return false;
|
||||
_EntityCatamaran = new EntityCatamaran();
|
||||
_EntityCatamaran.Init(speed, weight, bodyColor, additionalColor, sail, floatDetail, numOar);
|
||||
oarDrawing = new OarDrawing();
|
||||
oarDrawing.setNumOar(numOar);
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = Math.min(x, _pictureWidth-_catamaranWidth);
|
||||
_startPosY = Math.min(y, _pictureHeight-_catamaranHeight);
|
||||
}
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (_EntityCatamaran == null){
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - _EntityCatamaran.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)_EntityCatamaran.Step;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - _EntityCatamaran.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)_EntityCatamaran.Step;
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + _catamaranWidth + _EntityCatamaran.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)_EntityCatamaran.Step;
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + _catamaranHeight + _EntityCatamaran.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)_EntityCatamaran.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (_EntityCatamaran == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
g.setColor(_EntityCatamaran.AdditionalColor);
|
||||
//поплавки
|
||||
if (_EntityCatamaran.FloatDetail)
|
||||
{
|
||||
int[] xPoints1 = new int[] {
|
||||
_startPosX,
|
||||
_startPosX + 160,
|
||||
_startPosX + 180,
|
||||
_startPosX + 160,
|
||||
_startPosX
|
||||
};
|
||||
int[] yPoints1 = new int[] {
|
||||
_startPosY + 80,
|
||||
_startPosY + 80,
|
||||
_startPosY + 95,
|
||||
_startPosY + 110,
|
||||
_startPosY + 110
|
||||
};
|
||||
int[] xPoints2 = new int[] {
|
||||
_startPosX,
|
||||
_startPosX + 160,
|
||||
_startPosX + 180,
|
||||
_startPosX + 160,
|
||||
_startPosX
|
||||
};
|
||||
int[] yPoints2 = new int[] {
|
||||
_startPosY + 130,
|
||||
_startPosY + 130,
|
||||
_startPosY + 145,
|
||||
_startPosY + 160,
|
||||
_startPosY + 160
|
||||
};
|
||||
g.setColor(_EntityCatamaran.AdditionalColor);
|
||||
g.fillPolygon(xPoints1, yPoints1, 5);
|
||||
g.fillPolygon(xPoints2, yPoints2, 5);
|
||||
g.drawPolygon(xPoints1, yPoints1, 5);
|
||||
g.drawPolygon(xPoints2, yPoints2, 5);
|
||||
}
|
||||
|
||||
//основной корпус парусника
|
||||
g.setColor(_EntityCatamaran.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.lightGray);
|
||||
g.fillOval(_startPosX + 20, _startPosY + 100, 90, 40);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_startPosX + 20, _startPosY + 100, 90, 40);
|
||||
|
||||
//весла
|
||||
oarDrawing.Draw(_startPosX, _startPosY, Color.DARK_GRAY, g);
|
||||
|
||||
//парус
|
||||
if (_EntityCatamaran.Sail)
|
||||
{
|
||||
g.setColor(_EntityCatamaran.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);
|
||||
}
|
||||
}
|
||||
}
|
23
src/EntityCatamaran.java
Normal file
23
src/EntityCatamaran.java
Normal file
@ -0,0 +1,23 @@
|
||||
import java.awt.*;
|
||||
public class EntityCatamaran {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public Color AdditionalColor;
|
||||
public boolean Sail;
|
||||
public boolean FloatDetail;
|
||||
public double Step;
|
||||
public int numOar;
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean sail, boolean floatDetail, int _numOar)
|
||||
{
|
||||
numOar = _numOar;
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Sail = sail;
|
||||
FloatDetail = floatDetail;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
}
|
||||
}
|
120
src/FormCatamaran.java
Normal file
120
src/FormCatamaran.java
Normal file
@ -0,0 +1,120 @@
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
public class FormCatamaran {
|
||||
private DrawingCatamaran _drawingCatamaran;
|
||||
Canvas canv;
|
||||
public void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
public FormCatamaran(){
|
||||
JFrame w=new JFrame ("Catamaran");
|
||||
JButton buttonCreate = new JButton("create");
|
||||
JButton up = new JButton();
|
||||
up.setBorderPainted(false);
|
||||
up.setFocusPainted(false);
|
||||
up.setContentAreaFilled(false);
|
||||
up.setName("up");
|
||||
up.setIcon(new ImageIcon("C:\\Users\\Admin\\Desktop\\3 семестр\\PIbd-22_Safiulova_K.N._Catamaran._HARD\\labaCatamaran\\Resources\\upper-arrow.png"));
|
||||
|
||||
JButton down = new JButton();
|
||||
down.setBorderPainted(false);
|
||||
down.setFocusPainted(false);
|
||||
down.setContentAreaFilled(false);
|
||||
down.setName("down");
|
||||
down.setIcon(new ImageIcon("C:\\Users\\Admin\\Desktop\\3 семестр\\PIbd-22_Safiulova_K.N._Catamaran._HARD\\labaCatamaran\\Resources\\down-arrow.png"));
|
||||
|
||||
JButton left = new JButton();
|
||||
left.setBorderPainted(false);
|
||||
left.setFocusPainted(false);
|
||||
left.setContentAreaFilled(false);
|
||||
left.setName("left");
|
||||
left.setIcon(new ImageIcon("C:\\Users\\Admin\\Desktop\\3 семестр\\PIbd-22_Safiulova_K.N._Catamaran._HARD\\labaCatamaran\\Resources\\left-arrow.png"));
|
||||
|
||||
JButton right = new JButton();
|
||||
right.setBorderPainted(false);
|
||||
right.setFocusPainted(false);
|
||||
right.setContentAreaFilled(false);
|
||||
right.setName("right");
|
||||
right.setIcon(new ImageIcon("C:\\Users\\Admin\\Desktop\\3 семестр\\PIbd-22_Safiulova_K.N._Catamaran._HARD\\labaCatamaran\\Resources\\right-arrow.png"));
|
||||
|
||||
buttonCreate.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
_drawingCatamaran = new DrawingCatamaran();
|
||||
_drawingCatamaran.Init(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), 1000, 560);
|
||||
_drawingCatamaran.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
canv._drawingCatamaran = _drawingCatamaran;
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
ActionListener actioListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawingCatamaran == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawingCatamaran.MoveTransport(Direction.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawingCatamaran.MoveTransport(Direction.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawingCatamaran.MoveTransport(Direction.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawingCatamaran.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(2, 540, 100, 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);
|
||||
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 DrawingCatamaran _drawingCatamaran;
|
||||
public Canvas(){
|
||||
}
|
||||
public void paintComponent (Graphics g){
|
||||
if (_drawingCatamaran == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
_drawingCatamaran.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
FormCatamaran formCatamaran = new FormCatamaran();
|
||||
}
|
||||
}
|
5
src/NumOar.java
Normal file
5
src/NumOar.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum NumOar {
|
||||
OneOar,
|
||||
TwoOar,
|
||||
ThreeOar
|
||||
}
|
63
src/OarDrawing.java
Normal file
63
src/OarDrawing.java
Normal file
@ -0,0 +1,63 @@
|
||||
import java.awt.*;
|
||||
public class OarDrawing {
|
||||
private NumOar numOar;
|
||||
public NumOar getSomeProperty() {
|
||||
return numOar;
|
||||
}
|
||||
public void setNumOar(int kOar){
|
||||
switch(kOar){
|
||||
case 1:
|
||||
numOar = numOar.OneOar;
|
||||
break;
|
||||
case 2:
|
||||
numOar = numOar.TwoOar;
|
||||
break;
|
||||
case 3:
|
||||
numOar = numOar.ThreeOar;
|
||||
break;
|
||||
default:
|
||||
numOar = NumOar.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 (numOar == NumOar.TwoOar){
|
||||
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 (numOar == NumOar.ThreeOar){
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user