Отрисовка объекта с доп.перечислением.
This commit is contained in:
parent
79ad67c00f
commit
cab773db88
6
Direction.java
Normal file
6
Direction.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum Direction {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
5
DirectionEnginesOnStormtrooper.java
Normal file
5
DirectionEnginesOnStormtrooper.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum DirectionEnginesOnStormtrooper {
|
||||
TWO,
|
||||
FOUR,
|
||||
SIX
|
||||
}
|
39
DrawingEngines.java
Normal file
39
DrawingEngines.java
Normal file
@ -0,0 +1,39 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEngines {
|
||||
private DirectionEnginesOnStormtrooper enginesCount;
|
||||
private Color color;
|
||||
public void SetNewEngines(int countEngines){
|
||||
if (countEngines == 4) {
|
||||
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
|
||||
} else if (countEngines == 6) {
|
||||
enginesCount = DirectionEnginesOnStormtrooper.SIX;
|
||||
}
|
||||
else {
|
||||
enginesCount = DirectionEnginesOnStormtrooper.TWO;
|
||||
}
|
||||
}
|
||||
public void Draw(Graphics2D g, int x, int y, Color color) {
|
||||
g.setColor(color != null ? color : Color.BLACK);
|
||||
switch (enginesCount) {
|
||||
case TWO:
|
||||
g.fillRect(x + 50, y, 20, 5);
|
||||
g.fillRect(x + 50, y+95, 20, 5);
|
||||
break;
|
||||
case FOUR:
|
||||
g.fillRect(x + 50, y, 20, 5);
|
||||
g.fillRect(x + 50, y+10, 20, 5);
|
||||
g.fillRect(x + 50, y+85, 20, 5);
|
||||
g.fillRect(x + 50, y+95, 20, 5);
|
||||
break;
|
||||
case SIX:
|
||||
g.fillRect(x + 50, y, 20, 5);
|
||||
g.fillRect(x + 50, y+10, 20, 5);
|
||||
g.fillRect(x + 50, y+20, 20, 5);
|
||||
g.fillRect(x + 50, y+75, 20, 5);
|
||||
g.fillRect(x + 50, y+85, 20, 5);
|
||||
g.fillRect(x + 50, y+95, 20, 5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
180
DrawingStormtrooper.java
Normal file
180
DrawingStormtrooper.java
Normal file
@ -0,0 +1,180 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingStormtrooper {
|
||||
public EntityStormtrooper Stormtrooper;
|
||||
public DrawingEngines drawingEngines;
|
||||
public float _startPosX;
|
||||
public float _startPosY;
|
||||
private Integer _pictureWidth = null;
|
||||
private Integer _pictureHeight = null;
|
||||
private static final int _StormWidth = 135;
|
||||
private static final int _StormHeight = 100;
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor, int numberOfEngines){
|
||||
Stormtrooper = new EntityStormtrooper();
|
||||
drawingEngines = new DrawingEngines();
|
||||
Stormtrooper.Init(speed, weight, bodyColor);
|
||||
System.out.println(numberOfEngines + "");
|
||||
drawingEngines.SetNewEngines(numberOfEngines);
|
||||
}
|
||||
|
||||
public EntityStormtrooper getStormtrooper() {
|
||||
return Stormtrooper;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height){
|
||||
if ((x > 0 && y > 0) && (_StormHeight + y < height) && (_StormWidth + x < width))
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (_pictureWidth == null || _pictureHeight == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
// вправо
|
||||
case RIGHT:
|
||||
if (_startPosX + _StormWidth + Stormtrooper.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += Stormtrooper.Step;
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case LEFT:
|
||||
if (_startPosX - Stormtrooper.Step > 0)
|
||||
{
|
||||
_startPosX -= Stormtrooper.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case UP:
|
||||
if (_startPosY - Stormtrooper.Step > 0)
|
||||
{
|
||||
_startPosY -= Stormtrooper.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DOWN:
|
||||
if (_startPosY + _StormHeight + Stormtrooper.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += Stormtrooper.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (_startPosX < 0 || _startPosY < 0
|
||||
|| _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int _startPosXInt = (int)_startPosX;
|
||||
int _startPosYInt = (int)_startPosY;
|
||||
|
||||
|
||||
|
||||
//фюзеляж самолёта
|
||||
g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20);
|
||||
|
||||
//отрисовка крыла
|
||||
int[] wingX =
|
||||
{
|
||||
_startPosXInt + 60 ,
|
||||
_startPosXInt + 80 ,
|
||||
_startPosXInt+90 ,
|
||||
_startPosXInt+90 ,
|
||||
_startPosXInt+80 ,
|
||||
_startPosXInt + 60
|
||||
};
|
||||
|
||||
int[] wingY =
|
||||
{
|
||||
_startPosYInt,
|
||||
_startPosYInt,
|
||||
_startPosYInt + 40,
|
||||
_startPosYInt + 60,
|
||||
_startPosYInt + 100,
|
||||
_startPosYInt + 100
|
||||
};
|
||||
g.setColor(Stormtrooper.getBodyColor());
|
||||
g.fillPolygon(wingX, wingY, wingY.length);
|
||||
|
||||
//нос самолёта
|
||||
int[] noseX =
|
||||
{
|
||||
_startPosXInt + 20,
|
||||
_startPosXInt + 20,
|
||||
_startPosXInt
|
||||
};
|
||||
|
||||
int[] noseY =
|
||||
{
|
||||
_startPosYInt + 60,
|
||||
_startPosYInt + 40,
|
||||
_startPosYInt +50
|
||||
};
|
||||
|
||||
g.fillPolygon(noseX, noseY, noseX.length);
|
||||
|
||||
//стабилизатор
|
||||
int[] stabX =
|
||||
{
|
||||
_startPosXInt + 120,
|
||||
_startPosXInt + 135,
|
||||
_startPosXInt + 135,
|
||||
_startPosXInt + 120
|
||||
};
|
||||
|
||||
int[] stabY =
|
||||
{
|
||||
_startPosYInt + 60,
|
||||
_startPosYInt + 80,
|
||||
_startPosYInt + 20,
|
||||
_startPosYInt + 40
|
||||
};
|
||||
|
||||
g.fillPolygon(stabX, stabY, stabX.length);
|
||||
|
||||
//отрисовка двигателей
|
||||
drawingEngines.Draw(g, (int) _startPosX, (int) _startPosY,Stormtrooper.getBodyColor());
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
|
||||
g.drawRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20);
|
||||
g.drawPolygon(wingX, wingY, wingX.length);
|
||||
g.drawPolygon(noseX, noseY, noseX.length);
|
||||
g.drawPolygon(stabX, stabY, stabX.length);
|
||||
}
|
||||
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureWidth <= _StormWidth || _pictureHeight <= _StormHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _StormWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _StormWidth;
|
||||
}
|
||||
if (_startPosY + _StormHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _StormHeight;
|
||||
}
|
||||
}
|
||||
}
|
142
FormStormtrooper.java
Normal file
142
FormStormtrooper.java
Normal file
@ -0,0 +1,142 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.ComponentListener;
|
||||
import java.util.Random;
|
||||
public class FormStormtrooper extends JComponent {
|
||||
private DrawingStormtrooper _stormtrooper;
|
||||
private EntityStormtrooper _entityStormtrooper;
|
||||
public static void main(String[] args) {
|
||||
FormStormtrooper formStormtrooper = new FormStormtrooper();
|
||||
}
|
||||
public FormStormtrooper() {
|
||||
JFrame form = new JFrame("Штурмовик");
|
||||
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
form.setSize(800, 500);
|
||||
form.setVisible(true);
|
||||
form.setLocationRelativeTo(null);
|
||||
form.addComponentListener(new ComponentListener() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
if(_stormtrooper != null) _stormtrooper.ChangeBorders(getWidth(), getHeight());
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentMoved(ComponentEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentShown(ComponentEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentHidden(ComponentEvent e) {
|
||||
}
|
||||
});
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
statusPanel.setLayout(new GridBagLayout());
|
||||
setLayout(new BorderLayout());
|
||||
form.add(statusPanel, BorderLayout.SOUTH);
|
||||
Label speedLabel = new Label("Скорость: ");
|
||||
Label weightLabel = new Label("Вес: ");
|
||||
Label colorLabel = new Label("Цвет: ");
|
||||
|
||||
|
||||
JButton createButton = new JButton("Создать");
|
||||
createButton.addActionListener(e -> {
|
||||
int[] countBlocks = {2, 4, 6};
|
||||
Random rnd = new Random();
|
||||
_stormtrooper = new DrawingStormtrooper();
|
||||
_stormtrooper.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
|
||||
rnd.nextInt(0, 256)), countBlocks[rnd.nextInt(0, 3)]);
|
||||
_stormtrooper.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 80);
|
||||
speedLabel.setText("Скорость: " + _stormtrooper.Stormtrooper.getSpeed());
|
||||
weightLabel.setText("Вес: " + (int)_stormtrooper.Stormtrooper.getWeight());
|
||||
colorLabel.setText("Цвет: " + _stormtrooper.Stormtrooper.getBodyColor().getRed() + " " + _stormtrooper.Stormtrooper.getBodyColor().getGreen() + " " + _stormtrooper.Stormtrooper.getBodyColor().getBlue() );
|
||||
repaint();
|
||||
|
||||
});
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
|
||||
c.fill = GridBagConstraints.NONE;
|
||||
c.anchor = GridBagConstraints.CENTER;
|
||||
c.weightx = 0.5;
|
||||
c.gridx = 0;
|
||||
c.gridy = 1;
|
||||
statusPanel.add(createButton,c);
|
||||
|
||||
c.fill = GridBagConstraints.HORIZONTAL;
|
||||
c.gridx = 1;
|
||||
c.gridy = 1;
|
||||
statusPanel.add(speedLabel,c);
|
||||
|
||||
c.gridx = 2;
|
||||
c.gridy = 1;
|
||||
statusPanel.add(weightLabel,c);
|
||||
|
||||
|
||||
c.gridx = 3;
|
||||
c.gridy = 1;
|
||||
c.gridwidth = 1;
|
||||
statusPanel.add(colorLabel,c);
|
||||
JButton upButton = new JButton("↑");
|
||||
JButton rightButton = new JButton("→");
|
||||
JButton leftButton = new JButton("←");
|
||||
JButton downButton = new JButton("↓");
|
||||
upButton.addActionListener(e -> {
|
||||
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.UP);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
rightButton.addActionListener(e -> {
|
||||
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.RIGHT);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
leftButton.addActionListener(e -> {
|
||||
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.LEFT);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
downButton.addActionListener(e -> {
|
||||
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.DOWN);
|
||||
repaint();
|
||||
});
|
||||
c.fill = GridBagConstraints.NONE;
|
||||
c.gridx = 8;
|
||||
c.gridy = 0;
|
||||
c.gridwidth = 1;
|
||||
statusPanel.add(upButton,c);
|
||||
|
||||
c.gridx = 7;
|
||||
c.gridy = 1;
|
||||
c.gridwidth = 1;
|
||||
statusPanel.add(leftButton,c);
|
||||
|
||||
c.gridx = 9;
|
||||
c.gridy = 1;
|
||||
c.gridwidth = 1;
|
||||
statusPanel.add(rightButton,c);
|
||||
c.gridx = 8;
|
||||
c.gridy = 1;
|
||||
c.gridwidth = 1;
|
||||
statusPanel.add(downButton,c);
|
||||
|
||||
form.getContentPane().add(this);
|
||||
}
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (_stormtrooper != null) _stormtrooper.DrawTransport(g2);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user