Guest User

Untitled

a guest
Apr 25th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.76 KB | None | 0 0
  1. import javafx.scene.layout.BackgroundImage;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. public class RaceGui {
  11.  
  12. // private final JLabel background;
  13. Race race;
  14.  
  15. JFrame window, raceWindow;
  16. Container container, raceContainer;
  17. JPanel startPanel, startButtonPanel, customiseRacePanel, preview, nextBackPanel;
  18. JLabel startLabel, distanceLabel, lapsLabel, horsesLabel, trackSelectorLabel;
  19. Font startFont = new Font("American Typewriter", Font.BOLD, 60);
  20. Font normalFont = new Font("Arial", Font.BOLD, 20);
  21. Font isInfo = new Font("Arial", Font.BOLD, 20);
  22. Font showSymbol = new Font("", Font.PLAIN, 200);
  23. JButton startButton, customiseHorsesButton;
  24. JSlider distanceSlider, lapsSlider, horsesSlider;
  25. JPanel[] horsePanels;
  26. JButton[] horseButtons;
  27. JComboBox[] symbolSelectors;
  28. JTextField[] nameFields;
  29. JSlider[] confidenceSliders;
  30. private Map<String, ImageIcon> trackImages; // Store track images
  31. private JLayeredPane layeredPane;
  32. private JComboBox<String> trackComboBox;
  33. TitleScreenHandler tScreenHandler = new TitleScreenHandler();
  34. RaceCustomiseHandler rcHandler = new RaceCustomiseHandler();
  35. HorseCustomiseHandler horseHandler = new HorseCustomiseHandler();
  36. JPanel horseCustomizationPanel;
  37. BackgroundPanel mainBackground;
  38.  
  39. JPanel storeSymbol;
  40. JLabel holdSymbol;
  41. JPanel storeInfo;
  42. JLabel holdName;
  43. JLabel holdConfidence;
  44.  
  45. char displaySymbol;
  46. int displayConfidence;
  47. String displayName;
  48.  
  49. Color transparentGreen = new Color(0, 255, 0, 4);
  50. int numHorses = 3;
  51. int laps;
  52. int distance;
  53. private String[] trackTypes = {"Grass", "Dirt", "Underwater", "Space", "Backrooms"};
  54.  
  55. ImageIcon imageIcon;
  56. Image background, raceBackground;
  57.  
  58.  
  59. private char[] symbolChar = {
  60. '♘', 'ଲ', '♞', '♛', '♚', '♕', '♔', '❄', '❁', '➵',
  61. '✈', '☀', '☁', '⚽', '⚡', '⭐', '☕', '☂', '✿', '✤',
  62. '⓪', '①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨',
  63. '⓿', '❶', '❷', '❸', '❹', '❺', '❻', '❼', '❽', '❾',
  64. '♠', '♥', '♦', '♣',
  65. '⌘', '⏎', '⌫', '♿', '⚜', '⚛'
  66. };
  67. private JPanel trackSelectorPanel;
  68. private JPanel horsePreview;
  69. private JPanel buttonPanel;
  70. private int laneNumber;
  71. private JPanel watchRace;
  72. private ImageIcon backgroundImage;
  73.  
  74.  
  75. public static void main(String[] args) {
  76. new RaceGui();
  77. }
  78.  
  79. public RaceGui() {
  80. backgroundImage = new ImageIcon("Images/Background.jpg");
  81. trackImages = new HashMap<>();
  82. loadTrackImages();
  83. background = backgroundImage.getImage();
  84.  
  85. window = new JFrame();
  86. window.setSize(800, 600);
  87. window.setLayout(null);
  88. window.getContentPane().setBackground(Color.black);
  89. window.setResizable(false);
  90. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91. window.setVisible(true);
  92. container = window.getContentPane();
  93.  
  94. ImageIcon backgroundImageIcon = new ImageIcon("Images/Background.jpg"); // Ensure correct path
  95. Image backgroundImage = backgroundImageIcon.getImage();
  96.  
  97. // Create the background panel with the loaded image
  98. BackgroundPanel backgroundPanel = new BackgroundPanel(backgroundImage, BackgroundPanel.SCALED);
  99. backgroundPanel.setLayout(new BorderLayout());
  100.  
  101. window.setContentPane(backgroundPanel);
  102. window.setVisible(true);
  103.  
  104.  
  105. startPanel = new JPanel();
  106. startPanel.setBounds(100, 100, 600, 150);
  107. startPanel.setBackground(transparentGreen);
  108.  
  109. startLabel = new JLabel("Off To The Races");
  110. startLabel.setFont(startFont);
  111. startLabel.setForeground(Color.white);
  112.  
  113. startButtonPanel = new JPanel();
  114. startButtonPanel.setBounds(300, 400, 200, 100);
  115. startButtonPanel.setBackground(transparentGreen);
  116.  
  117. startButton = new JButton("Start Customising");
  118. startButton.setForeground(Color.white);
  119. startButton.setFont(normalFont);
  120. startButton.addActionListener(tScreenHandler);
  121.  
  122. startPanel.add(startLabel);
  123. startButtonPanel.add(startButton);
  124.  
  125.  
  126. // layeredPane.add(startPanel, Integer.valueOf(1));
  127. // layeredPane.add(startButtonPanel, Integer.valueOf(1));
  128. //
  129. // // Set the layered pane as the content pane
  130. // window.setContentPane(layeredPane);
  131.  
  132. container.add(startPanel);
  133. container.add(startButtonPanel);
  134. }
  135.  
  136. public void customiseRace() {
  137. startPanel.setVisible(false);
  138. startButtonPanel.setVisible(false);
  139.  
  140. customiseRacePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  141. customiseRacePanel.setLayout(new GridLayout(4, 1));
  142. trackSelectorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  143. customiseRacePanel.setBounds(0, 450, 550, 100); // Adjusted bounds
  144. trackSelectorPanel.setBounds(550, 450, 250, 100); // Adjusted bounds
  145. customiseRacePanel.setBackground(Color.blue);
  146. trackSelectorPanel.setBackground(Color.red);
  147. container.add(customiseRacePanel);
  148. container.add(trackSelectorPanel);
  149.  
  150. customiseRacePanel.setVisible(true);
  151. trackSelectorPanel.setVisible(true);
  152.  
  153.  
  154. // Creating and configuring sliders
  155. distanceSlider = new JSlider(1, 20, 10);
  156. lapsSlider = new JSlider(1, 5, 1);
  157. horsesSlider = new JSlider(2, 6, 3);
  158. distanceSlider.setMajorTickSpacing(2);
  159. lapsSlider.setMajorTickSpacing(1);
  160. horsesSlider.setMajorTickSpacing(1);
  161.  
  162. distanceLabel = new JLabel("Distance: 10");
  163. distanceLabel.setFont(normalFont);
  164. lapsLabel = new JLabel("Laps: 1");
  165. lapsLabel.setFont(normalFont);
  166. horsesLabel = new JLabel("Horses: 3");
  167. horsesLabel.setFont(normalFont);
  168.  
  169. // Update labels dynamically
  170. distanceSlider.addChangeListener(e -> {
  171. distance = distanceSlider.getValue();
  172. distanceLabel .setText("Distance: " + distance);
  173.  
  174. });
  175.  
  176. lapsSlider.addChangeListener(e -> {
  177. laps = lapsSlider.getValue();
  178. lapsLabel.setText("Laps: " + laps);
  179.  
  180. });
  181.  
  182. horsesSlider.addChangeListener(e -> {
  183. numHorses = horsesSlider.getValue();
  184. horsesLabel.setText("Horses: " + numHorses);
  185.  
  186. });
  187. System.out.println(numHorses);
  188.  
  189. trackComboBox = new JComboBox<>(trackTypes);
  190. trackComboBox.addActionListener(new ColorChangeHandler());
  191. trackComboBox.setPreferredSize(new Dimension(150, 30));
  192. trackSelectorLabel = new JLabel("Select Track");
  193. customiseRacePanel.add(trackComboBox); // Add combo box to the panel
  194.  
  195.  
  196. // Adding components to the panel
  197. customiseRacePanel.add(distanceLabel);
  198. customiseRacePanel.add(distanceSlider);
  199. customiseRacePanel.add(lapsLabel);
  200. customiseRacePanel.add(lapsSlider);
  201. customiseRacePanel.add(horsesLabel);
  202. customiseRacePanel.add(horsesSlider);
  203. trackSelectorPanel.add(trackSelectorLabel);
  204. trackSelectorPanel.add(trackComboBox);
  205.  
  206. // Button to proceed to horse customisation
  207. customiseHorsesButton = new JButton("Customise Horses");
  208. customiseHorsesButton.setPreferredSize(new Dimension(150, 30));
  209. customiseHorsesButton.addActionListener(rcHandler);
  210. trackSelectorPanel.add(customiseHorsesButton);
  211.  
  212. preview = new JPanel();
  213. preview.setBackground(transparentGreen);
  214. preview.setBounds(100, 100, 600, 300); // Set bounds to cover the top part of the window
  215. container.add(preview);
  216. preview.setVisible(true);
  217. }
  218.  
  219. public void customiseHorses() {
  220. Character[] symbols = new Character[symbolChar.length];
  221. for (int i = 0; i < symbolChar.length; i++) {
  222. symbols[i] = symbolChar[i]; // Auto-boxing char to Character
  223. }
  224.  
  225. storeSymbol = new JPanel();
  226. storeInfo = new JPanel();
  227.  
  228. holdSymbol = new JLabel(Character.toString(displaySymbol));
  229. holdName = new JLabel("Name: " + displayName );
  230. holdConfidence = new JLabel("Confidence: " + displayConfidence + "%");
  231.  
  232. horseCustomizationPanel = new JPanel();
  233. horseCustomizationPanel.setLayout(new GridLayout(0, 1));
  234. horseCustomizationPanel.setBounds(450, 0, 400, 500);
  235. horseCustomizationPanel.setBackground(Color.lightGray);
  236. container.add(horseCustomizationPanel);
  237.  
  238. horsePanels = new JPanel[numHorses];
  239. horseButtons = new JButton[numHorses];
  240. symbolSelectors = new JComboBox[numHorses];
  241. nameFields = new JTextField[numHorses];
  242. confidenceSliders = new JSlider[numHorses];
  243. nextBackPanel = new JPanel();
  244. nextBackPanel.setLayout(new GridLayout(0, 1));
  245.  
  246. nextBackPanel.setBounds(500, 510, 300, 50);
  247. nextBackPanel.setBackground(Color.black);
  248. nextBackPanel.setVisible(true);
  249. container.add(nextBackPanel);
  250.  
  251. String selectedTrack;
  252.  
  253.  
  254. for (int i = 0; i < numHorses; i++) {
  255. // Panel for each horse
  256. final int index = i;
  257. horsePanels[i] = new JPanel();
  258. horsePanels[i].setLayout(new FlowLayout());
  259. horsePanels[i].setBackground(Color.decode("#F0F0F0"));
  260.  
  261. // Button to select horse
  262. horseButtons[i] = new JButton("Change Horse " + (i + 1));
  263. horseButtons[index].addActionListener(e -> {
  264. displayHorseSettings(e, index); // Pass the button index
  265. });
  266. horsePanels[i].add(horseButtons[i]);
  267.  
  268. // Symbol selector
  269. symbolSelectors[i] = new JComboBox<>(symbols);
  270. symbolSelectors[i].setSelectedItem('♘'); // Default selection
  271. symbolSelectors[i].addActionListener(e -> {
  272. displaySymbol = (char) symbolSelectors[index].getSelectedItem(); // Use the captured index
  273.  
  274. //System.out.println(displaySymbol);
  275. });
  276. horsePanels[i].add(symbolSelectors[i]);
  277.  
  278. // Name field
  279. nameFields[i] = new JTextField(10);
  280. nameFields[i].addActionListener(e -> {
  281. displayName = nameFields[index].getText();; // Use the captured index
  282. displaySymbol = (char) symbolSelectors[index].getSelectedItem();
  283. //System.out.println(displayName);
  284. });
  285. horsePanels[i].add(nameFields[i]);
  286.  
  287.  
  288. // Confidence slider
  289. confidenceSliders[i] = new JSlider(1, 10, 5);
  290. confidenceSliders[i].setMajorTickSpacing(1);
  291. confidenceSliders[i].setPaintTicks(true);
  292. confidenceSliders[i].setPaintLabels(true);
  293. confidenceSliders[i].addChangeListener(e -> {
  294. // Use the captured index
  295.  
  296.  
  297. //System.out.println(displayConfidence);
  298. });
  299. //
  300.  
  301.  
  302. horsePanels[i].add(confidenceSliders[i]);
  303. // Add panel to horse customization panel
  304. horseCustomizationPanel.add(horsePanels[i]);
  305. }
  306.  
  307. JButton startRaceButton = new JButton("Start Race");
  308. startRaceButton.setBackground(Color.green);
  309. startRaceButton.setForeground(Color.white);
  310. startRaceButton.setFont(normalFont);
  311. startRaceButton.addActionListener(horseHandler);
  312. nextBackPanel.add(startRaceButton);
  313.  
  314.  
  315. JButton goBackButton = new JButton("Back to Race Config");
  316. goBackButton.setBackground(Color.green);
  317. goBackButton.setBackground(Color.white);
  318. goBackButton.setFont(normalFont);
  319. goBackButton.addActionListener(tScreenHandler);
  320. nextBackPanel.add(goBackButton);
  321.  
  322.  
  323.  
  324. container.add(storeSymbol);
  325. container.add(storeInfo);
  326. container.add(horseCustomizationPanel); // Re-add horse customization panel
  327. container.add(nextBackPanel); // Re-add next/back panel
  328.  
  329. // Set visibility to true to ensure components are visible
  330. storeInfo.setVisible(true);
  331. storeSymbol.setVisible(true);
  332. horseCustomizationPanel.setVisible(true);
  333. nextBackPanel.setVisible(true);
  334. for (int i = 0; i < numHorses; i++) {
  335. horsePanels[i].setVisible(true);
  336. }
  337.  
  338. // Show settings panel for first horse initially
  339. displayHorseSettings(new ActionEvent(horseButtons[0], ActionEvent.ACTION_PERFORMED, null), 0);
  340. }
  341.  
  342. private void displayHorseSettings(ActionEvent e, int index) {
  343. // System.out.println("♘");
  344. for (int i = 0; i < numHorses; i++) {
  345. if (e.getSource() == horseButtons[i]) {
  346.  
  347. displaySymbol = (char) symbolSelectors[index].getSelectedItem();
  348. displayConfidence = confidenceSliders[index].getValue();
  349. displayName = nameFields[index].getText();
  350.  
  351. holdSymbol.setText(Character.toString(displaySymbol));
  352. holdSymbol.setFont(showSymbol);
  353. holdName.setText("Name: " + displayName );
  354. holdConfidence.setText("Confidence: " + displayConfidence * 10 + "%");
  355. holdName.setFont(isInfo);
  356. holdConfidence.setFont(isInfo);
  357. //horsePreview.setBounds(50, 100, 350, 350);
  358.  
  359. storeSymbol.setBounds(50, 100, 350, 250);
  360. storeInfo.setBounds(50, 350, 350, 100);
  361.  
  362. storeSymbol.setBackground(Color.orange);
  363. storeInfo.setBackground(Color.red);
  364. storeInfo.setLayout(new GridLayout(0, 1));
  365.  
  366. storeInfo.add(holdName);
  367. storeInfo.add(holdConfidence);
  368. storeSymbol.add(holdSymbol);
  369. }
  370. }
  371. }
  372.  
  373. private void loadTrackImages() {
  374. // Load images for each track type
  375. trackImages.put("Grass", new ImageIcon("Images/Grass.jpg"));
  376. trackImages.put("Dirt", new ImageIcon("Images/Dirt.jpg"));
  377. trackImages.put("Underwater", new ImageIcon("Images/Underwater.jpg"));
  378. trackImages.put("Space", new ImageIcon("Images/Space.jpg"));
  379. trackImages.put("Backrooms", new ImageIcon("Images/Backrooms.jpg"));
  380. trackImages.put("Background", new ImageIcon("Images/Background.jpg"));
  381. }
  382.  
  383. private void updatePreview(String trackType) {
  384. imageIcon = trackImages.get(trackType);
  385.  
  386. if (imageIcon != null) {
  387. // Scale the image to fit the panel while maintaining aspect ratio
  388. Image scaledImage = imageIcon.getImage().getScaledInstance(preview.getWidth(), preview.getHeight(), Image.SCALE_SMOOTH);
  389.  
  390. preview.removeAll(); // Clear existing content
  391. preview.add(new JLabel(new ImageIcon(scaledImage))); // Add scaled image to preview
  392. preview.revalidate(); // Refresh the panel
  393. preview.repaint(); // Redraw the panel
  394. }
  395. }
  396.  
  397.  
  398. public void beginRace() {
  399. customiseRacePanel.setVisible(false);
  400. nextBackPanel.setVisible(false);
  401. container.setVisible(false);
  402.  
  403. raceWindow = new JFrame();
  404. raceWindow.setSize(800, 600);
  405. raceWindow.setLayout(null);
  406. raceWindow.getContentPane().setBackground(Color.black);
  407. raceWindow.setResizable(false);
  408. raceWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  409. raceWindow.setVisible(true);
  410.  
  411. raceContainer = raceWindow.getContentPane();
  412.  
  413. // Create a panel to display the race
  414. watchRace = new BackgroundPanel(background, BackgroundPanel.SCALED){
  415. @Override
  416. protected void paintComponent(Graphics g) {
  417. // Call the superclass to ensure other Swing components are painted
  418. super.paintComponent(g);
  419. // Draw the background image first
  420. if (watchRace.getBackground() != null) {
  421. System.out.println(backgroundImage);
  422.  
  423. g.drawImage(
  424. backgroundImage.getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH),
  425. 0,
  426. 0,
  427. this
  428. );
  429. }
  430.  
  431. // Now draw other content, such as the race
  432. if (race != null) {
  433. race.paintRace(g); // Draw the race content on top
  434. }
  435. }
  436.  
  437.  
  438. };
  439.  
  440. watchRace.setBounds(50, 50, 700, 380); // Set the race panel size
  441. raceContainer.add(watchRace);
  442.  
  443. if(imageIcon == null){
  444. //System.out.println("No image selected, proceeding with basics");
  445. imageIcon = trackImages.get("Grass");
  446. }
  447.  
  448. race = new Race(distance, laps, watchRace, imageIcon); // Initialize the race with the panel
  449. for (int i = 0; i < numHorses; i++) {
  450. Character symbol = (Character) symbolSelectors[i].getSelectedItem(); // Get the horse symbol
  451. double confidence = (double) confidenceSliders[i].getValue() / 10;
  452. String name = nameFields[i].getText(); // Get the horse name
  453.  
  454. Horse horse = new Horse(symbol, name, confidence); // Create the horse
  455. race.addHorse(horse); // Add to the race
  456. }
  457.  
  458. //System.out.println(imageIcon);
  459. race.startRace(); // Start the race
  460. }
  461.  
  462.  
  463. public class TitleScreenHandler implements ActionListener {
  464. public void actionPerformed(ActionEvent e) {
  465. if(horseCustomizationPanel != null){
  466. storeInfo.setVisible(false);
  467. storeSymbol.setVisible(false);
  468. horseCustomizationPanel.setVisible(false);
  469. nextBackPanel.setVisible(false);}
  470. customiseRace();
  471. }
  472. }
  473.  
  474. public class RaceCustomiseHandler implements ActionListener {
  475. public void actionPerformed(ActionEvent e) {
  476. preview.setVisible(false);
  477. customiseRacePanel.setVisible(false);
  478. trackSelectorPanel.setVisible(false);
  479. customiseHorses();
  480. }
  481. }
  482.  
  483. public class HorseCustomiseHandler implements ActionListener {
  484. public void actionPerformed(ActionEvent e) {
  485. beginRace();
  486. }
  487. }
  488.  
  489. private class ColorChangeHandler implements ActionListener {
  490. @Override
  491. public void actionPerformed(ActionEvent e) {
  492. String selectedTrack = (String) trackComboBox.getSelectedItem();
  493. updatePreview(selectedTrack); // Update the preview with the new image
  494. }
  495. }
  496.  
  497. }
  498.  
Advertisement
Add Comment
Please, Sign In to add comment