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"): _abstractMap = new SpikeMap(); break; case ("Rail Map"): _abstractMap = new RailMap(); 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, 490, 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, 490, 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("Down"); moveDownButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); repaint(); }); JButton moveUpButton = new JButton("Up"); moveUpButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); repaint(); }); JButton moveLeftButton = new JButton("Left"); moveLeftButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); repaint(); }); JButton moveRightButton = new JButton("Right"); 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); super.repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,490,null); super.repaint(); } }