import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Dimension; import java.awt.Color; import java.awt.image.BufferedImage; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.AlphaComposite; public class SpaceWars extends JFrame { static String windowTitle = "SpaceWars"; public SpaceWars(String name){ setTitle(name); setDefaultCloseOperation(EXIT_ON_CLOSE); //フレームを終了 } public static void main(String args[]){ SpaceWars obj = new SpaceWars(windowTitle); SpaceWarsPanel panel = new SpaceWarsPanel(); obj.getContentPane().add(panel); obj.pack(); Thread t = new Thread(panel); t.start(); obj.setVisible(true); } } class SpaceWarsPanel extends JPanel implements Runnable { public int count = 0; Thread thread = null; Star[] stars = new Star[300]; ShootingStar shootstars[] = new ShootingStar[10]; Explosion explosions[] = new Explosion[7]; static int screenW = 640; static int screenH = 480; BufferedImage offImage = new BufferedImage(screenW, screenH, BufferedImage.TYPE_INT_BGR); public SpaceWarsPanel() { Graphics2D g = offImage.createGraphics(); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); count = 0; for(int i=0 ; i 255) { brightness = 255; speed = -speed; } if(brightness < 0) { brightness = 0; speed = -speed; } color = new Color(brightness, brightness, brightness); } } class ShootingStar { public static final int PHASE0 = 0; public static final int PHASE1 = 1; public static final int PHASE2 = 2; public int phase = PHASE0; public double iniX = 10; public double iniY = 10; public double endX = 100; public double endY = 100; public double waitcount=0; public double waitlimit=100; public double t = 0; ShootingStar() { init(); phase = PHASE2; } public void init() { iniX = Math.random() * SpaceWarsPanel.screenW; iniY = Math.random() * SpaceWarsPanel.screenH; endX = Math.random() * SpaceWarsPanel.screenW; endY = Math.random() * SpaceWarsPanel.screenH; waitlimit = Math.random() * 15 + 5; phase = PHASE0; } public void draw(Graphics g) { g.setColor(Color.white); switch(phase) { case PHASE0: phase0(g); break; case PHASE1: phase1(g); break; } } public void proc() { switch(phase) { case PHASE0: t += 0.1; if(t>1) { t=0; phase = PHASE1; } break; case PHASE1: t += 0.1; if(t>1) { t=0; phase = PHASE2; waitcount = 0; } break; case PHASE2: waitcount++; if(waitcount>waitlimit) { init(); } break; } } public void phase0(Graphics g) { double px, py; px = t*(endX - iniX); py = t*(endY - iniY); g.drawLine((int)iniX, (int)iniY, (int)(iniX + px), (int)(iniY + py)); } public void phase1(Graphics g) { double px, py; px = t*(endX - iniX); py = t*(endY - iniY); g.drawLine((int)(iniX + px), (int)(iniY + py), (int)endX, (int)endY); } } class Explosion { double x; double y; double r; double maxR; Color color = null; int phase; static final int PHASE0 = 0; static final int PHASE1 = 1; static final int PHASE2 = 2; public double waitcount=0; public double waitlimit=10; public double t = 0; public Explosion() { init(); phase = PHASE2; } public void init() { x = Math.random() * SpaceWarsPanel.screenW; y = Math.random() * SpaceWarsPanel.screenH; maxR = Math.random() * 20 + 10; waitlimit = Math.random() * 20 + 5; color = new Color(255, 255, 255, 255); phase = PHASE0; } public void draw(Graphics g) { g.setColor(Color.white); switch(phase) { case PHASE0: phase0(g); break; case PHASE1: phase1(g); break; } } public void proc() { switch(phase) { case PHASE0: t += 0.05; if(t>1) { t=1; phase = PHASE1; } color = new Color(1.f, 1.f, (float)(1.5-t>1.0 ? 1.0 : 1.5-t)); break; case PHASE1: t -= 0.01; color = new Color(1.f, 1.f, (float)(1.5-t>1.0 ? 1.0 : 1.5-t)); if(t<0.5) { t=0; phase = PHASE2; waitcount = 0; } break; case PHASE2: waitcount++; if(waitcount>waitlimit) { init(); } break; } } public void phase0(Graphics g) { r = t*maxR; Graphics2D g2 = (Graphics2D)g; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); g.setColor(color); g.fillOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r)); } public void phase1(Graphics g) { r = t*maxR; Graphics2D g2 = (Graphics2D)g; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)t)); g.setColor(color); g.fillOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r)); } }