@Override public void paintComponent(Graphics gr) { super.paintComponent(gr); Graphics2D g = (Graphics2D) gr; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); if (level != null) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } level.updateLevel(); double xS = level.getScreenXShift(); double yS = level.getScreenYShift(); if (level.timeToReset() && !CollisionEffect.started()) { CollisionEffect.start(level); } if (CollisionEffect.running()) { int[] vals = CollisionEffect.update(); xS = vals[0]; yS = vals[1]; } else { CollisionEffect.kill(); } level.draw((int) xS, (int) yS, g); if (CollisionEffect.running()) { CollisionEffect.draw(g); } else if (!CollisionEffect.running() && level.timeToReset()) { level.reset(); } else { level.getBall().draw(xS, yS, g); } repaint(); } }
/** * Updates the effect. * * @param g The Graphics component the particles are being drawn with. * @return the new screenX and screenY shift values */ public static int[] update() { double screenXShift = currentLevel.getScreenXShift(); double screenYShift = currentLevel.getScreenYShift(); long elapsedTime = elapsedTime(); screenXShift += (shakeValues[0] * Math.sin(elapsedTime / shakeValues[1]) * Math.exp(shakeValues[2] * elapsedTime)); screenYShift += (shakeValues[3] * Math.sin(elapsedTime / shakeValues[4]) * Math.exp(shakeValues[5] * elapsedTime)); for (int i = 0; i < particles.size(); i++) { Particle p = particles.get(i); for (Body bod : currentLevel.getBodies()) { if (bod.intersects(p) || bod.getCenter().distanceSquared(p.getCenter()) <= (bod.getRadius() * .5) * (bod.getRadius() * .5) && i != 0) { particles.remove(i); i--; } } for (Blockage blockage : currentLevel.getBlockages()) { if (blockage.intersects(p.getCenter()) && i != 0) { particles.remove(i); i--; } } for (GoalPost gp : currentLevel.getGoalPosts()) { if (p.intersects(gp) && i != 0) { particles.remove(i); i--; } } p.setVelocity(p.getVelocity().multiply(0.99)); p.move(); } xShift = (int) screenXShift; yShift = (int) screenYShift; return new int[] {xShift, yShift}; }