Lab1
This commit is contained in:
parent
eb538c1a1b
commit
2978773e84
5
scr/DeckType.java
Normal file
5
scr/DeckType.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum DeckType {
|
||||
OneDeck,
|
||||
TwoDecks,
|
||||
ThreeDecks
|
||||
}
|
6
scr/DirectionType.java
Normal file
6
scr/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
47
scr/DrawingDecks.java
Normal file
47
scr/DrawingDecks.java
Normal file
@ -0,0 +1,47 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingDecks {
|
||||
private DeckType deckType;
|
||||
private int NumberDecks;
|
||||
public void SetNumberDecks(int value){
|
||||
NumberDecks = value;
|
||||
switch (value){
|
||||
case 2:
|
||||
deckType = DeckType.TwoDecks;
|
||||
break;
|
||||
case 3:
|
||||
deckType = DeckType.ThreeDecks;
|
||||
break;
|
||||
default:
|
||||
deckType = DeckType.OneDeck;
|
||||
}
|
||||
};
|
||||
public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g){
|
||||
switch (deckType){
|
||||
case OneDeck:
|
||||
g.setColor(MainColor);
|
||||
g.fillRect(_startPosX + 30, _startPosY + 30, 60, 10);
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(_startPosX + 30, _startPosY + 30, 60, 10);
|
||||
break;
|
||||
case TwoDecks:
|
||||
g.setColor(MainColor);
|
||||
g.fillRect(_startPosX + 30, _startPosY + 30, 60, 10);
|
||||
g.fillRect(_startPosX + 36, _startPosY + 22, 54, 8);
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(_startPosX + 30, _startPosY + 30, 60, 10);
|
||||
g.drawRect(_startPosX + 36, _startPosY + 22, 54, 8);
|
||||
break;
|
||||
case ThreeDecks:
|
||||
g.setColor(MainColor);
|
||||
g.fillRect(_startPosX + 30, _startPosY + 30, 60, 10);
|
||||
g.fillRect(_startPosX + 36, _startPosY + 22, 54, 8);
|
||||
g.fillRect(_startPosX + 50, _startPosY + 14, 40, 8);
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(_startPosX + 30, _startPosY + 30, 60, 10);
|
||||
g.drawRect(_startPosX + 36, _startPosY + 22, 54, 8);
|
||||
g.drawRect(_startPosX + 50, _startPosY + 14, 40, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
112
scr/DrawingWarmlyShip.java
Normal file
112
scr/DrawingWarmlyShip.java
Normal file
@ -0,0 +1,112 @@
|
||||
import java.awt.*;
|
||||
public class DrawingWarmlyShip {
|
||||
private EntityWarmlyShip entityWarmlyShip;
|
||||
public EntityWarmlyShip GetEntityWarmlyShip(){
|
||||
return entityWarmlyShip;
|
||||
}
|
||||
private void SetEntityWarmlyShip(EntityWarmlyShip entityWarmlyShip){
|
||||
this.entityWarmlyShip = entityWarmlyShip;
|
||||
}
|
||||
private DrawingDecks drawingDecks;
|
||||
public DrawingDecks GetDrawingDecks(){
|
||||
return drawingDecks;
|
||||
}
|
||||
private void SetDrawingDecks( DrawingDecks drawingDecks){
|
||||
this.drawingDecks = drawingDecks;
|
||||
}
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private final int _shipWidth = 100;
|
||||
private final int _shipHeight = 70;
|
||||
public boolean Init(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment, int width, int height, int numberDecks){
|
||||
if (width < _shipWidth || height <_shipHeight){
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityWarmlyShip = new EntityWarmlyShip();
|
||||
entityWarmlyShip.Init(speed, weight, mainColor, optionalColor, pipes, fuelCompartment);
|
||||
drawingDecks = new DrawingDecks();
|
||||
drawingDecks.SetNumberDecks(numberDecks);
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y){
|
||||
if (x < 0 || x + _shipWidth > _pictureWidth){
|
||||
x = 20;
|
||||
}
|
||||
if (y < 0 || y + _shipHeight > _pictureHeight){
|
||||
y = 20;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (entityWarmlyShip == null){
|
||||
return;
|
||||
}
|
||||
switch (direction){
|
||||
case Left:
|
||||
System.out.println(_startPosX + " " + entityWarmlyShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityWarmlyShip.GetStep() + _shipWidth) + " " + _pictureWidth);
|
||||
if (_startPosX - entityWarmlyShip.GetStep() > 0){
|
||||
_startPosX -= (int)entityWarmlyShip.GetStep();
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
System.out.println(_startPosY + " " + entityWarmlyShip.GetStep() + " " + (_startPosY + entityWarmlyShip.GetStep()) + " " + _pictureHeight);
|
||||
if (_startPosY - entityWarmlyShip.GetStep() > 0){
|
||||
_startPosY -= (int)entityWarmlyShip.GetStep();
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
System.out.println(_startPosX + " " + entityWarmlyShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityWarmlyShip.GetStep() + _shipWidth) + " " + _pictureWidth);
|
||||
if (_startPosX + entityWarmlyShip.GetStep() + _shipWidth < _pictureWidth){
|
||||
_startPosX += (int)entityWarmlyShip.GetStep();
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
System.out.println(_startPosY + " " + entityWarmlyShip.GetStep() + " " + (_startPosY + entityWarmlyShip.GetStep()) + " " + _pictureHeight);
|
||||
if (_startPosY + entityWarmlyShip.GetStep() + _shipHeight < _pictureHeight){
|
||||
_startPosY += (int)entityWarmlyShip.GetStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g){
|
||||
if (entityWarmlyShip == null || drawingDecks == null){
|
||||
System.out.println("?????????");
|
||||
return;
|
||||
}
|
||||
if (entityWarmlyShip.GetPipes()){
|
||||
g.setColor(entityWarmlyShip.GetOptionalColor());
|
||||
g.fillRect(_startPosX + 70, _startPosY, 10, 30);
|
||||
g.fillRect(_startPosX + 50, _startPosY + 10, 10, 20);
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(_startPosX + 50, _startPosY + 10, 10, 20);
|
||||
g.drawRect(_startPosX + 70, _startPosY, 10, 30);
|
||||
}
|
||||
if (entityWarmlyShip.GetFuelCompartment())
|
||||
{
|
||||
g.setColor(entityWarmlyShip.GetOptionalColor());
|
||||
g.fillRect(_startPosX + 10, _startPosY + 30, 10, 10);
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(_startPosX + 10, _startPosY + 30, 10, 10);
|
||||
}
|
||||
//палуба
|
||||
drawingDecks.DrawingDecks(_startPosX, _startPosY, entityWarmlyShip.GetMainColor(), g);
|
||||
//корпус
|
||||
int[] xPoints = {_startPosX, _startPosX + 100, _startPosX + 90, _startPosX + 20, _startPosX};
|
||||
int[] yPoints = {_startPosY + 40, _startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40};
|
||||
int nPoints = 5;
|
||||
g.setColor(entityWarmlyShip.GetMainColor());
|
||||
g.fillPolygon(xPoints, yPoints, nPoints);
|
||||
g.setColor(Color.black);
|
||||
g.drawPolygon(xPoints, yPoints, nPoints);
|
||||
//якорь
|
||||
g.drawLine(_startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55);
|
||||
g.drawLine(_startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55);
|
||||
}
|
||||
|
||||
}
|
58
scr/EntityWarmlyShip.java
Normal file
58
scr/EntityWarmlyShip.java
Normal file
@ -0,0 +1,58 @@
|
||||
import java.awt.*;
|
||||
public class EntityWarmlyShip {
|
||||
private int Speed;
|
||||
public int GetSpeed(){
|
||||
return Speed;
|
||||
}
|
||||
private void SetSpeed(int speed){
|
||||
Speed = speed;
|
||||
}
|
||||
private double Weight;
|
||||
public double GetWeight(){
|
||||
return Weight;
|
||||
}
|
||||
private void SetWeight(int weight){
|
||||
Weight = weight;
|
||||
}
|
||||
private Color MainColor;
|
||||
public Color GetMainColor(){
|
||||
return MainColor;
|
||||
}
|
||||
private void SetMainColor(Color mainColor){
|
||||
MainColor = mainColor;
|
||||
}
|
||||
private Color OptionalColor;
|
||||
public Color GetOptionalColor(){
|
||||
return OptionalColor;
|
||||
}
|
||||
private void SetOptionalColor(Color optionalColor){
|
||||
OptionalColor = optionalColor;
|
||||
}
|
||||
private boolean Pipes;
|
||||
public boolean GetPipes(){
|
||||
return Pipes;
|
||||
}
|
||||
private void SetPipes(boolean pipes){
|
||||
Pipes = pipes;
|
||||
}
|
||||
private boolean FuelCompartment;
|
||||
public boolean GetFuelCompartment(){
|
||||
return FuelCompartment;
|
||||
}
|
||||
private void SetFuelCompartment(boolean fuelCompartment){
|
||||
FuelCompartment = fuelCompartment;
|
||||
}
|
||||
private double Step;
|
||||
public double GetStep() {
|
||||
return (double)Speed * 100 / Weight;
|
||||
}
|
||||
public void Init(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
MainColor = mainColor;
|
||||
OptionalColor = optionalColor;
|
||||
Pipes = pipes;
|
||||
FuelCompartment = fuelCompartment;
|
||||
}
|
||||
}
|
117
scr/WarmlyShipForm.java
Normal file
117
scr/WarmlyShipForm.java
Normal file
@ -0,0 +1,117 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.*;
|
||||
|
||||
public class WarmlyShipForm{
|
||||
private DrawingWarmlyShip _drawingWarmlyShip;
|
||||
Canvas canv;
|
||||
public void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
public WarmlyShipForm(){
|
||||
JFrame frame = new JFrame("Warmly Ship");
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
buttonCreate.setFocusPainted(false);
|
||||
buttonCreate.setContentAreaFilled(false);
|
||||
JButton buttonUp = new JButton();
|
||||
//buttonUp.setBorderPainted(false); //граница кнопки
|
||||
buttonUp.setFocusPainted(false); //контур вокруг текста
|
||||
buttonUp.setContentAreaFilled(false); //раскраска конпки
|
||||
buttonUp.setName("up"); //имя кнопки при обработке нажания
|
||||
buttonUp.setIcon(new ImageIcon(((new ImageIcon("images/ArrowUp.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||
JButton buttonDown = new JButton();
|
||||
buttonDown.setFocusPainted(false);
|
||||
buttonDown.setContentAreaFilled(false);
|
||||
buttonDown.setName("down");
|
||||
buttonDown.setIcon(new ImageIcon(((new ImageIcon("images/ArrowDown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||
JButton buttonLeft = new JButton();
|
||||
buttonLeft.setFocusPainted(false);
|
||||
buttonLeft.setContentAreaFilled(false);
|
||||
buttonLeft.setName("left");
|
||||
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("images/ArrowLeft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||
JButton buttonRight = new JButton();
|
||||
buttonRight.setFocusPainted(false);
|
||||
buttonRight.setContentAreaFilled(false);
|
||||
buttonRight.setName("right");
|
||||
buttonRight.setIcon(new ImageIcon(((new ImageIcon("images/ArrowRight.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();
|
||||
_drawingWarmlyShip = new DrawingWarmlyShip();
|
||||
_drawingWarmlyShip.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.nextBoolean(), random.nextBoolean(), 900, 460, random.nextInt(3) + 1);
|
||||
_drawingWarmlyShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
canv._drawingWarmlyShip = _drawingWarmlyShip;
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawingWarmlyShip == null){
|
||||
return;
|
||||
}
|
||||
switch ((((JButton)(e.getSource())).getName())){
|
||||
case "up":
|
||||
_drawingWarmlyShip.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawingWarmlyShip.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawingWarmlyShip.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawingWarmlyShip.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
};
|
||||
buttonUp.addActionListener(actionListener);
|
||||
buttonDown.addActionListener(actionListener);
|
||||
buttonLeft.addActionListener(actionListener);
|
||||
buttonRight.addActionListener(actionListener);
|
||||
frame.setSize(920, 500);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setBounds(0, 0, 900, 500);
|
||||
buttonCreate.setBounds(2, 420, 100, 40);
|
||||
buttonUp.setBounds(800, 380, 40, 40);
|
||||
buttonDown.setBounds(800, 420, 40, 40);
|
||||
buttonLeft.setBounds(760, 420, 40, 40);
|
||||
buttonRight.setBounds(840, 420, 40, 40);
|
||||
frame.add(canv);
|
||||
frame.add(buttonCreate);
|
||||
frame.add(buttonUp);
|
||||
frame.add(buttonDown);
|
||||
frame.add(buttonLeft);
|
||||
frame.add(buttonRight);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
class Canvas extends JComponent{
|
||||
public DrawingWarmlyShip _drawingWarmlyShip;
|
||||
public Canvas(){}
|
||||
|
||||
public void paintComponent(Graphics g){
|
||||
if (_drawingWarmlyShip == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
_drawingWarmlyShip.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user