1
0

Add drawToImage method in Background

This commit is contained in:
Victor 2014-10-25 16:48:42 +03:00
parent 8ae8ce81fd
commit 9d2f16fd21
3 changed files with 29 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import static com.annimon.turrets.Constants.WIDTH;
import com.annimon.turrets.util.Util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
/**
*
@ -12,12 +13,18 @@ import java.awt.Graphics;
*/
public final class Background {
public static void drawToImage(BufferedImage img) {
final Graphics g = img.createGraphics();
new Background().draw(g);
g.dispose();
}
private static final int STARS_COUNT = (int) Math.sqrt(WIDTH * HEIGHT);
private final Color bg, color;
private final double shadeAmount;
public Background() {
private Background() {
bg = new Color(Util.randomColor(0, 15));
color = new Color(Util.randomColor(150, 235));
shadeAmount = Util.rand(0.5, 2.5);

View File

@ -11,7 +11,6 @@ import com.annimon.turrets.util.ExceptionHandler;
import com.annimon.turrets.util.Util;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
@ -154,9 +153,7 @@ public final class GameCanvas extends DoubleBufferedCanvas implements Runnable,
}
private void initBackground() {
final Graphics g = background.createGraphics();
new Background().draw(g);
g.dispose();
Background.drawToImage(background);
}
private void startGame(long seed) {

View File

@ -22,42 +22,46 @@ public final class GuiUtil {
public static <T extends JComponent> JLayer<T> createPlanetLayer(T component) {
return new JLayer<>(component, (LayerUI<T>) new PlanetBackground());
}
public static class GradientBackground extends javax.swing.plaf.LayerUI<JComponent> {
public static <T extends JComponent> JLayer<T> createGradientLayer(T component) {
return new JLayer<>(component, (LayerUI<T>) new GradientBackground());
}
private static class GradientBackground extends BackgroundLayer {
@Override
public void paint(Graphics g, JComponent component) {
super.paint(g, component);
final int w = component.getWidth();
final int h = component.getHeight();
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f));
protected void paint(Graphics2D g2d, int w, int h) {
g2d.setPaint(new GradientPaint(0, 0, Color.BLACK, 0, h, Color.GREEN));
g2d.fillRect(0, 0, w, h);
g2d.dispose();
}
}
public static class PlanetBackground extends javax.swing.plaf.LayerUI<JComponent> {
private static class PlanetBackground extends BackgroundLayer {
private final BufferedImage image;
public PlanetBackground() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
new Background().draw(g);
g.dispose();
Background.drawToImage(image);
}
@Override
protected void paint(Graphics2D g2d, int w, int h) {
g2d.drawImage(image, 0, 0, null);
}
}
private static abstract class BackgroundLayer extends LayerUI<JComponent> {
@Override
public void paint(Graphics g, JComponent component) {
super.paint(g, component);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f));
g2d.drawImage(image, 0, 0, null);
paint(g2d, component.getWidth(), component.getHeight());
g2d.dispose();
}
protected abstract void paint(Graphics2D g, int w, int h);
}
}