import java.awt.image.BufferedImage; import java.awt.Graphics2D; import java.awt.Color; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; import java.awt.Rectangle; import java.awt.Polygon; import java.awt.Image; import java.awt.RenderingHints; /** * Draw an awesome looking tank * * @author Moises Ramirez (original aritisty), and Phill Conrad (minor coding help) * @version for lab06, 05/21/2009 */ public class MoisesTank { private BufferedImage tank; public MoisesTank(int width, int height) { // This creates a new image object that we can draw on, including drawing fills // The TYPE_INT_ARGB just means that we have Red, Green, Blue and "Alpha" // 'Alpha' is a way of making this transparent, or partially transparent. final int ORIG_ULX = 0 ; // the leftmost point in the thing you are drawing final int ORIG_ULY = 160 ; // the point closest to the top of the screen in your drawing (lowest Y number) final int ORIG_WIDTH = 480; // the distance from the left most point to the right most point final int ORIG_HEIGHT = 160; // the distance from the top of your drawing to the bottom // The next two numbers are how big your original drawing surface was final int ORIG_TOTAL_WIDTH = 480; // width of the ENTIRE drawing surface final int ORIG_TOTAL_HEIGHT = 640; // height of the ENTIRE drawing surface // Create a temporary image that we can draw on, that is just like the original // surface you used to create your image. We'll then crop the image out of that, // and finally scale it to the size that the user passed into the constructor. BufferedImage origTank = new BufferedImage(ORIG_TOTAL_WIDTH, ORIG_TOTAL_HEIGHT, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = origTank.createGraphics(); // Make a white background. For some reason not clear to us, this seems to be necessary---otherwise it ends up black. Rectangle background = new Rectangle(0,0,ORIG_TOTAL_WIDTH,ORIG_TOTAL_HEIGHT); g2.setColor(Color.WHITE); g2.fill(background); // BELOW THIS LINE, JUST PUT ALL THE CODE THAT WAS ORIGINALLY IN // YOUR paintComponent method, EXCEPT FOR THE LINE Graphics2D g2 = (Graphics2D) g; Color wheelBand = new Color(23,23,23); Color darkerGreen = new Color(51,102,0); Color armyLightGreen = new Color(102,153,0); Color armyDarkGreen = new Color(69, 139,0); Color wheelColor = new Color(189,183,107); // Dimension for work space 319 height x 480 width // **Code to draw a simple 2d tank.** // //*Code for cannon of tank* Rectangle cannon = new Rectangle(237,205, 180, 13); Rectangle cannonEnd = new Rectangle(417,202, 7, 19); g2.setColor(Color.BLACK); g2.draw(cannonEnd); g2.fill(cannonEnd); g2.draw(cannon); g2.fill(cannon); //*Code for body of tank* //*upper part of tank* //*Hatch of tank (Door) Polygon tankHatch = new Polygon(); tankHatch.addPoint(102, 196); tankHatch.addPoint(103, 186); tankHatch.addPoint(200, 189); tankHatch.addPoint(199, 196); g2.draw(tankHatch); g2.fill(tankHatch); //*Tank exhaust g2.setColor(Color.GRAY); Rectangle tankExhaustTip = new Rectangle (71, 170, 4, 10); g2.draw(tankExhaustTip); g2.fill(tankExhaustTip); g2.setColor(Color.BLACK); Rectangle tankExhaust = new Rectangle (68, 180, 10, 30); g2.draw(tankExhaust); g2.fill(tankExhaust); //*Antenna of tank Polygon antenna = new Polygon(); antenna.addPoint(55,210); antenna.addPoint(57,210); antenna.addPoint(43,160); antenna.addPoint(41,161); g2.draw(antenna); g2.fill(antenna); Polygon topBody = new Polygon(); topBody.addPoint(240, 259); topBody.addPoint(260, 230); topBody.addPoint(230, 195); topBody.addPoint(90, 190); topBody.addPoint(50, 210); topBody.addPoint(60, 259); g2.setColor(armyDarkGreen); g2.draw(topBody); g2.fill(topBody); Polygon bottomBody = new Polygon(); bottomBody.addPoint(306,259); bottomBody.addPoint(30,245); bottomBody.addPoint(15,260); bottomBody.addPoint(35,280); g2.setColor(darkerGreen); g2.draw(bottomBody); g2.fill(bottomBody); //Lower Parts of Tank Rectangle wheelHind = new Rectangle(50, 319-30, 250, 30); g2.draw(wheelHind); g2.setColor(wheelBand); g2.fill(wheelHind); Circle circularHindOne = new Circle(50,319-23,23); Circle circularHindTwo = new Circle(294,319-23,23); g2.draw(circularHindOne); g2.fill(circularHindOne); g2.draw(circularHindTwo); g2.fill(circularHindTwo); //*Code for wheels of tank* int xCoord = 52; g2.setColor(wheelColor); for(int i =1; i<=7; i++) { Circle wheel = new Circle ( xCoord, 297, 18); xCoord += 40; g2.draw(wheel); g2.fill(wheel); } //*touch up on wheels* xCoord = 52; g2.setColor(Color.BLACK); for(int i =1; i<=7; i++) { Circle innerWheel = new Circle ( xCoord, 297, 8); xCoord += 40; g2.draw(innerWheel); g2.fill(innerWheel); } g2.setColor(armyLightGreen); Rectangle wheelCover = new Rectangle(40, 259, 266, 40); g2.draw(wheelCover); g2.fill(wheelCover); //*touch up on wheelCover g2.setColor(Color.BLACK); xCoord = 40; for (int i = 1; i <= 4; i++) { Rectangle coverTouchUp = new Rectangle(xCoord, 259, 66, 40); xCoord += 66; g2.draw(coverTouchUp); } g2.setColor(armyLightGreen); Polygon coverExtension = new Polygon(); coverExtension.addPoint(306,259); coverExtension.addPoint(326,259); coverExtension.addPoint(326,279); coverExtension.addPoint(306,299); g2.draw(coverExtension); g2.fill(coverExtension); //*Details on tank g2.setColor(wheelColor); Rectangle squaresHAroundDetail = new Rectangle(124, 210,61, 5); Rectangle squaresHAroundDetail2 = new Rectangle(124, 223,61, 5); g2.draw(squaresHAroundDetail); g2.fill(squaresHAroundDetail); g2.draw(squaresHAroundDetail2); g2.fill(squaresHAroundDetail2); Rectangle squaresVAroundDetail = new Rectangle(139, 199,7, 41); Rectangle squaresVAroundDetail2 = new Rectangle(164, 199,7, 41); g2.draw(squaresVAroundDetail); g2.fill(squaresVAroundDetail); g2.draw(squaresVAroundDetail2); g2.fill(squaresVAroundDetail2); g2.setColor(Color.BLACK); Rectangle detail = new Rectangle (130,205 ,50, 30); g2.draw(detail); g2.fill(detail); //ventilatioin on back of tank xCoord = 52; int yCoord = 220; for (int i = 1; i <=3; i++) { Rectangle vent = new Rectangle(xCoord, yCoord, 25, 2); xCoord += 2; yCoord += 8; g2.draw(vent); g2.fill(vent); } // ABOVE THIS LINE IS WHERE THE CODE YOU PASTE FROM YOUR OLD paintComponent METHOD STOPS // BELOW THIS LINE, ONLY CHANGE "tank" TO WHATEVER YOUR NEW IMAGE IS. // Next, crop out the portion of the drawing that is the thing we drew BufferedImage croppedImage = origTank.getSubimage(ORIG_ULX, ORIG_ULY, ORIG_WIDTH, ORIG_HEIGHT); // Now create the scaled version of the image. This is the one we save to the instance variable. tank = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // this starts out as a blank image // Paint scaled version of image to new image Graphics2D tg2 = tank.createGraphics(); tg2.drawImage(croppedImage, 0, 0, width, height, null); } public BufferedImage getImage() { return this.tank; } }