Remove some try statements and switches
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| package dev.lh; | ||||
|  | ||||
| import static java.awt.Color.*; | ||||
|  | ||||
| import java.awt.*; | ||||
| import java.util.Random; | ||||
|  | ||||
| @@ -27,30 +29,56 @@ public class FoodFactory { | ||||
| 	 * @since Snake 1.0 | ||||
| 	 */ | ||||
| 	public static enum Food { | ||||
|  | ||||
| 		/** | ||||
| 		 * Use if white food is wanted. | ||||
| 		 */ | ||||
| 		white, | ||||
| 		white( | ||||
| 			WHITE, 40 | ||||
| 		), | ||||
|  | ||||
| 		/** | ||||
| 		 * Use if yellow food is wanted. | ||||
| 		 */ | ||||
| 		yellow, | ||||
| 		yellow( | ||||
| 			YELLOW, 15 | ||||
| 		), | ||||
|  | ||||
| 		/** | ||||
| 		 * Use if orange food is wanted. | ||||
| 		 */ | ||||
| 		orange, | ||||
| 		orange( | ||||
| 			ORANGE, 6 | ||||
| 		), | ||||
|  | ||||
| 		/** | ||||
| 		 * Use if red food is wanted. | ||||
| 		 */ | ||||
| 		red, | ||||
| 		red( | ||||
| 			RED, 2 | ||||
| 		), | ||||
|  | ||||
| 		/** | ||||
| 		 * Use if blue food is wanted. | ||||
| 		 */ | ||||
| 		blue | ||||
| 		blue( | ||||
| 			BLUE, 1 | ||||
| 		); | ||||
|  | ||||
| 		/** | ||||
| 		 * The color of the food item. | ||||
| 		 */ | ||||
| 		public final Color color; | ||||
|  | ||||
| 		/** | ||||
| 		 * The length bonus of the food item. | ||||
| 		 */ | ||||
| 		public final int lengthBonus; | ||||
|  | ||||
| 		private Food(Color color, int lengthBonus) { | ||||
| 			this.color = color; | ||||
| 			this.lengthBonus = lengthBonus; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	private static FoodFactory foodFactory = new FoodFactory(); | ||||
| @@ -142,23 +170,7 @@ public class FoodFactory { | ||||
| 	 * @since Snake 1.0 | ||||
| 	 */ | ||||
| 	public void colorOfFood(Graphics g) { | ||||
| 		switch (nextFood) { | ||||
| 			case white: | ||||
| 				g.setColor(Color.white); | ||||
| 				break; | ||||
| 			case yellow: | ||||
| 				g.setColor(Color.yellow); | ||||
| 				break; | ||||
| 			case orange: | ||||
| 				g.setColor(Color.orange); | ||||
| 				break; | ||||
| 			case red: | ||||
| 				g.setColor(Color.red); | ||||
| 				break; | ||||
| 			case blue: | ||||
| 				g.setColor(Color.blue); | ||||
| 				break; | ||||
| 		} | ||||
| 		g.setColor(nextFood.color); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -172,12 +184,12 @@ public class FoodFactory { | ||||
|  | ||||
| 	/** | ||||
| 	 * @param snakeHead the the head of a {@link Snake} object | ||||
| 	 * @return true if the current food intersects with the snakehead | ||||
| 	 * @return true if the current food intersects with the snake head | ||||
| 	 * @since Snake 1.0 | ||||
| 	 */ | ||||
| 	public boolean checkCollision(Rectangle snakeHead) { | ||||
| 		int			s		= rectangleSize * 5; | ||||
| 		Rectangle	food	= new Rectangle(pFood, new Dimension(s, s)); | ||||
| 		int s = rectangleSize * 5; | ||||
| 		Rectangle food = new Rectangle(pFood, new Dimension(s, s)); | ||||
| 		return food.intersects(snakeHead); | ||||
| 	} | ||||
|  | ||||
| @@ -186,24 +198,6 @@ public class FoodFactory { | ||||
| 	 * @since Snake 1.0 | ||||
| 	 */ | ||||
| 	public int getAdditionalLength() { | ||||
| 		int snakeAdditionalLength = 0; | ||||
| 		switch (nextFood) { | ||||
| 			case white: | ||||
| 				snakeAdditionalLength = 40; | ||||
| 				break; | ||||
| 			case yellow: | ||||
| 				snakeAdditionalLength = 15; | ||||
| 				break; | ||||
| 			case orange: | ||||
| 				snakeAdditionalLength = 6; | ||||
| 				break; | ||||
| 			case red: | ||||
| 				snakeAdditionalLength = 2; | ||||
| 				break; | ||||
| 			case blue: | ||||
| 				snakeAdditionalLength = 1; | ||||
| 				break; | ||||
| 		} | ||||
| 		return snakeAdditionalLength; | ||||
| 		return nextFood.lengthBonus; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -3,8 +3,8 @@ package dev.lh; | ||||
| import java.awt.Graphics; | ||||
|  | ||||
| /** | ||||
|  * This interface contains everything that needs to updated regularly.<br> | ||||
|  * <br> | ||||
|  * This interface contains everything that needs to be updated regularly. | ||||
|  * <p> | ||||
|  * Project: <strong>Snake</strong><br> | ||||
|  * File: <strong>Updateable.java</strong><br> | ||||
|  * Created: <strong>11 Mar 2020</strong><br> | ||||
|   | ||||
| @@ -20,9 +20,9 @@ public class Endscreen extends JDialog { | ||||
|  | ||||
| 	private static final long serialVersionUID = -4457484397259161063L; | ||||
|  | ||||
| 	private static final int	goodOrBadResult	= 200; | ||||
| 	private final JPanel		contentPanel	= new JPanel(); | ||||
| 	private final int			score; | ||||
| 	private static final int goodOrBadResult = 200; | ||||
| 	private final JPanel contentPanel = new JPanel(); | ||||
| 	private final int score; | ||||
|  | ||||
| 	/** | ||||
| 	 * Create the dialog. | ||||
| @@ -31,32 +31,33 @@ public class Endscreen extends JDialog { | ||||
| 	 */ | ||||
| 	public Endscreen(int score) { | ||||
| 		this.score = score; | ||||
| 		try { | ||||
| 		setTitle("Endscreen"); | ||||
| 		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||||
| 		setBounds(100, 100, 700, 700); | ||||
| 		getContentPane().setLayout(new BorderLayout()); | ||||
| 		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); | ||||
| 		contentPanel.setLayout(new BorderLayout(0, 0)); | ||||
| 		getContentPane().add(contentPanel, BorderLayout.CENTER); | ||||
|  | ||||
| 			setTitle("Endscreen"); | ||||
| 			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||||
| 			setBounds(100, 100, 700, 700); | ||||
| 			getContentPane().setLayout(new BorderLayout()); | ||||
| 			contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); | ||||
| 			contentPanel.setLayout(new BorderLayout(0, 0)); | ||||
| 			getContentPane().add(contentPanel, BorderLayout.CENTER); | ||||
| 		JButton btnNewButton = new JButton("Play again"); | ||||
| 		btnNewButton.setMnemonic(KeyEvent.VK_ENTER); | ||||
| 		btnNewButton.addActionListener(e -> { | ||||
| 			Main.startGame(); | ||||
| 			dispose(); | ||||
| 		}); | ||||
| 		btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15)); | ||||
| 		contentPanel.add(btnNewButton, BorderLayout.SOUTH); | ||||
|  | ||||
| 			JButton btnNewButton = new JButton("Play again"); | ||||
| 			btnNewButton.setMnemonic(KeyEvent.VK_ENTER); | ||||
| 			btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); }); | ||||
| 			btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15)); | ||||
| 			contentPanel.add(btnNewButton, BorderLayout.SOUTH); | ||||
| 		JLabel lblDeinPunktestand = new JLabel("Dein Punktestand:	     " + String.valueOf(score)); | ||||
| 		lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25)); | ||||
| 		contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH); | ||||
|  | ||||
| 			JLabel lblDeinPunktestand = new JLabel("Dein Punktestand:	     " + String.valueOf(score)); | ||||
| 			lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25)); | ||||
| 			contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH); | ||||
|  | ||||
| 			Image resultImage = Toolkit.getDefaultToolkit() | ||||
| 				.getImage(this.getClass().getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png")); | ||||
| 			resultImage.flush(); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 		Image resultImage = Toolkit.getDefaultToolkit() | ||||
| 			.getImage( | ||||
| 				this.getClass() | ||||
| 					.getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png") | ||||
| 			); | ||||
| 		resultImage.flush(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
|   | ||||
| @@ -21,13 +21,14 @@ import dev.lh.Main; | ||||
|  */ | ||||
| public class StartScreen extends JFrame { | ||||
|  | ||||
| 	private static final long	serialVersionUID	= 6055940532003735543L; | ||||
| 	private JPanel				contentPane; | ||||
| 	private static final long serialVersionUID = 6055940532003735543L; | ||||
|  | ||||
| 	/** | ||||
| 	 * Closes the application. | ||||
| 	 */ | ||||
| 	public static void close() { System.exit(0); } | ||||
| 	public static void close() { | ||||
| 		System.exit(0); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Launches Snake. | ||||
| @@ -43,31 +44,27 @@ public class StartScreen extends JFrame { | ||||
| 	 * Create the frame. | ||||
| 	 */ | ||||
| 	public StartScreen() { | ||||
| 		try { | ||||
| 			// readInHighscores(); | ||||
| 			setTitle("Snake - Startscreen"); | ||||
| 			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||||
| 			setBounds(500, 200, 550, 550); | ||||
| 			contentPane = new JPanel(); | ||||
| 			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | ||||
| 			setContentPane(contentPane); | ||||
| 		setTitle("Snake - Startscreen"); | ||||
| 		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||||
| 		setBounds(500, 200, 550, 550); | ||||
|  | ||||
| 			JButton buPlay = new JButton("Start Game"); | ||||
| 			buPlay.setBounds(158, 197, 190, 131); | ||||
| 			buPlay.setText("Play Again"); | ||||
| 			buPlay.setMnemonic(KeyEvent.VK_ENTER); | ||||
| 			buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16)); | ||||
| 			buPlay.addActionListener(a -> { | ||||
| 				Main.startGame(); | ||||
| 				setVisible(false); | ||||
| 				dispose(); | ||||
| 				System.gc(); | ||||
| 			}); | ||||
| 			contentPane.add(buPlay); | ||||
| 			contentPane.setLayout(null); | ||||
| 			setVisible(true); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 		JPanel contentPane = new JPanel(); | ||||
| 		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | ||||
| 		setContentPane(contentPane); | ||||
|  | ||||
| 		JButton buPlay = new JButton("Start Game"); | ||||
| 		buPlay.setBounds(158, 197, 190, 131); | ||||
| 		buPlay.setText("Play Again"); | ||||
| 		buPlay.setMnemonic(KeyEvent.VK_ENTER); | ||||
| 		buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16)); | ||||
| 		buPlay.addActionListener(a -> { | ||||
| 			Main.startGame(); | ||||
| 			setVisible(false); | ||||
| 			dispose(); | ||||
| 			System.gc(); | ||||
| 		}); | ||||
| 		contentPane.add(buPlay); | ||||
| 		contentPane.setLayout(null); | ||||
| 		setVisible(true); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user