diff --git a/src/MonorailHard/DrawningObjects/DrawningLocomotive.java b/src/MonorailHard/DrawningObjects/DrawningLocomotive.java index 6c3c2ae..e09e382 100644 --- a/src/MonorailHard/DrawningObjects/DrawningLocomotive.java +++ b/src/MonorailHard/DrawningObjects/DrawningLocomotive.java @@ -7,8 +7,8 @@ import java.awt.*; public class DrawningLocomotive extends DrawningMonorail{ public DrawningLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb, - int width, int height, boolean secondCabine, boolean magniteRail, Color additionalColor, JPanel monorailPanel){ - super(speed, weight, bodyColor, wheelColor, tireColor, width, height, monorailPanel); + int width, int height, boolean secondCabine, boolean magniteRail, Color additionalColor){ + super(speed, weight, bodyColor, wheelColor, tireColor, width, height); if(EntityMonorail() != null){ EntityMonorail = new EntityLocomotive(speed, weight, bodyColor, wheelColor, tireColor, wheelNumb, secondCabine, magniteRail, additionalColor); @@ -16,16 +16,15 @@ public class DrawningLocomotive extends DrawningMonorail{ } @Override - public void DrawMonorail(){ + public void DrawMonorail(Graphics2D g2d){ if (!(EntityMonorail instanceof EntityLocomotive)) { return; } - super.DrawMonorail(); + super.DrawMonorail(g2d); int dif = _monorailWidth / 10; _monorailWidth -= dif; EntityLocomotive _locomotive = (EntityLocomotive) EntityMonorail; - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); //вторая кабина if (_locomotive.SecondCabine()) { int[] pointsSecondCabineX = {_startPosX + _monorailWidth / 20 * 19, diff --git a/src/MonorailHard/DrawningObjects/DrawningMonorail.java b/src/MonorailHard/DrawningObjects/DrawningMonorail.java index 9b8a2e0..1f03901 100644 --- a/src/MonorailHard/DrawningObjects/DrawningMonorail.java +++ b/src/MonorailHard/DrawningObjects/DrawningMonorail.java @@ -11,7 +11,6 @@ import java.util.Random; public class DrawningMonorail { - protected JPanel MonorailPanel; protected EntityMonorail EntityMonorail; private int _pictureWidth; private int _pictureHeight; @@ -26,40 +25,34 @@ public class DrawningMonorail { return EntityMonorail; } public DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, - int width, int height, JPanel monorailPanel){ + int width, int height){ if(width <= _monorailWidth || height <= _monorailHeight) return; _startPosY=0; _startPosX = 0; - monorailPanel.setSize(width, height); - MonorailPanel = monorailPanel; - monorailPanel.paint(MonorailPanel.getGraphics()); wheelSz = _monorailHeight - _monorailHeight * 7 / 10; _pictureWidth = width; _pictureHeight = height; EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor); int dif = _monorailWidth / 10; - DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); Random rand = new Random(); int variant = rand.nextInt(0, 3); if(variant == 0) - DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); else if(variant == 1) - DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); else - DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6)); } protected DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, - int width, int height, int monorailWidth, int monorailHeight, JPanel monorailPanel){ + int width, int height, int monorailWidth, int monorailHeight){ if(width <= _monorailWidth || height <= _monorailHeight) return; _startPosY=0; _startPosX = 0; - monorailPanel.setSize(width, height); - MonorailPanel = monorailPanel; - monorailPanel.paint(MonorailPanel.getGraphics()); wheelSz = _monorailHeight - _monorailHeight * 7 / 10; _pictureWidth = width; _pictureHeight = height; @@ -70,11 +63,11 @@ public class DrawningMonorail { Random rand = new Random(); int variant = rand.nextInt(0, 3); if(variant == 0) - DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); else if(variant == 1) - DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); else - DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor); DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6)); @@ -123,7 +116,6 @@ public class DrawningMonorail { public void MoveTransport(DirectionType direction){ if (!CanMove(direction) || EntityMonorail == null) return; - MonorailPanel.paint(MonorailPanel.getGraphics()); switch (direction) { case Left: @@ -147,8 +139,7 @@ public class DrawningMonorail { DrawningWheels.ChangeY(_startPosY); } - public void DrawMonorail(){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawMonorail(Graphics2D g2d){ if (EntityMonorail == null) return; int dif = _monorailWidth / 10; @@ -231,7 +222,7 @@ public class DrawningMonorail { g2d.setColor(Color.BLUE); g2d.drawRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height); //колеса - DrawningWheels.DrawWheels(); + DrawningWheels.DrawWheels(g2d); _monorailWidth += dif; } } diff --git a/src/MonorailHard/DrawningObjects/DrawningWheels.java b/src/MonorailHard/DrawningObjects/DrawningWheels.java index 460bbaf..4caa45c 100644 --- a/src/MonorailHard/DrawningObjects/DrawningWheels.java +++ b/src/MonorailHard/DrawningObjects/DrawningWheels.java @@ -7,7 +7,6 @@ import java.awt.*; public class DrawningWheels implements IDraw{ private int WheelSz; - JPanel MonorailPanel; private NumberType WheelsNumb; private Color WheelColor, TireColor; private int Width, Height; @@ -16,7 +15,7 @@ public class DrawningWheels implements IDraw{ return WheelSz; } - public DrawningWheels(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ + public DrawningWheels(int width, int height, int curX, int curY, Color wheelColor, Color tireColor){ Width = width; Height = height; CurX = curX; @@ -24,7 +23,6 @@ public class DrawningWheels implements IDraw{ WheelColor = wheelColor; TireColor = tireColor; WheelSz = Height - Height * 7 / 10; - MonorailPanel = monorailPanel; } public void ChangeX(int x){ CurX = x; @@ -45,16 +43,14 @@ public class DrawningWheels implements IDraw{ return WheelsNumb; } - public void DrawWheel(int x, int y){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawWheel(Graphics2D g2d, int x, int y){ g2d.setColor(WheelColor); g2d.fillOval( x, y , WheelSz, WheelSz); g2d.setColor(TireColor); g2d.drawOval(x, y, WheelSz, WheelSz); } - public void DrawWheels(){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawWheels(Graphics2D g2d){ //передняя часть тележки int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2, CurX, CurX + Width / 10 * 4, @@ -73,15 +69,15 @@ public class DrawningWheels implements IDraw{ g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length); - DrawWheel(CurX + Width / 10, CurY + Height / 10 * 7); - DrawWheel(CurX + Width / 10 * 8, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10 * 8, CurY + Height / 10 * 7); //3 колеса if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four) - DrawWheel(CurX + Width / 10 * 6, CurY + Height / 10 * 7); + DrawWheel(g2d,CurX + Width / 10 * 6, CurY + Height / 10 * 7); //4 колеса if (WheelsNumb == NumberType.Four) - DrawWheel(CurX + Width / 10 * 3, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10 * 3, CurY + Height / 10 * 7); } diff --git a/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java b/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java index 4994f71..fce5f86 100644 --- a/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java +++ b/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java @@ -7,7 +7,6 @@ import java.awt.*; public class DrawningWheelsCart implements IDraw{ private int WheelSz; - JPanel MonorailPanel; private NumberType WheelsNumb; private Color WheelColor, TireColor; private int Width, Height; @@ -16,7 +15,7 @@ public class DrawningWheelsCart implements IDraw{ return WheelSz; } - public DrawningWheelsCart(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ + public DrawningWheelsCart(int width, int height, int curX, int curY, Color wheelColor, Color tireColor){ Width = width; Height = height; CurX = curX; @@ -24,7 +23,6 @@ public class DrawningWheelsCart implements IDraw{ WheelColor = wheelColor; TireColor = tireColor; WheelSz = Height - Height * 7 / 10; - MonorailPanel = monorailPanel; } public void ChangeWheelsNumb(int x){ @@ -46,16 +44,14 @@ public class DrawningWheelsCart implements IDraw{ public void ChangeY(int y){ CurY = y; } - public void DrawWheel(int x, int y){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawWheel(Graphics2D g2d, int x, int y){ g2d.setColor(WheelColor); g2d.fillOval( x, y , WheelSz, WheelSz); g2d.setColor(TireColor); g2d.drawOval(x, y, WheelSz, WheelSz); } - public void DrawWheels(){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawWheels(Graphics2D g2d){ //передняя часть тележки int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2, CurX, CurX + Width / 10 * 4, @@ -80,14 +76,14 @@ public class DrawningWheelsCart implements IDraw{ g2d.drawLine(CurX + Width / 10 * 6, CurY + Height / 10 * 7,CurX + Width / 10 * 9, CurY + Height / 10 * 9); g2d.drawLine(CurX + Width / 10 * 9, CurY + Height / 10 * 7,CurX + Width / 10 * 6, CurY + Height / 10 * 9); - DrawWheel(CurX + Width / 10, CurY + Height / 10 * 7); - DrawWheel(CurX + Width / 10 * 8, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10 * 8, CurY + Height / 10 * 7); //3 колеса if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four) - DrawWheel(CurX + Width / 10 * 6, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10 * 6, CurY + Height / 10 * 7); //4 колеса if (WheelsNumb == NumberType.Four) - DrawWheel(CurX + Width / 10 * 3, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10 * 3, CurY + Height / 10 * 7); } } diff --git a/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java b/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java index bb81b8d..cdaf2e2 100644 --- a/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java +++ b/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java @@ -7,7 +7,6 @@ import java.awt.*; public class DrawningWheelsOrn implements IDraw{ private int WheelSz; - JPanel MonorailPanel; private NumberType WheelsNumb; private Color WheelColor, TireColor; private int Width, Height; @@ -16,7 +15,7 @@ public class DrawningWheelsOrn implements IDraw{ return WheelSz; } - public DrawningWheelsOrn(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ + public DrawningWheelsOrn(int width, int height, int curX, int curY, Color wheelColor, Color tireColor){ Width = width; Height = height; CurX = curX; @@ -24,7 +23,6 @@ public class DrawningWheelsOrn implements IDraw{ WheelColor = wheelColor; TireColor = tireColor; WheelSz = Height - Height * 7 / 10; - MonorailPanel = monorailPanel; } public void ChangeWheelsNumb(int x){ @@ -46,8 +44,7 @@ public class DrawningWheelsOrn implements IDraw{ public void ChangeY(int y){ CurY = y; } - public void DrawWheel(int x, int y){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawWheel(Graphics2D g2d, int x, int y){ g2d.setColor(WheelColor); g2d.fillOval( x, y , WheelSz, WheelSz); g2d.setColor(TireColor); @@ -56,8 +53,7 @@ public class DrawningWheelsOrn implements IDraw{ g2d.drawLine(x + WheelSz / 2, y, x + WheelSz / 2, y + WheelSz); } - public void DrawWheels(){ - Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + public void DrawWheels(Graphics2D g2d){ //передняя часть тележки int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2, CurX, CurX + Width / 10 * 4, @@ -76,14 +72,14 @@ public class DrawningWheelsOrn implements IDraw{ g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length); - DrawWheel(CurX + Width / 10, CurY + Height / 10 * 7); - DrawWheel(CurX + Width / 10 * 8, CurY + Height / 10 * 7); + DrawWheel(g2d, CurX + Width / 10, CurY + Height / 10 * 7); + DrawWheel(g2d,CurX + Width / 10 * 8, CurY + Height / 10 * 7); //3 колеса if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four) - DrawWheel(CurX + Width / 10 * 6, CurY + Height / 10 * 7); + DrawWheel(g2d,CurX + Width / 10 * 6, CurY + Height / 10 * 7); //4 колеса if (WheelsNumb == NumberType.Four) - DrawWheel(CurX + Width / 10 * 3, CurY + Height / 10 * 7); + DrawWheel(g2d,CurX + Width / 10 * 3, CurY + Height / 10 * 7); } } diff --git a/src/MonorailHard/DrawningObjects/IDraw.java b/src/MonorailHard/DrawningObjects/IDraw.java index 7dfe11a..072df79 100644 --- a/src/MonorailHard/DrawningObjects/IDraw.java +++ b/src/MonorailHard/DrawningObjects/IDraw.java @@ -2,11 +2,13 @@ package MonorailHard.DrawningObjects; import MonorailHard.NumberType; +import java.awt.*; + public interface IDraw { public void ChangeWheelsNumb(int x); public NumberType WheelsNumb(); - public void DrawWheels(); - public void DrawWheel(int x, int y); + public void DrawWheels(Graphics2D g2d); + public void DrawWheel(Graphics2D g2d, int x, int y); public void ChangeX(int x); public void ChangeY(int y); } diff --git a/src/MonorailHard/Main.java b/src/MonorailHard/Main.java index 50dcfd0..1191b22 100644 --- a/src/MonorailHard/Main.java +++ b/src/MonorailHard/Main.java @@ -14,157 +14,7 @@ import javax.imageio.ImageIO; import javax.swing.*; public class Main { - static DrawningMonorail DrawningMonorail; - static AbstractStrategy _abstractStrategy; public static void main(String[] args) throws IOException { - String[] items = {"Довести до центра", "Довести до края"}; - - JComboBox comboBoxStrategy = new JComboBox(items); - comboBoxStrategy.setBounds(562,12,151,28); - JFrame MonorailFrame = new JFrame(); - MonorailFrame.setResizable(false); - JPanel MonorailPanel = new JPanel(); - MonorailFrame.setLayout(new BorderLayout()); - MonorailFrame.setSize(743, 576); - MonorailFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - MonorailFrame.setLayout(new BorderLayout(1,1)); - MonorailPanel.setLayout(null); - BufferedImage RightIcon = ImageIO.read(new File("RightButton.png")); - BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png")); - BufferedImage UpIcon = ImageIO.read(new File("UpButton.png")); - BufferedImage DownIcon = ImageIO.read(new File("DownButton.png")); - - JButton RightButton = new JButton(new ImageIcon(RightIcon)); - JButton LeftButton = new JButton(new ImageIcon(LeftIcon)); - JButton UpButton = new JButton(new ImageIcon(UpIcon)); - JButton DownButton = new JButton(new ImageIcon(DownIcon)); - JButton CreateButton = new JButton(); - JButton CreateLocomotiveButton = new JButton(); - JButton buttonStep = new JButton(); - - CreateLocomotiveButton.setBounds(198,477,180, 40); - CreateLocomotiveButton.setText("Создать локомотив"); - CreateButton.setText("Создать"); - buttonStep.setBounds(619, 46, 94, 29); - buttonStep.setText("шаг"); - CreateButton.setBounds(12, 477, 180, 40); - RightButton.setBounds(683,487,30,30); - LeftButton.setBounds(611,487,30,30); - UpButton.setBounds(647,451,30,30); - DownButton.setBounds(647,487,30,30); - MonorailPanel.add(CreateButton); - MonorailPanel.add(CreateLocomotiveButton); - MonorailPanel.add(RightButton); - MonorailPanel.add(LeftButton); - MonorailPanel.add(UpButton); - MonorailPanel.add(DownButton); - MonorailPanel.add(comboBoxStrategy); - MonorailPanel.add(buttonStep); - comboBoxStrategy.setSelectedIndex(-1); - - - MonorailFrame.add(MonorailPanel, BorderLayout.CENTER); - Random random = new Random(); - CreateButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - MonorailPanel.getWidth(), MonorailPanel.getHeight(), MonorailPanel); - DrawningMonorail.DrawMonorail(); - comboBoxStrategy.enable(true); - } - }); - - CreateLocomotiveButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - random.nextInt(1, 6), - MonorailPanel.getWidth(), MonorailPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - MonorailPanel); - DrawningMonorail.DrawMonorail(); - comboBoxStrategy.enable(true); - } - }); - - buttonStep.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningMonorail == null) - return; - if(comboBoxStrategy.isEnabled()){ - if(comboBoxStrategy.getSelectedIndex() == 0) - _abstractStrategy = new MoveToCenter(); - else if(comboBoxStrategy.getSelectedIndex() == 1) - _abstractStrategy = new MoveToBorder(); - else - _abstractStrategy = null; - if(_abstractStrategy == null) - return; - _abstractStrategy.SetData(new DrawningObjectMonorail(DrawningMonorail), MonorailPanel.getWidth(), - MonorailPanel.getHeight()); - comboBoxStrategy.enable(false); - } - if (_abstractStrategy == null) - { - return; - } - _abstractStrategy.MakeStep(); - DrawningMonorail.DrawMonorail(); - if (_abstractStrategy.GetStatus() == Status.Finish) - { - comboBoxStrategy.enable(true); - _abstractStrategy = null; - } - - } - - }); - RightButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningMonorail.EntityMonorail() == null) { - return; - } - DrawningMonorail.MoveTransport(DirectionType.Right); - DrawningMonorail.DrawMonorail(); - } - }); - LeftButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningMonorail.EntityMonorail() == null) - return; - DrawningMonorail.MoveTransport(DirectionType.Left); - DrawningMonorail.DrawMonorail(); - } - }); - UpButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningMonorail.EntityMonorail() == null) - return; - DrawningMonorail.MoveTransport(DirectionType.Up); - DrawningMonorail.DrawMonorail(); - } - }); - DownButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningMonorail.EntityMonorail() == null) - return; - DrawningMonorail.MoveTransport(DirectionType.Down); - DrawningMonorail.DrawMonorail(); - } - }); - - MonorailFrame.setVisible(true); + FormMonorail form = new FormMonorail(); } }