// set size of the button (inherited from JButton =) ) // changes the size of the button, and resizes the image to fit it. public void setSize(int x, int y) { if (!mode.equals("String")) { width = x; height = x; image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH); BWimage = BWimage.getScaledInstance(width, height, Image.SCALE_SMOOTH); } }
public void drawRocket( Image image, int xpos, int ypos, double rot, double xscale, double yscale) { int width = rocketImage.getWidth(this); int height = rocketImage.getHeight(this); g.translate(xpos, ypos); g.rotate(rot * Math.PI / 180.0); g.scale(xscale, yscale); g.drawImage(image, -width / 2, -height / 2, width, height, this); g.scale(1.0 / xscale, 1.0 / yscale); g.rotate(-rot * Math.PI / 180.0); g.translate(-xpos, -ypos); }
public void paint(Graphics gOld) { if (image == null || xsize != getSize().width || ysize != getSize().height) { xsize = getSize().width; ysize = getSize().height; image = createImage(xsize, ysize); g = (Graphics2D) image.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } // fill background g.setColor(Color.cyan); g.fillRect(0, 0, xsize, ysize); int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)}; int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)}; // fill border g.setColor(Color.black); g.fillPolygon(x, y, 4); // draw border g.setColor(Color.red); g.drawPolyline(x, y, 5); if (animateFirstTime) { gOld.drawImage(image, 0, 0, null); return; } if (gameOver) return; g.drawImage(outerSpaceImage, getX(0), getY(0), getWidth2(), getHeight2(), this); for (int index = 0; index < missile.length; index++) { if (missile[index].active) { g.setColor(Color.red); drawCircle(getX(missile[index].xPos), getYNormal(missile[index].yPos), 90, .3, 1.5); } } if (rocketRight) { drawRocket(rocketImage, getX(rocketXPos), getYNormal(rocketYPos), 0.0, 2.0, 2.0); } else { drawRocket(rocketImage, getX(rocketXPos), getYNormal(rocketYPos), 0.0, -2.0, 2.0); } for (int index = 0; index < numStars; index++) { g.setColor(Color.yellow); if (starActive[index]) drawCircle(getX(starXPos[index]), getYNormal(starYPos[index]), 0, 1.5, 1.5); } g.setColor(Color.magenta); g.setFont(new Font("Impact", Font.BOLD, 15)); g.drawString("Score: " + score, 10, 45); g.setColor(Color.magenta); g.setFont(new Font("Impact", Font.BOLD, 15)); g.drawString("HighScore: " + highScore, 300, 45); g.setColor(Color.magenta); g.setFont(new Font("Impact", Font.BOLD, 15)); g.drawString("Lives: " + rocketLife, 150, 45); if (rocketLife == 0) { g.setColor(Color.red); g.setFont(new Font("Impact", Font.BOLD, 60)); g.drawString("GAME OVER", getX(getWidth2() / 6), getYNormal(getHeight2() / 2)); } gOld.drawImage(image, 0, 0, null); }
public void reset() { // init the location of the rocket to the center. rocketXPos = getWidth2() / 2; rocketYPos = getHeight2() / 2; star = new Star[starNum]; missle = new Missle[missleNum]; firsttouch = new boolean[starNum]; for (int i = 0; i < starNum; i++) { star[i] = new Star(20, 20); star[i].setXPos((int) (Math.random() * getWidth2())); star[i].setYPos((int) (Math.random() * getHeight2())); while (star[i].getLeftSide() < rocketXPos + Rocketwidth / 2 && star[i].getRightSide() > rocketXPos - Rocketwidth / 2 && star[i].getTop() > rocketYPos - Rocketheight / 2 && star[i].getBottom() < rocketYPos + Rocketheight / 2) { star[i].setXPos((int) (Math.random() * getWidth2())); star[i].setYPos((int) (Math.random() * getHeight2())); } firsttouch[i] = false; } speed = 0; for (int i = 0; i < missle.length; i++) { missle[i] = new Missle(10, 10); } missleIndex = 0; Rocketwidth = rocketImage.getWidth(this); Rocketheight = rocketImage.getHeight(this); score = 0; lives = 3; GameOver = false; ff = false; ffwidth = 25; }
public void paint(Graphics gOld) { if (image == null || xsize != getSize().width || ysize != getSize().height) { xsize = getSize().width; ysize = getSize().height; image = createImage(xsize, ysize); g = (Graphics2D) image.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } // fill background g.setColor(Color.cyan); g.fillRect(0, 0, xsize, ysize); int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)}; int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)}; // fill border g.setColor(Color.black); g.fillPolygon(x, y, 4); // draw border g.setColor(Color.red); g.drawPolyline(x, y, 5); if (animateFirstTime) { gOld.drawImage(image, 0, 0, null); return; } g.drawImage(outerSpaceImage, getX(0), getY(0), getWidth2(), getHeight2(), this); g.setColor(Color.yellow); for (int i = 0; i < starNum; i++) { if (!GameOver) drawStar(getX(star[i].getXPos()), getYNormal(star[i].getYPos()), 0.0, 1.0, 1.0); } g.setColor(Color.white); for (int i = 0; i < missle.length; i++) { if (missle[i].getActive() && !GameOver) { drawCircle( getX(missle[i].getXPos()), getYNormal(missle[i].getYPos()), 0.0, 1.0, 1.0, missle[i].getWidth()); } } if (!GameOver) { if (!rocketRight) drawRocket(rocketImage, getX(rocketXPos), getYNormal(rocketYPos), 0, 1.0, 1.0); else drawRocket(rocketImage, getX(rocketXPos), getYNormal(rocketYPos), 0, -1.0, 1.0); } if (ff) { drawCircle(getX(rocketXPos), getYNormal(rocketYPos), 0, ffsize, ffsize, ffwidth); } g.setColor(Color.BLACK); g.drawString("Lives: " + lives, 115, 43); g.drawString("Score: " + score, 50, 43); if (GameOver) { g.setColor(Color.WHITE); g.drawString("GAMEOVER", getWidth2() / 2, getHeight2() / 2); g.drawString("HighScore: " + highscore, getWidth2() / 2, getHeight2() / 2 - 20); } gOld.drawImage(image, 0, 0, null); }