// purpose: paint a rocket taking off import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.image.*; public class TakeOff { // RGB value base public static final int RGB_BASE = 256; // ****** desired dimensions and background public static final int PICTURE_WIDTH = 500; public static final int PICTURE_HEIGHT = 700; // ****** convenience constant public static final int RGB = BufferedImage.TYPE_INT_RGB; // ****** rocket description final static String TEXT = "CS 1112"; final static int PIXELS_PER_CHARACTER = 8; final static int ROCKET_WIDTH = 1 + ( 4 + PIXELS_PER_CHARACTER ) * TEXT.length(); final static int HULL_HEIGHT = 270; final static int ANTENNA_HEIGHT = HULL_HEIGHT / 8; final static int CONE_HEIGHT = 2 * ROCKET_WIDTH / 3 ; final static int ROCKET_HEIGHT = ANTENNA_HEIGHT + CONE_HEIGHT + HULL_HEIGHT; final static int ANTENNA_WIDTH = 1; final static int FIN_WIDTH = ROCKET_WIDTH / 3 ; final static int FIN_HEIGHT = ROCKET_HEIGHT / 3 ; final static int FIN_THICKNESS = 5; final static Color BACKGROUND_COLOR = Color.BLACK; final static Color TEXT_COLOR = Color.BLUE; final static Color FIN_COLOR = Color.BLUE; final static Color ANTENNA_COLOR = Color.WHITE; final static Color CONE_COLOR = Color.RED; final static Color HULL_COLOR = Color.WHITE; final static Color FIRE_COLOR = Color.ORANGE; // method main(): program starting point public static void main( String[] args ) throws Exception { // ****** set up window JFrame window = new JFrame( "We have ignition" ); window.setAlwaysOnTop( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // ****** set up a surface for painting the image BufferedImage surface = new BufferedImage( PICTURE_WIDTH, PICTURE_HEIGHT, RGB ); // ****** get the window to put the surface on its content pane ImageIcon icon = new ImageIcon( surface ); JLabel pane = new JLabel( icon ); window.setContentPane( pane ); // ****** size and display the window window.pack(); window.setVisible( true ); // ****** get a renderer to manipulate the surface's display Graphics g = surface.getGraphics(); pane.setBackground( Color.BLACK ); // ****** paint the entire surface the background color g.setColor( BACKGROUND_COLOR ); g.fillRect( 0, 0, PICTURE_WIDTH, PICTURE_HEIGHT ); // ****** set current location to be where we want top of rocket int currentX = pane.getWidth() / 2; int currentY = pane.getHeight() - ROCKET_HEIGHT; // ****** ready to produce the animation while( currentY > -2 * ROCKET_HEIGHT ) { // **** paint background g.setColor( BACKGROUND_COLOR ); g.fillRect( 0, 0, pane.getWidth(), pane.getHeight() ); int xRocketTop = currentX; int yRocketTop = currentY; // **** paint antenna int xAntennaTop = xRocketTop; int yAntennaTop = yRocketTop; int xAntennaBottom = xRocketTop; int yAntennaBottom = yRocketTop + ANTENNA_HEIGHT; g.setColor( ANTENNA_COLOR ); g.drawLine( xAntennaTop, yAntennaTop, xAntennaBottom, yAntennaBottom ); // **** paint cone int xNoseCone = xAntennaBottom - ( ROCKET_WIDTH / 2 ); int yNoseCone = yAntennaBottom; g.setColor( CONE_COLOR ); g.fillOval( xNoseCone, yNoseCone, ROCKET_WIDTH, 2 * CONE_HEIGHT ); // **** paint hull int xHull = xNoseCone; int yHull = yNoseCone + CONE_HEIGHT; g.setColor( HULL_COLOR ); g.fillRect( xHull, yHull, ROCKET_WIDTH, HULL_HEIGHT ); // **** paint text int xText = xHull + ( ROCKET_WIDTH - ( TEXT.length() * PIXELS_PER_CHARACTER ) ) / 2; int yText = yHull + 3 * ANTENNA_HEIGHT / 2; g.setColor( TEXT_COLOR ); g.drawString( TEXT, xText, yText ); // **** paint left fin int axLeft = xHull; int ayLeft = yHull + HULL_HEIGHT; int bxLeft = axLeft - FIN_WIDTH; int byLeft = ayLeft; int cxLeft = axLeft; int cyLeft = ayLeft - FIN_HEIGHT; int[] xLeftFin = { axLeft, bxLeft, cxLeft }; int[] yLeftFin = { ayLeft, byLeft, cyLeft }; g.setColor( FIN_COLOR ); g.fillPolygon( xLeftFin, yLeftFin, 3 ); // **** paint right fin int axRight = xHull + ROCKET_WIDTH; int ayRight = yHull + HULL_HEIGHT; int bxRight = axRight + FIN_WIDTH; int byRight = ayRight; int cxRight = axRight; int cyRight = ayRight - FIN_HEIGHT; int[] xRightFin = { axRight, bxRight, cxRight }; int[] yRightFin = { ayRight, byRight, cyRight }; g.setColor( FIN_COLOR ); g.fillPolygon( xRightFin, yRightFin, 3 ); // **** paint middle fin int xMiddleFinTop = xRocketTop - ( FIN_THICKNESS / 2 ); int yMiddleFinTop = yRocketTop + ROCKET_HEIGHT - FIN_HEIGHT; g.setColor( FIN_COLOR ); g.fillRect( xMiddleFinTop, yMiddleFinTop, FIN_THICKNESS, FIN_HEIGHT ); // **** paint the fire int fireLeft = xHull; int fireRight = xHull + ROCKET_WIDTH; int fireTop = yHull + HULL_HEIGHT; int[] xFire = { fireLeft , fireLeft + ROCKET_WIDTH * 1 / 10, fireLeft + ROCKET_WIDTH * 3 / 10, fireLeft + ROCKET_WIDTH * 5 / 10, fireLeft + ROCKET_WIDTH * 7 / 10, fireLeft + ROCKET_WIDTH * 9 / 10, fireRight }; int[] yFire = { fireTop, fireTop + 70 - ( 20 * ( currentY % 2 ) ), fireTop + 40, fireTop + 130 + ( 20 * ( currentY % 3 ) ), fireTop + 30, fireTop + 60 + ( 20 * ( currentY % 2 ) ), fireTop }; g.setColor( FIRE_COLOR ); g.fillPolygon( xFire, yFire, 7 ); currentY = currentY - 25; currentX = currentX + 4; // enjoy the rising of the rocket Pause.moment(); pane.repaint(); } } }