Skip to content

Commit

Permalink
Rename Project to JWeather.
Browse files Browse the repository at this point in the history
+ Added togglable gravity.
+ Added particleList failsafe for >2500 particles.
+ Rain delay now shown in option button.
# Changed rendering for quality rather than speed to reduce stutter.
# Changed text colouring to Color.GRAY rather than black.
# Changed colours of the day mode to allow for particles to be seen
better.
# Now constantly updating gravity coefficient in particle to allow for
particle changes during particle processing.
  • Loading branch information
jacojazz committed Jan 19, 2016
1 parent b739210 commit d1c7950
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ProjectileTest/.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ProjectileTest</name>
<name>JWeather</name>
<comment></comment>
<projects>
</projects>
Expand Down
22 changes: 18 additions & 4 deletions ProjectileTest/src/gui/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,33 @@ public class Game extends JPanel {
static boolean MANUAL = false;
static boolean NIGHT = true;
static int rainX = 0, rainY = 10;
static int snowX = 0, snowY = 5;
static int snowX = 0, snowY = 3;
static int rainDelay = 10;
static int snowDelay = 10;
static boolean dragging = false;
static int mouseX, mouseY;
static Timer snowTimer, rainTimer;

final static double GRAVITY = 0.05;
static double GRAVITY = 0.10; //0.10

Menu menu = new Menu();

Game() {
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
menu.mouseClicked(e);
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
menu.mousePressed(e);

Expand All @@ -67,6 +71,7 @@ public void mousePressed(MouseEvent e) {
}
}

@Override
public void mouseReleased(MouseEvent e) {
menu.mouseReleased(e);

Expand All @@ -82,13 +87,15 @@ public void mouseReleased(MouseEvent e) {
});

addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
menu.mouseDragged(e);

mouseX = e.getX();
mouseY = e.getY();
}

@Override
public void mouseMoved(MouseEvent e) {
menu.mouseMoved(e);

Expand Down Expand Up @@ -148,6 +155,10 @@ public double xSpawnPosition(int type) {
}

public void update() {
if(particleList.size() > 2500) {
particleList.clear();
}

frames++;
for (int i = 0; i < particleList.size(); i++) {
try {
Expand Down Expand Up @@ -204,6 +215,7 @@ public void update() {
rainTimer = new Timer();

snowTimer.schedule(new TimerTask() {
@Override
public void run() {
if (SNOW) {
particleList.add(new Particle(xSpawnPosition(1), -2, snowX, snowY, 10, randInt(2, 5), (float) randDouble(0.01, 1), 1));
Expand All @@ -212,6 +224,7 @@ public void run() {
}, 0, snowDelay);

rainTimer.schedule(new TimerTask() {
@Override
public void run() {
if (RAIN) {
particleList.add(new Particle(xSpawnPosition(2), -2, rainX, rainY, 5, randInt(2, 3), (float) randDouble(0.01, 0.8), 2));
Expand All @@ -232,17 +245,18 @@ public double getYFromAngle(double initialY, int angle, double velocity) {
return initialY + (velocity * Math.cos(angle));
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

if (NIGHT == true) {
g2d.setPaint(new GradientPaint(width / 2, height, new Color(0x212121), width / 2, 0, new Color(0x00263B)));
} else {
g2d.setPaint(new GradientPaint(width / 2, height, new Color(0xF2B3DA), width / 2, 0, new Color(0x91D5FF)));
g2d.setPaint(new GradientPaint(width / 2, height, new Color(0x305e91), width / 2, 0, new Color(0xC8ACF1)));
}
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.black);
Expand Down
28 changes: 23 additions & 5 deletions ProjectileTest/src/gui/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class Menu {
Rectangle changeAngle;

Rectangle dayNightToggle;

Rectangle gravityToggle;

Rectangle exitRect;

Expand Down Expand Up @@ -94,6 +96,12 @@ public void mouseClicked(MouseEvent e) {
Game.snowX = xVariation;
} else if (dayNightToggle.contains(p)) {
Game.NIGHT = !Game.NIGHT;
} else if (gravityToggle.contains(p)) {
if(Game.GRAVITY != 0) {
Game.GRAVITY = 0;
} else {
Game.GRAVITY = 0.10;
}
} else if (exitRect.contains(p)) {
System.exit(0);
}
Expand Down Expand Up @@ -146,6 +154,8 @@ public void update() {
changeAngle = new Rectangle(x + 10, y + 130, width - 20, height - 280);

dayNightToggle = new Rectangle(x + 10, y + 160, width - 20, height - 280);

gravityToggle = new Rectangle(x + 10, y + 190, width - 20, height - 280);

exitRect = new Rectangle(x + 10, y + 270, width - 20, height - 280);

Expand All @@ -165,10 +175,10 @@ public void paint(Graphics2D g2d) {
g2d.setColor(Color.GRAY);
g2d.fill(moveRect);

FontMetrics fmT = g2d.getFontMetrics(titleFont);
FontMetrics fmT = g2d.getFontMetrics(textFont);
g2d.setFont(titleFont);
g2d.setColor(Color.BLACK);
g2d.drawString(Reference.NAME + " " + Reference.VERSION, moveRect.x + 5, (moveRect.y + (moveRect.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString(Reference.NAME + " " + Reference.VERSION + " Options", moveRect.x + 5, (moveRect.y + (moveRect.height / 2)) + (fmT.getHeight() / 4));

g2d.setColor(Color.BLACK);
g2d.fill(minRect);
Expand All @@ -188,7 +198,7 @@ public void paint(Graphics2D g2d) {

g2d.setColor(Color.GRAY);
g2d.drawString("+", rainPlus.x + (rainPlus.width / 2) - (fmT.stringWidth("+") / 2), (rainPlus.y + (rainPlus.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString("Rain Toggle", rainRect.x + (rainRect.width / 2) - (fmT.stringWidth("Rain Toggle") / 2), (rainRect.y + (rainRect.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString("Rain Toggle (" + Game.rainDelay + ")", rainRect.x + (rainRect.width / 2) - (fmT.stringWidth("Rain Toggle (" + Game.rainDelay + ")") / 2), (rainRect.y + (rainRect.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString("-", rainMinus.x + (rainMinus.width / 2) - (fmT.stringWidth("-") / 2), (rainMinus.y + (rainMinus.height / 2)) + (fmT.getHeight() / 4));


Expand All @@ -199,7 +209,7 @@ public void paint(Graphics2D g2d) {

g2d.setColor(Color.GRAY);
g2d.drawString("+", snowPlus.x + (snowPlus.width / 2) - (fmT.stringWidth("+") / 2), (snowPlus.y + (snowPlus.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString("Snow Toggle", snowRect.x + (snowRect.width / 2) - (fmT.stringWidth("Snow Toggle") / 2), (snowRect.y + (snowRect.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString("Snow Toggle (" + Game.snowDelay + ")", snowRect.x + (snowRect.width / 2) - (fmT.stringWidth("Snow Toggle (" + Game.snowDelay + ")") / 2), (snowRect.y + (snowRect.height / 2)) + (fmT.getHeight() / 4));
g2d.drawString("-", snowMinus.x + (snowMinus.width / 2) - (fmT.stringWidth("-") / 2), (snowMinus.y + (snowMinus.height / 2)) + (fmT.getHeight() / 4));

g2d.setColor(new Color(51, 51, 51, 220));
Expand All @@ -212,11 +222,19 @@ public void paint(Graphics2D g2d) {
g2d.fill(dayNightToggle);

g2d.setColor(Color.GRAY);
g2d.drawString("Day/Night Toggle", dayNightToggle.x + (dayNightToggle.width / 2) - (fmT.stringWidth("Day/Night Toggle") / 2), (dayNightToggle.y + (dayNightToggle.height / 2)) + (fmT.getHeight() / 4));

g2d.setColor(new Color(51, 51, 51, 220));
g2d.fill(gravityToggle);

g2d.setColor(Color.GRAY);
g2d.drawString("Gravity Toggle", gravityToggle.x + (gravityToggle.width / 2) - (fmT.stringWidth("Gravity Toggle") / 2), (gravityToggle.y + (gravityToggle.height / 2)) + (fmT.getHeight() / 4));

g2d.setColor(new Color(0x521616));
g2d.fill(exitRect);

g2d.setColor(Color.GRAY);
g2d.setColor(Color.WHITE);
g2d.drawString("Exit", exitRect.x + (exitRect.width / 2) - (fmT.stringWidth("Exit") / 2), (exitRect.y + (exitRect.height / 2)) + (fmT.getHeight() / 4));
}
}

Expand Down
5 changes: 3 additions & 2 deletions ProjectileTest/src/gui/Particle.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Particle {
boolean touchFloor = false;
float alpha;

final double GRAVITY = Game.GRAVITY;
double GRAVITY = Game.GRAVITY;

double FRICTION_COEFFICIENT;
double BOUNCE_COEFFICIENT;
Expand All @@ -42,7 +42,7 @@ public class Particle {
if (TYPE == 0) {
color = Color.WHITE;
this.FRICTION_COEFFICIENT = 0.7;
this.BOUNCE_COEFFICIENT = 0.62;
this.BOUNCE_COEFFICIENT = 0.7;
this.lifetime = lifetime * Game.TARGET_FPS;
rect = new Ellipse2D.Double(x, y, size, size);
} else if (TYPE == 1) {
Expand All @@ -68,6 +68,7 @@ private AlphaComposite makeComposite(float alpha) {
}

public void update() {
GRAVITY = Game.GRAVITY;
age++;

// setY(Game.height - rect.getHeight());
Expand Down

0 comments on commit d1c7950

Please sign in to comment.