DopHarder
This commit is contained in:
parent
d94c613ddf
commit
9ae0458bb2
@ -26,6 +26,12 @@ public class DrawingLoco extends DrawingTrain{
|
||||
EntityTrain = new EntityLoco(speed, weight, bodyColor, _numWheel, additionalColor, tube, fuelTank, locoLine);
|
||||
_locoWidth = ((EntityLoco)EntityTrain).FuelTank ? 169 : 83;
|
||||
}
|
||||
// конструктор для 3 сложной лабы
|
||||
public DrawingLoco(EntityLoco train, IWheelDrawing _wheelDrawing, int width, int height ){
|
||||
super(train, _wheelDrawing, width, height);
|
||||
if (height < _locoHeight || width < _locoWidth)
|
||||
return;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
|
@ -70,6 +70,16 @@ public class DrawingTrain {
|
||||
}
|
||||
wheelDrawing.setNumWheel(_numWheel);
|
||||
}
|
||||
// конструктор для 3 сложной лабы
|
||||
public DrawingTrain(EntityTrain train, IWheelDrawing _wheelDrawing, int width, int height ){
|
||||
if (height < _locoHeight || width < _locoWidth)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityTrain = train;
|
||||
wheelDrawing = _wheelDrawing;
|
||||
wheelDrawing.setNumWheel(EntityTrain.numWheel);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
|
62
laba1Loco/Form4GenericDopClass.java
Normal file
62
laba1Loco/Form4GenericDopClass.java
Normal file
@ -0,0 +1,62 @@
|
||||
package laba1Loco;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Form4GenericDopClass extends JFrame {
|
||||
static int pictureBoxWidth = 980;
|
||||
static int pictureBoxHeight = 560;
|
||||
public DrawingTrain _drawingTrain;
|
||||
private class Canvas extends JComponent{
|
||||
public Canvas(){
|
||||
}
|
||||
public void paintComponent (Graphics g){
|
||||
if (_drawingTrain == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
_drawingTrain.SetPosition(50, 50);
|
||||
_drawingTrain.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
GenericDopClass<EntityTrain, IWheelDrawing> genericDopClass;
|
||||
public Form4GenericDopClass(){
|
||||
_drawingTrain = null;
|
||||
Canvas canv = new Canvas();
|
||||
setSize (1000, 600);
|
||||
setLayout(null);
|
||||
canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
|
||||
|
||||
genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight);
|
||||
genericDopClass.addTrain(new EntityTrain(100, 100, Color.BLUE, 2));
|
||||
genericDopClass.addTrain(new EntityTrain(100, 100, Color.RED, 3));
|
||||
genericDopClass.addTrain(new EntityTrain(100, 100, Color.GRAY, 4));
|
||||
genericDopClass.addTrain(new EntityLoco(100, 100, Color.BLUE, 2, Color.ORANGE, true, true, true));
|
||||
genericDopClass.addWheel(new WheelDrawingDavidStar());
|
||||
genericDopClass.addWheel(new WheelDrawingBalls());
|
||||
|
||||
JButton creatButton = new JButton("createButton");
|
||||
creatButton.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
_drawingTrain = genericDopClass.getRDrawingObject();
|
||||
canv.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
creatButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20);
|
||||
|
||||
add(canv);
|
||||
add(creatButton);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ import javax.swing.Timer;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class FormTrain{
|
||||
class Canvas extends JComponent{
|
||||
private class Canvas extends JComponent{
|
||||
public DrawingTrain _drawingTrain;
|
||||
public Canvas(){
|
||||
}
|
||||
@ -208,7 +208,6 @@ public class FormTrain{
|
||||
right.addActionListener(actioListener);
|
||||
|
||||
w.setSize (1000, 600);
|
||||
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||
w.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
|
||||
|
@ -12,7 +12,7 @@ import javax.swing.JOptionPane;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class FormTrainCollecltion {
|
||||
class Canvas extends JComponent{
|
||||
private class Canvas extends JComponent{
|
||||
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
|
||||
public Canvas(){
|
||||
}
|
||||
@ -112,6 +112,15 @@ public class FormTrainCollecltion {
|
||||
}
|
||||
);
|
||||
|
||||
JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass");
|
||||
toForm4GenericDopClass.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
w.setSize (1000, 600);
|
||||
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||
w.setLayout(null);
|
||||
@ -120,11 +129,13 @@ public class FormTrainCollecltion {
|
||||
TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20);
|
||||
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20);
|
||||
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20);
|
||||
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20);
|
||||
w.add(canv);
|
||||
w.add(ButtonAddTrain);
|
||||
w.add(ButtonRemoveTrain);
|
||||
w.add(ButtonRefreshCollection);
|
||||
w.add(TextBoxNumber);
|
||||
w.add(toForm4GenericDopClass);
|
||||
w.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
67
laba1Loco/GenericDopClass.java
Normal file
67
laba1Loco/GenericDopClass.java
Normal file
@ -0,0 +1,67 @@
|
||||
package laba1Loco;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
|
||||
|
||||
private Object[] Trains;
|
||||
private Object[] Wheels;
|
||||
private int maxCountTrains;
|
||||
private int countTrains;
|
||||
private int maxCountWheels;
|
||||
private int countWheels;
|
||||
private Random random;
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
|
||||
public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){
|
||||
maxCountTrains = _maxCountTrains;
|
||||
maxCountWheels = _maxCountWheels;
|
||||
Trains = new Object[maxCountTrains];
|
||||
Wheels = new Object[maxCountWheels];
|
||||
countTrains = 0;
|
||||
countWheels = 0;
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
public boolean addTrain(T train){
|
||||
if (train == null)
|
||||
return false;
|
||||
if (countTrains > maxCountTrains)
|
||||
return false;
|
||||
Trains[countTrains++] = train;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean addWheel(U wheel){
|
||||
if (wheel == null)
|
||||
return false;
|
||||
if (countWheels > maxCountWheels)
|
||||
return false;
|
||||
Wheels[countWheels++] = wheel;
|
||||
return true;
|
||||
}
|
||||
|
||||
public DrawingTrain getRDrawingObject(){
|
||||
if (countTrains == 0 || countWheels == 0)
|
||||
return null;
|
||||
int i = random.nextInt(countTrains);
|
||||
int j = random.nextInt(countWheels);
|
||||
DrawingTrain drawingTrain;
|
||||
if (Trains[i] instanceof EntityLoco){
|
||||
drawingTrain = new DrawingLoco((EntityLoco)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight);
|
||||
}
|
||||
else{
|
||||
drawingTrain = new DrawingTrain((EntityTrain)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight);
|
||||
}
|
||||
return drawingTrain;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user