// From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135 void drawArrow( Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke, BasicStroke drawStroke) { double aDir = Math.atan2(xCenter - x, yCenter - y); // Line can be dashed. g2d.setStroke(drawStroke); g2d.drawLine(x, y, xCenter, yCenter); // make the arrow head solid even if dash pattern has been specified g2d.setStroke(lineStroke); Polygon tmpPoly = new Polygon(); int i1 = 12 + (int) (stroke * 2); // make the arrow head the same size regardless of the length length int i2 = 6 + (int) stroke; tmpPoly.addPoint(x, y); tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5)); tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir)); tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5)); tmpPoly.addPoint(x, y); // arrow tip g2d.drawPolygon(tmpPoly); // Remove this line to leave arrow head unpainted: g2d.fillPolygon(tmpPoly); }
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); }
/** * draw triangle * * @param g graphics * @param x1 x1 * @param y1 y1 * @param x2 x2 * @param y2 y2 * @param x3 x3 * @param y3 y3 */ private void drawRotatedTriangle( Graphics2D g, double x1, double y1, double x2, double y2, double x3, double y3) { /** for drawing */ int[] xPoint = new int[3]; /** for drawing */ int[] yPoint = new int[3]; xPoint[0] = x0 + (int) (x1 * cost - y1 * sint); yPoint[0] = y0 + (int) (x1 * sint + y1 * cost); double s = isSouth ? -1 : 1; xPoint[1] = x0 + (int) (x2 * s * cost - y2 * sint); yPoint[1] = y0 + (int) (x2 * s * sint + y2 * cost); xPoint[2] = x0 + (int) (x3 * cost - y3 * sint); yPoint[2] = y0 + (int) (x3 * sint + y3 * cost); g.fillPolygon(xPoint, yPoint, 3); }
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); }
protected void hitOrDraw(Graphics2D graphics, DrawInfo2D info, Bag putInHere) { final Grid2D field = (Grid2D) (this.field); if (field == null) return; // first question: determine the range in which we need to draw. final int maxX = field.getWidth(); final int maxY = field.getHeight(); if (maxX == 0 || maxY == 0) return; final double divideByX = ((maxX % 2 == 0) ? (3.0 * maxX / 2.0 + 0.5) : (3.0 * maxX / 2.0 + 2.0)); final double divideByY = (1.0 + 2.0 * maxY); final double xScale = info.draw.width / divideByX; final double yScale = info.draw.height / divideByY; int startx = (int) (((info.clip.x - info.draw.x) / xScale - 0.5) / 1.5) - 2; int starty = (int) ((info.clip.y - info.draw.y) / (yScale * 2.0)) - 2; int endx = /*startx +*/ (int) (((info.clip.x - info.draw.x + info.clip.width) / xScale - 0.5) / 1.5) + 4; // with rounding, width be as much as 1 off int endy = /*starty +*/ (int) ((info.clip.y - info.draw.y + info.clip.height) / (yScale * 2.0)) + 4; // with rounding, height be as much as 1 off // // // CAUTION! // // At some point we should triple check the math for rounding such // that the margins are drawn properly // // // next we determine if this is a DoubleGrid2D or an IntGrid2D // final Rectangle clip = (graphics==null ? null : graphics.getClipBounds()); final boolean isDoubleGrid2D = (field instanceof DoubleGrid2D); final double[][] doubleField = (isDoubleGrid2D ? ((DoubleGrid2D) field).field : null); final int[][] intField = (isDoubleGrid2D ? null : ((IntGrid2D) field).field); double xyC_x, xyC_y, xyC_ulx, xyC_uly, xyC_upx, xyC_upy, xyC_urx, xyC_ury, x0, y0, tx, ty; if (startx < 0) startx = 0; if (starty < 0) starty = 0; if (endx > maxX) endx = maxX; if (endy > maxY) endy = maxY; for (int y = starty; y < endy; y++) for (int x = startx; x < endx; x++) { // getxyC( x, y, xScale, yScale, info.draw.x, info.draw.y, xyC ); // getxyC( field.ulx(x,y), field.uly(x,y), xScale, yScale, info.draw.x, info.draw.y, xyC_ul // ); // getxyC( field.upx(x,y), field.upy(x,y), xScale, yScale, info.draw.x, info.draw.y, xyC_up // ); // getxyC( field.urx(x,y), field.ury(x,y), xScale, yScale, info.draw.x, info.draw.y, xyC_ur // ); x0 = x; y0 = y; tx = info.draw.x; ty = info.draw.y; xyC_x = tx + xScale * (1.5 * x0 + 1); xyC_y = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2)); x0 = field.ulx(x, y); y0 = field.uly(x, y); tx = info.draw.x; ty = info.draw.y; xyC_ulx = tx + xScale * (1.5 * x0 + 1); xyC_uly = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2)); x0 = field.upx(x, y); y0 = field.upy(x, y); tx = info.draw.x; ty = info.draw.y; xyC_upx = tx + xScale * (1.5 * x0 + 1); xyC_upy = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2)); x0 = field.urx(x, y); y0 = field.ury(x, y); tx = info.draw.x; ty = info.draw.y; xyC_urx = tx + xScale * (1.5 * x0 + 1); xyC_ury = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2)); xPoints[0] = (int) (xyC_urx - 0.5 * xScale); yPoints[0] = (int) (xyC_ury + yScale); xPoints[1] = (int) (xyC_upx + 0.5 * xScale); yPoints[1] = (int) (xyC_upy + yScale); xPoints[2] = (int) (xyC_upx - 0.5 * xScale); yPoints[2] = (int) (xyC_upy + yScale); xPoints[3] = (int) (xyC_ulx + 0.5 * xScale); yPoints[3] = (int) (xyC_uly + yScale); xPoints[4] = (int) (xyC_x - 0.5 * xScale); yPoints[4] = (int) (xyC_y + yScale); xPoints[5] = (int) (xyC_x + 0.5 * xScale); yPoints[5] = (int) (xyC_y + yScale); if (graphics == null) { generalPath.reset(); generalPath.moveTo(xPoints[0], yPoints[0]); for (int i = 1; i < 6; i++) generalPath.lineTo(xPoints[i], yPoints[i]); generalPath.closePath(); Area area = new Area(generalPath); if (area.intersects(info.clip.x, info.clip.y, info.clip.width, info.clip.height)) { valueToPass.val = isDoubleGrid2D ? doubleField[x][y] : intField[x][y]; putInHere.add(getWrapper(valueToPass.val, x, y)); } } else { Color c = map.getColor(isDoubleGrid2D ? doubleField[x][y] : intField[x][y]); if (c.getAlpha() == 0) continue; graphics.setColor(c); // MacOS X 10.3 Panther has a bug which resets the clip, YUCK // graphics.setClip(clip); graphics.fillPolygon(xPoints, yPoints, 6); } } }
// also clip, transform, composite, // public boolean isOpaque(){return false;}//theOpaque!=null&&theOpaque;} // --------------------------------------------------------- private void doPaint(Graphics2D g, int s, Object o) { // process an operation from the buffer // System.out.println(s); Object o1 = null, o2 = null, o3 = null, o4 = null, o5 = null, o6 = null, o7 = null, o8 = null, o9 = null, o10 = null, o11 = null; if (o instanceof Object[]) { Object[] a = (Object[]) o; if (a.length > 0) o1 = a[0]; if (a.length > 1) o2 = a[1]; if (a.length > 2) o3 = a[2]; if (a.length > 3) o4 = a[3]; if (a.length > 4) o5 = a[4]; if (a.length > 5) o6 = a[5]; if (a.length > 6) o7 = a[6]; if (a.length > 7) o8 = a[7]; if (a.length > 8) o9 = a[8]; if (a.length > 9) o10 = a[9]; if (a.length > 10) o11 = a[10]; } switch (s) { case clear: paintBackground(g, theBackground); break; // public void addRenderingHints(Map<?,?> hints) // {toBuffer("addRenderingHints",hints );} case addRenderingHints: g.addRenderingHints((Map<?, ?>) o); break; case clip1: g.clip((Shape) o); break; case draw1: g.draw((Shape) o); break; case draw3DRect: g.draw3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5); break; case drawGlyphVector: g.drawGlyphVector((GlyphVector) o1, (Float) o2, (Float) o3); break; case drawImage1: g.drawImage((BufferedImage) o1, (BufferedImageOp) o2, (Integer) o3, (Integer) o4); break; case drawImage2: g.drawImage((Image) o1, (AffineTransform) o2, (ImageObserver) o3); break; case drawRenderableImage: g.drawRenderableImage((RenderableImage) o1, (AffineTransform) o2); break; case drawRenderedImage: g.drawRenderedImage((RenderedImage) o1, (AffineTransform) o2); break; case drawString1: g.drawString((AttributedCharacterIterator) o1, (Float) o2, (Float) o3); break; case drawString2: g.drawString((AttributedCharacterIterator) o1, (Integer) o2, (Integer) o3); break; case drawString3: g.drawString((String) o1, (Float) o2, (Float) o3); break; case drawString4: g.drawString((String) o1, (Integer) o2, (Integer) o3); break; case fill: g.fill((Shape) o); break; case fill3DRect: g.fill3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5); break; case rotate1: g.rotate((Double) o); break; case rotate2: g.rotate((Double) o1, (Double) o2, (Double) o3); break; case scale1: g.scale((Double) o1, (Double) o2); break; case setBackground: g.setBackground( (Color) o); // paintBackground(g,(Color)o); /*super.setBackground((Color)o) ;*/ break; case setComposite: g.setComposite((Composite) o); break; case setPaint: g.setPaint((Paint) o); break; case setRenderingHint: g.setRenderingHint((RenderingHints.Key) o1, o2); break; case setRenderingHints: g.setRenderingHints((Map<?, ?>) o); break; case setStroke: g.setStroke((Stroke) o); break; case setTransform: g.setTransform(makeTransform(o)); break; case shear: g.shear((Double) o1, (Double) o2); break; case transform1: g.transform(makeTransform(o)); break; case translate1: g.translate((Double) o1, (Double) o2); break; case translate2: g.translate((Integer) o1, (Integer) o2); break; case clearRect: g.clearRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case copyArea: g.copyArea( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case drawArc: g.drawArc( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case drawBytes: g.drawBytes((byte[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5); break; case drawChars: g.drawChars((char[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5); break; case drawImage4: g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (Color) o4, (ImageObserver) o5); break; case drawImage5: g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (ImageObserver) o4); break; case drawImage6: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Color) o6, (ImageObserver) o7); break; case drawImage7: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (ImageObserver) o6); break; case drawImage8: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6, (Integer) o7, (Integer) o8, (Integer) o9, (Color) o10, (ImageObserver) o11); break; case drawImage9: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6, (Integer) o7, (Integer) o8, (Integer) o9, (ImageObserver) o10); break; case drawLine: g.drawLine((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawOval: g.drawOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawPolygon1: g.drawPolygon((int[]) o1, (int[]) o2, (Integer) o3); break; case drawPolygon2: g.drawPolygon((Polygon) o); break; case drawPolyline: g.drawPolyline((int[]) o1, (int[]) o2, (Integer) o3); break; case drawRect: g.drawRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawRoundRect: g.drawRoundRect( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case fillArc: g.fillArc( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case fillOval: g.fillOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; // {toBuffer("fillPolygon",mkArg(xPoints, yPoints, nPoints) );} case fillPolygon1: g.fillPolygon((int[]) o1, (int[]) o2, (Integer) o3); break; case fillPolygon2: g.fillPolygon((Polygon) o); break; case fillRect: g.fillRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case fillRoundRect: g.fillRoundRect( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case setClip1: g.setClip((Shape) o); break; case setColor: g.setColor((Color) o); break; case setFont: g.setFont((Font) o); break; case setPaintMode: g.setPaintMode(); break; case setXORMode: g.setXORMode((Color) o); break; case opaque: super.setOpaque((Boolean) o); break; case drawOutline: // g.drawString((String)o1, (Integer)o2, (Integer)o3) ;break; { FontRenderContext frc = g.getFontRenderContext(); TextLayout tl = new TextLayout((String) o1, g.getFont(), frc); Shape s1 = tl.getOutline(null); AffineTransform af = g.getTransform(); g.translate((Integer) o2, (Integer) o3); g.draw(s1); g.setTransform(af); } ; break; default: System.out.println("Unknown image operation " + s); } }