refactorings I

This commit is contained in:
Jan-Niclas Loosen 2024-11-27 16:50:18 +01:00
parent e31f2e3544
commit 50937ab7fd
10 changed files with 215 additions and 154 deletions

View File

@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -1,53 +0,0 @@
package com.example.flappybird; /**
* Animation.java
* Creates animation with array of sprites
*
* @author Paul Krishnamurthy
*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
public class Animation {
// Start at first frame
private static double currentFrame = 0;
/**
* Creates an animation with an array of sprites
*
* @param sprites Array of BufferedImages
* @param x x-coordinate
* @param y y-coordinate
* @param speed Speed of animation
* @param angle Angle to rotate sprite
*/
public static void animate (Graphics g, BufferedImage[] sprites, int x, int y, double speed, double angle) {
Graphics2D g2d = (Graphics2D) g;
AffineTransform trans = g2d.getTransform();
AffineTransform at = new AffineTransform();
// Number of frames
int count = sprites.length;
// Rotate the image
at.rotate(angle, x + 25, y + 25);
g2d.transform(at);
// Draw the current rotated frame
g2d.drawImage(sprites[(int) (Math.round(currentFrame))], x, y, null);
g2d.setTransform(trans);
// Switch animation frames
if (currentFrame >= count - 1) {
currentFrame = 0;
} else currentFrame += speed;
}
}

View File

@ -8,12 +8,9 @@ package com.example.flappybird; /**
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.net.URL;
public class Audio {
private AudioInputStream audioInputStream;
private Clip clip;
private void playSound (String sound) {
// Path to sound file
@ -21,8 +18,11 @@ public class Audio {
// Try to load and play sound
try {
audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(soundURL));
clip = AudioSystem.getClip();
URL audioUrl = this.getClass().getResource(soundURL);
assert audioUrl != null;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioUrl);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception e) {

View File

@ -5,15 +5,15 @@ package com.example.flappybird; /**
* @author Paul Krishnamurthy
*/
import javax.swing.JPanel;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
public class Bird extends JPanel {
public class Bird extends HasPosition {
// Bird attributes
public String color;
private int x, y;
private boolean isAlive = true;
// Bird constants
@ -34,11 +34,12 @@ public class Bird extends JPanel {
// Bird sprites
private BufferedImage[] sprites;
private static double currentFrame = 0;
public Bird (String color, int x, int y, BufferedImage[] s) {
super(x, y);
this.color = color;
this.x = x;
this.y = y;
this.sprites = s;
}
@ -144,7 +145,7 @@ public class Bird extends JPanel {
rotation /= 2;
// Handle rotation offset
rotation = rotation > Math.PI / 2 ? Math.PI / 2 : rotation;
rotation = Math.min(rotation, Math.PI / 2);
if (!isAlive()) {
@ -157,8 +158,32 @@ public class Bird extends JPanel {
}
// Create bird animation and pass in rotation angle
Animation.animate(g, sprites, x, y, .09, rotation);
animateBird(g, rotation);
}
private void animateBird(Graphics g, double angle) {
Graphics2D g2d = (Graphics2D) g;
AffineTransform trans = g2d.getTransform();
AffineTransform at = new AffineTransform();
// Number of frames
int count = sprites.length;
// Rotate the image
at.rotate(angle, x + 25, y + 25);
g2d.transform(at);
// Draw the current rotated frame
g2d.drawImage(sprites[(int) (Math.round(currentFrame))], x, y, null);
g2d.setTransform(trans);
// Switch animation frames
if (currentFrame >= count - 1) {
currentFrame = 0;
} else currentFrame += 0.09;
}
}

View File

@ -8,6 +8,7 @@ package com.example.flappybird; /**
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URI;
import java.util.Random;
import java.util.ArrayList;
import java.util.HashMap;
@ -501,6 +502,18 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
}
public void openURL (String url) {
try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(new URI(url));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Sorry could not open URL...");
}
}
//////////////////////
// Keyboard actions //
@ -569,7 +582,7 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
if (gameBird.isAlive()) {
if (isTouching(textures.get("rateButton").getRect())) {
Helper.openURL("http://paulkr.com"); // Open website
openURL("http://paulkr.com"); // Open website
}
}

View File

@ -0,0 +1,19 @@
package com.example.flappybird;
public class HasPosition {
protected int x;
protected int y;
public HasPosition(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}

View File

@ -1,31 +0,0 @@
package com.example.flappybird; /**
* Helper.java
* Helper class with various tools
*
* @author Paul Krishnamurthy
*/
import java.awt.Desktop;
import java.net.URI;
public class Helper {
/**
* Tries to open url in default web browser
*
* @param url Destination URL
*/
public static void openURL (String url) {
try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(new URI(url));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Sorry could not open URL...");
}
}
}

View File

@ -7,12 +7,7 @@ package com.example.flappybird;
* @author Paul Krishnamurthy
*/
public class Pipe {
// Pipe coordinates
private int x = FlappyBird.WIDTH + 5;
private int y;
public class Pipe extends HasPosition {
// Placement (top or bottom) of pipe
String location;
@ -27,17 +22,18 @@ public class Pipe {
public boolean canAwardPoint = true;
public Pipe (String location) {
super(FlappyBird.WIDTH + 5, 0);
this.location = location;
reset();
}
public void reset () {
x = FlappyBird.WIDTH + 5; // Reset x-coordinate
super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate
// Set boundaries for top pipes
// This y-coordinte + PIPE_SPACING will be for the bottom pipe
if (location.equals("top")) {
y = - Math.max((int) (Math.random() * 320) + 30, 140);
super.y = - Math.max((int) (Math.random() * 320) + 30, 140);
}
}
@ -45,7 +41,7 @@ public class Pipe {
* Moves the pipe
*/
public void move () {
x += SPEED;
super.x += SPEED;
}
@ -61,29 +57,14 @@ public class Pipe {
public boolean collide (int nX, int nY, int nW, int nH) {
// Do not allow bird to jump over pipe
if (nX > x && nY < 0 && canAwardPoint) {
if (nX > super.x && nY < 0 && canAwardPoint) {
return true;
}
return nX < x + WIDTH &&
nX + nW > x &&
nY < y + HEIGHT &&
nY + nH > y;
}
/**
* @return Pipe's x-coordinate
*/
public int getX () {
return x;
}
/**
* @return Pipe's y-coordinate
*/
public int getY () {
return y;
return nX < super.x + WIDTH &&
nX + nW > super.x &&
nY < super.y + HEIGHT &&
nY + nH > super.y;
}
/**
@ -94,6 +75,5 @@ public class Pipe {
public void setY (int newY) {
y = newY;
}
}

View File

@ -8,20 +8,20 @@ package com.example.flappybird; /**
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
public class Texture {
public class Texture extends HasPosition {
// Image attributes
private BufferedImage image;
private int x, y, width, height;
private Rectangle rect;
private final BufferedImage image;
private final Rectangle rect;
public Texture (BufferedImage image, int x, int y) {
super(x, y);
this.image = image;
this.x = x;
this.y = y;
this.width = image.getWidth();
this.height = image.getHeight();
int width = image.getWidth();
int height = image.getHeight();
this.rect = new Rectangle(x, y, width, height);
}
@ -33,22 +33,6 @@ public class Texture {
return image;
}
public int getX () {
return x;
}
public int getY () {
return y;
}
public int getWidth () {
return width;
}
public int getHeight () {
return height;
}
public Rectangle getRect () {
return rect;
}