From 8e0a97dd2bc81460e4c117755bb2f1775a3c646b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 20:32:35 +0400 Subject: [PATCH] Added & fixed FormMap --- DrawningWarmlyLocomotive.java | 2 +- FormLocomotive.java | 5 +- FormMap.java | 126 ++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 FormMap.java diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index cdc0625..bc5cc09 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -2,7 +2,7 @@ import java.awt.*; public class DrawningWarmlyLocomotive extends DrawningLocomotive{ public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage) { - super(speed, weight, bodyColor, 130, 70); + super(speed, weight, bodyColor, 140, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } @Override diff --git a/FormLocomotive.java b/FormLocomotive.java index 6616754..7535926 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -99,9 +99,6 @@ public class FormLocomotive extends JComponent{ formFrame.getContentPane().add(this); } - - - @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -111,6 +108,6 @@ public class FormLocomotive extends JComponent{ } public static void main(String[] args) { - new FormLocomotive(); + new FormMap(); } } diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..9741bc5 --- /dev/null +++ b/FormMap.java @@ -0,0 +1,126 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class FormMap extends JComponent { + + private AbstractMap _abstractMap; + private BufferedImage bufferImg = null; + + public FormMap() { + JFrame formFrame = new JFrame("Form Map"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(1000, 500); + formFrame.setVisible(true); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + Label speedLabel = new Label("Speed: "); + Label weightLabel = new Label("Weight: "); + Label colorLabel = new Label("Color: "); + + String[] maps = { + "Simple Map", + "Spike Map", + "Rail Map" + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String item = (String)mapSelectComboBox.getSelectedItem(); + if (item == null) return; + switch (item) { + case ("Simple Map"): + _abstractMap = new SimpleMap(); + break; + case ("Spike Map"): + break; + case ("Rail Map"): + break; + } + } + }); + + JButton createButton = new JButton("Create"); + createButton.addActionListener(e -> { + Random rnd = new Random(); + var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); + weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); + colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue()); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); + repaint(); + }); + + JButton modifiedButton = new JButton("Modified"); + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + var locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean()); + speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); + weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); + colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() ); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); + repaint(); + }); + statusPanel.add(mapSelectComboBox); + statusPanel.add(createButton); + statusPanel.add(modifiedButton); + statusPanel.add(speedLabel); + statusPanel.add(weightLabel); + statusPanel.add(colorLabel); + + JButton moveDownButton = new JButton("D"); + moveDownButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); + repaint(); + }); + + JButton moveUpButton = new JButton("U"); + moveUpButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); + repaint(); + }); + + JButton moveLeftButton = new JButton("L"); + moveLeftButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); + repaint(); + }); + + JButton moveRightButton = new JButton("R"); + moveRightButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Right); + repaint(); + }); + + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + + formFrame.getContentPane().add(this); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,500,null); + super.repaint(); + } + +}