@Override public void paint(Graphics2D g, IGizmo gizmo) { int orientation = gizmo.getOrientation(); int x = gizmo.getX(), y = gizmo.getY(); Flipper flipper = (Flipper) gizmo; Path2D.Double path = new Path2D.Double(); path.moveTo(x, y + 0.25); path.lineTo(x, y + 1.75); path.curveTo(x, y + 2, x + 0.5, y + 2, x + 0.5, y + 1.75); path.lineTo(x + 0.5, y + 0.25); path.curveTo(x + 0.5, y, x, y, x, y + 0.25); if (flipper.getAngle() != 0) path.transform(AffineTransform.getRotateInstance(flipper.getAngle(), x + 0.25, y + 0.25)); if (orientation != 0) path.transform(AffineTransform.getRotateInstance(Math.PI / 2 * orientation, x + 1, y + 1)); g.setColor(Color.ORANGE); g.fill(path); g.setColor(Color.ORANGE.darker()); g.draw(path); }
private void drawKeyText( String text, MIDICtlNoteCfgBean mccb, boolean highlight, boolean blackKey, int ctlNum) { Color oldColor = g2.getColor(); // int left=mccb.getLeft()+1; Color txtColour = Color.WHITE; if (!blackKey) { txtColour = Color.BLACK; } if (highlight) { txtColour = Color.RED; g2.setColor(Color.BLACK); g2.fillRect( mccb.getLeft(), mccb.getTop() + TOP_BAR_OFFSET, mccb.getWidth(), mccb.getHeight()); g2.setColor(Color.WHITE); g2.drawRect( mccb.getLeft(), mccb.getTop() + TOP_BAR_OFFSET, mccb.getWidth(), mccb.getHeight()); } g2.setTransform( AffineTransform.getRotateInstance( -Math.PI / 2, (double) (mccb.getLeft() + 13), (double) mccb.getTop() + TOP_BAR_OFFSET + mccb.getHeight())); text(text, mccb.getLeft() + 13, mccb.getTop() + TOP_BAR_OFFSET + mccb.getHeight(), txtColour); int keyBottom = mccb.getTop() + TOP_BAR_OFFSET + mccb.getHeight(); addBoundedObject("#" + ctlNum, mccb.getLeft(), keyBottom - 50, 13, keyBottom); g2.setTransform(new AffineTransform()); g2.setColor(oldColor); }
private synchronized void initBars() { if (bars != null) { return; } bars = new Area[BAR_COUNT]; final double fixedAngle = 2.0 * Math.PI / (double) bars.length; for (int i = 0; i < bars.length; ++i) { Area primitive = makeBar(); Point2D.Double center = new Point2D.Double((double) DIAMETER / 2, (double) DIAMETER / 2); AffineTransform toCircle = AffineTransform.getRotateInstance( ((double) -i) * fixedAngle, center.getX(), center.getY()); AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0); AffineTransform toScale = AffineTransform.getScaleInstance(0.1, 0.1); primitive.transform(toBorder); primitive.transform(toCircle); primitive.transform(toScale); bars[i] = primitive; } }
private void drawArrow(Graphics2D g2d, double sx, double sy, double px, double py, int pass) { if (pass == 0) { g2d.setStroke(new BasicStroke(4.0f)); g2d.setPaint(preSynapticSiteColor.brighter()); g2d.draw(new Line2D.Double(sx, sy, px, py)); return; } double dx = px - sx; double dy = py - sy; Polygon tip = new Polygon(); tip.addPoint(0, 0); tip.addPoint(-10, -20); tip.addPoint(10, -20); AffineTransform transform = new AffineTransform(); transform.concatenate(AffineTransform.getTranslateInstance(px, py)); transform.concatenate(AffineTransform.getScaleInstance(0.5, 0.5)); transform.concatenate(AffineTransform.getRotateInstance(Math.atan2(dy, dx) - Math.PI * 0.5)); Shape shape = new GeneralPath(tip).createTransformedShape(transform); g2d.setPaint(preSynapticSiteColor.darker().darker()); g2d.draw(shape); g2d.setPaint(preSynapticSiteColor.brighter().brighter()); g2d.fill(shape); }
public void paint(Graphics2D g) { Font origFont = g.getFont(); g.setRenderingHint( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON); // 1) create scaled font Font font = origFont.deriveFont(AffineTransform.getScaleInstance(1.5, 3)); g.setFont(font); g.drawString("Scaled Font", 20, 40); // 2) create translated font font = origFont.deriveFont(AffineTransform.getTranslateInstance(50, 20)); g.setFont(font); g.drawString("Translated Font", 20, 80); g.drawLine(20, 80, 120, 80); // 3) create sheared font font = origFont.deriveFont(AffineTransform.getShearInstance(.5, .5)); g.setFont(font); g.drawString("Sheared Font", 20, 120); // 4) create rotated font font = origFont.deriveFont(AffineTransform.getRotateInstance(Math.PI / 4)); g.setFont(font); g.drawString("Rotated Font", 220, 120); }
protected void makeSheet() { if (false) { // TODO deal with sheets // sprite sheet is East 0, clockwise // direction sheet is North 0, clockwise /*int sheetIndex = (dir.ordinal() - Direction.EAST.ordinal() + 8) % 8; int soldierHeight = image.getHeight(); if (!isAttacking()) { sheetIndex += 8; } image = image.getSubimage(sheetIndex * soldierHeight, 0, soldierHeight, soldierHeight); */ } else { sprites = new BufferedImage[8]; sprites[0] = image; for (int i = 1; i < 8; i++) { sprites[i] = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); double rotationRequired = Math.toRadians(i * 45); double locationX = image.getWidth() / 2; double locationY = image.getHeight() / 2; AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); // Drawing the rotated image at the required drawing locations sprites[i].createGraphics().drawImage(op.filter(image, null), 0, 0, null); } } }
public static BufferedImage rotateImage(BufferedImage image, double theta) { int degrees = (int) Math.abs(Math.toDegrees(theta)); double xCenter = image.getWidth() / 2; double yCenter = image.getHeight() / 2; AffineTransform rotateTransform = AffineTransform.getRotateInstance(-theta, xCenter, yCenter); // Translation adjustments so image still centered after rotate width/height changes if (image.getHeight() != image.getWidth() && degrees != 180 && degrees != 0) { Point2D origin = new Point2D.Double(0.0, 0.0); origin = rotateTransform.transform(origin, null); double yTranslate = origin.getY(); Point2D yMax = new Point2D.Double(0, image.getHeight()); yMax = rotateTransform.transform(yMax, null); double xTranslate = yMax.getX(); AffineTransform translationAdjustment = AffineTransform.getTranslateInstance(-xTranslate, -yTranslate); rotateTransform.preConcatenate(translationAdjustment); } AffineTransformOp op = new AffineTransformOp(rotateTransform, AffineTransformOp.TYPE_BILINEAR); // Have to recopy image because of JDK bug #4723021, AffineTransformationOp throwing exception // sometimes image = copyImage(image, BufferedImage.TYPE_INT_ARGB); // Need to create filter dest image ourselves since AffineTransformOp's own dest image creation // throws exceptions in some cases. Rectangle bounds = op.getBounds2D(image).getBounds(); BufferedImage finalImage = new BufferedImage( (int) bounds.getWidth(), (int) bounds.getHeight(), BufferedImage.TYPE_INT_ARGB); return op.filter(image, finalImage); }
@Test public void drawWithClipAndTransform() throws Exception { graphics.setPaint(Color.BLACK); graphics.fill(imageBounds); AffineTransform tr = AffineTransform.getRotateInstance( Math.PI / 4, image.getMinX() + image.getWidth() / 2, image.getMinY() + image.getHeight() / 2); graphics.transform(tr); graphics.clip(midRect); graphics.setPaint(Color.RED); graphics.fill(imageBounds); showImage("drawWithClipAndTransform"); // Outside transformed clip region Rectangle outer = new Rectangle(midRect); outer.grow(5, 5); Point2D[] corners = getCorners(outer); Point2D[] trPoints = new Point2D[corners.length]; tr.transform(corners, 0, trPoints, 0, corners.length); assertColor(Color.BLACK, trPoints); // Inside transformed clip region Rectangle inner = new Rectangle(midRect); inner.grow(-5, -5); corners = getCorners(inner); tr.transform(corners, 0, trPoints, 0, corners.length); assertColor(Color.RED, trPoints); }
/** @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double) */ public void set(double x, double y) { double dx = x - getPoint().getX(); double dy = y - getPoint().getY(); Point2D center = new Point2D.Double((init.getX() + end.getX()) / 2, (init.getY() + end.getY()) / 2); // Point2D[] // p1=TrigonometricalFunctions.getPerpendicular(init,end,center); // Point2D[] // p2=TrigonometricalFunctions.getPerpendicular(p1[0],p1[1],new // Point2D.Double(x,y)); // Point2D // pl=TrigonometricalFunctions.getIntersection(p2[0],p2[1],p1[0],p1[1]); // double xdist=2*pl.distance(x,y); double xdist = 2 * center.distance(x, y); end = UtilFunctions.getPoint(center, end, center.distance(x, y)); // end=new Point2D.Double(end.getX()+dx,end.getY()+dy); init = UtilFunctions.getPoint(end, center, xdist); Arc2D.Double arc = new Arc2D.Double(init.getX(), init.getY() - ydist, xdist, 2 * ydist, 0, 360, Arc2D.OPEN); Point2D rotationPoint = new Point2D.Double(init.getX() + xdist / 2, init.getY()); double angle = UtilFunctions.getAngle(init, end); AffineTransform mT = AffineTransform.getRotateInstance(angle, init.getX(), init.getY()); gp = new GeneralPathX(arc); gp.transform(mT); }
/** * Builds the circular shape and returns the result as an array of <code>Area</code>. Each <code> * Area</code> is one of the bars composing the shape. */ private Area[] buildTicker() { width = this.getPreferredSize().getWidth(); height = this.getPreferredSize().getHeight(); Area[] ticker = new Area[barsCount]; Point2D.Double center = new Point2D.Double((double) width / 2, (double) height / 2); double fixedAngle = 2.0 * Math.PI / ((double) barsCount); for (double i = 0.0; i < (double) barsCount; i++) { Area primitive = buildPrimitive(); AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(), center.getY()); AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0 / 8, -6.0 / 8); AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAngle, center.getX(), center.getY()); AffineTransform toWheel = new AffineTransform(); toWheel.concatenate(toCenter); toWheel.concatenate(toBorder); primitive.transform(toWheel); primitive.transform(toCircle); ticker[(int) i] = primitive; } return ticker; }
public void render(Vehicle vehicle) { Vector adjustedPosition = vehicle.position().add(viewPoint); int x = (int) adjustedPosition.x(); int y = (int) adjustedPosition.y(); Vector tip = new Vector(x + vehicle.boundingRadius(), y); Vector left = new Vector(x - 5, y - 5); Vector right = new Vector(x - 5, y + 5); int[] xPos = {(int) tip.x(), (int) left.x(), (int) right.x()}; int[] yPos = {(int) tip.y(), (int) left.y(), (int) right.y()}; AffineTransform orig = graphics.getTransform(); AffineTransform rot = AffineTransform.getRotateInstance(vehicle.heading().x(), vehicle.heading().y(), x, y); graphics.transform(rot); graphics.drawPolygon(xPos, yPos, 3); graphics.setTransform(orig); if (showFeelers) { Vector[] feelers = createFeelersFor(vehicle); for (Vector feeler : feelers) { graphics.drawLine(x, y, (int) feeler.x(), (int) feeler.y()); } } renderHealthBar(vehicle, x, y); renderEnergyBar(vehicle, x, y); }
private void drawSliders(Graphics2D g2d) { g2d.translate(sliderBaseLineRect.x, sliderBaseLineRect.y); g2d.setStroke(STROKE_1); for (int i = 0; i < getSliderCount(); i++) { if (isSliderVisible(i)) { double sliderPos = getRelativeSliderPos(getSliderSample(i)); g2d.translate(sliderPos, 0.0); final Color sliderColor = getSliderColor(i); g2d.setPaint(sliderColor); g2d.fill(sliderShape); int gray = (sliderColor.getRed() + sliderColor.getGreen() + sliderColor.getBlue()) / 3; g2d.setColor(gray < 128 ? Color.white : Color.black); g2d.draw(sliderShape); String text = getFormattedValue(getSliderSample(i)); g2d.setColor(Color.black); // save the old transformation final AffineTransform oldTransform = g2d.getTransform(); g2d.transform(AffineTransform.getRotateInstance(Math.PI / 2)); g2d.drawString(text, 3 + 0.5f * SLIDER_HEIGHT, 0.35f * FONT_SIZE); // restore the old transformation g2d.setTransform(oldTransform); g2d.translate(-sliderPos, 0.0); } } g2d.translate(-sliderBaseLineRect.x, -sliderBaseLineRect.y); }
/** * Test that point features are rendered at the expected image coordinates when the map is * rotated. StreamingRenderer * * @throws Exception */ @Test public void testRotatedTransform() throws Exception { // If we rotate the world rectangle + 90 degrees around (0,0), we get the screen rectangle final Rectangle screen = new Rectangle(0, 0, 100, 50); final Envelope world = new Envelope(0, 50, 0, -100); final AffineTransform worldToScreen = AffineTransform.getRotateInstance(Math.toRadians(90), 0, 0); DefaultFeatureCollection fc = new DefaultFeatureCollection(); fc.add(createPoint(0, 0)); fc.add(createPoint(world.getMaxX(), world.getMinY())); MapContext mapContext = new DefaultMapContext(DefaultGeographicCRS.WGS84); mapContext.addLayer((FeatureCollection) fc, createPointStyle()); BufferedImage image = new BufferedImage(screen.width, screen.height, BufferedImage.TYPE_4BYTE_ABGR); final StreamingRenderer sr = new StreamingRenderer(); sr.setContext(mapContext); sr.paint(image.createGraphics(), screen, worldToScreen); assertTrue("Pixel should be drawn at 0,0 ", image.getRGB(0, 0) != 0); assertTrue( "Pixel should not be drawn in image centre ", image.getRGB(screen.width / 2, screen.height / 2) == 0); assertTrue( "Pixel should be drawn at image max corner ", image.getRGB(screen.width - 1, screen.height - 1) != 0); }
private void paintString( Graphics g, int x, int y, int width, int height, int fillStart, int amountFull, Insets b) { if (!(g instanceof Graphics2D)) { return; } Graphics2D g2D = (Graphics2D) g; String progressString = progressBar.getString(); g2D.setFont(progressBar.getFont()); Point renderLocation = getStringPlacement(g2D, progressString, x, y, width, height); Rectangle savedClip = g2D.getClipBounds(); if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { g2D.setColor(getSelectionBackground()); Utilities.drawString(progressBar, g2D, progressString, renderLocation.x, renderLocation.y); g2D.setColor(getSelectionForeground()); g2D.clipRect(fillStart, y, amountFull, height); Utilities.drawString(progressBar, g2D, progressString, renderLocation.x, renderLocation.y); } else { // VERTICAL g2D.setColor(getSelectionBackground()); AffineTransform rotate = AffineTransform.getRotateInstance(Math.PI / 2); g2D.setFont(progressBar.getFont().deriveFont(rotate)); renderLocation = getStringPlacement(g2D, progressString, x, y, width, height); Utilities.drawString(progressBar, g2D, progressString, renderLocation.x, renderLocation.y); g2D.setColor(getSelectionForeground()); g2D.clipRect(x, fillStart, width, amountFull); Utilities.drawString(progressBar, g2D, progressString, renderLocation.x, renderLocation.y); } g2D.setClip(savedClip); }
public BufferedImage getRotate() { AffineTransformOp atop = new AffineTransformOp( AffineTransform.getRotateInstance( 3.141592653589793D, this.image.getWidth() / 2, this.image.getHeight() / 2), 1); return this.image = atop.filter(this.image, null); }
/* Make a sample sprite for testing purposes. */ private static Sprite makeSprite() { Sprite body = new RectangleSprite(250, 150); Sprite boom = new BoomSprite(235, 50); Sprite arm = new ArmSprite(180, 30); Sprite bucket = new BucketSprite(90, 50); body.transform(AffineTransform.getTranslateInstance(150, 400)); // body boom.transform(AffineTransform.getTranslateInstance(215, 30)); boom.transform(AffineTransform.getRotateInstance(Math.PI / 180 * -30)); arm.transform(AffineTransform.getTranslateInstance(235, 0)); arm.transform(AffineTransform.getRotateInstance(Math.PI / 180 * 90)); bucket.transform(AffineTransform.getTranslateInstance(180, 0)); body.addChild(boom); boom.addChild(arm); arm.addChild(bucket); return body; }
/** * Draws a shape with the specified rotation about <code>(x, y)</code>. * * @param g2 the graphics device (<code>null</code> not permitted). * @param shape the shape (<code>null</code> not permitted). * @param angle the angle (in radians). * @param x the x coordinate for the rotation point. * @param y the y coordinate for the rotation point. */ public static void drawRotatedShape( final Graphics2D g2, final Shape shape, final double angle, final float x, final float y) { final AffineTransform saved = g2.getTransform(); final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y); g2.transform(rotate); g2.draw(shape); g2.setTransform(saved); }
/** * Rotates a shape about the specified coordinates. * * @param base the shape (<code>null</code> permitted, returns <code>null</code>). * @param angle the angle (in radians). * @param x the x coordinate for the rotation point (in Java2D space). * @param y the y coordinate for the rotation point (in Java2D space). * @return the rotated shape. */ public static Shape rotateShape( final Shape base, final double angle, final float x, final float y) { if (base == null) { return null; } final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y); final Shape result = rotate.createTransformedShape(base); return result; }
// Function to rotate spaceship using direction (left/right) public void rotate(int direction) { rotateAngle += direction * ANGLE; rotateAngle = rotateAngle % 360; double locationX = imageWidth / 2; double locationY = imageHeight / 2; this.angle = Math.toRadians(rotateAngle); tx.rotate(angle); tx = AffineTransform.getRotateInstance(angle, locationX, locationY); op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); }
/** * @param shape * @param rotation * @return */ public static Shape getRotatedShape(Shape shape, double rotation) { AffineTransform localAT = null; Shape localShape = null; localAT = AffineTransform.getRotateInstance( Math.toRadians(rotation), shape.getBounds().getX(), shape.getBounds().getY()); localShape = localAT.createTransformedShape(shape); return localShape; }
@Override public UndoRedoAction rotate( SVGHandle handle, Set<Element> elementsSet, Point2D centerPoint, double angle) { // getting the rotation affine transform AffineTransform actionTransform = AffineTransform.getRotateInstance(angle, centerPoint.getX(), centerPoint.getY()); return applyTransform( handle, elementsSet.iterator().next(), actionTransform, rotateUndoRedoLabel); }
/** * returns the rotation transform * * @param svgHandle a svg handle * @param bounds the bounds of the area to be rotated * @param firstPoint the first clicked point by the user * @param currentPoint the current point of the drag action by the user * @return the rotation transform */ protected AffineTransform getRotationTransform( SVGHandle svgHandle, Rectangle2D bounds, Point2D firstPoint, Point2D currentPoint) { // getting the center point Point2D centerPoint = getRotationSkewCenterPoint(svgHandle, bounds); // computing the angle for the rotation double angle = ShapeToolkit.getRotationAngle(centerPoint, firstPoint, currentPoint); // returning the rotation affine transform return AffineTransform.getRotateInstance(angle, centerPoint.getX(), centerPoint.getY()); }
public void run() { Point2D.Double center = new Point2D.Double((double) width / 2, (double) height / 2); double fixedIncrement = 2.0 * Math.PI / ((double) barsCount); AffineTransform toCircle = AffineTransform.getRotateInstance(fixedIncrement, center.getX(), center.getY()); long start = System.currentTimeMillis(); if (rampDelay == 0) alphaLevel = rampUp ? 255 : 0; started = true; boolean inRamp = rampUp; while (!Thread.interrupted()) { if (!inRamp) { for (int i = 0; i < ticker.length; i++) ticker[i].transform(toCircle); } repaint(); if (rampUp) { if (alphaLevel < 255) { alphaLevel = (int) (255 * (System.currentTimeMillis() - start) / rampDelay); if (alphaLevel >= 255) { alphaLevel = 255; inRamp = false; } } } else if (alphaLevel > 0) { alphaLevel = (int) (255 - (255 * (System.currentTimeMillis() - start) / rampDelay)); if (alphaLevel <= 0) { alphaLevel = 0; break; } } try { Thread.sleep(inRamp ? 10 : (int) (1000 / fps)); } catch (InterruptedException ie) { break; } Thread.yield(); } if (!rampUp) { started = false; repaint(); setVisible(false); removeMouseListener(InfiniteProgressPanel.this); } }
public void fill(Graphics2D g2) { // convert ellipse to awt shape java.awt.geom.Ellipse2D.Double ellipse = new java.awt.geom.Ellipse2D.Double(xc - r, yc - r, 2 * r, 2 * r); // need to rotate by angle theta java.awt.geom.AffineTransform trans = java.awt.geom.AffineTransform.getRotateInstance(theta, xc, yc); Shape shape = trans.createTransformedShape(ellipse); // draw the awt ellipse g2.fill(shape); }
@Test public void clipWithExistingTransform() throws Exception { AffineTransform tr = AffineTransform.getRotateInstance(Math.PI / 4); graphics.transform(tr); graphics.clip(midRect); /* * getClip should return the untransformed region - however we * allow for some slope because DiskMemImageGraphics is actually * returning the inverse-transformed version of its internal user * clip (transformed) shape. */ assertBounds(midRect, graphics.getClipBounds(), 2); }
/** * Create an arrow with the given x, y, arrow length and arrow width values. * * @param x The x extent. * @param y The y extent. * @param arrowLength Length of the arrow head. * @param arrowWidth Width of the arrow head. * @return An arrow with the given x, y, arrow length and arrow width values. */ private static Shape _createArrow(double x, double y, double arrowLength, double arrowWidth) { double halfWidth = arrowWidth / 2.0; Polygon2D polygon = new Polygon2D.Double(); polygon.moveTo(0.0, halfWidth); polygon.lineTo(arrowLength + 3.0, 0.0); polygon.lineTo(arrowLength, halfWidth); polygon.lineTo(Math.sqrt(x * x + y * y), halfWidth); polygon.lineTo(arrowLength, halfWidth); polygon.lineTo(arrowLength + 3.0, arrowWidth); polygon.closePath(); AffineTransform transform = AffineTransform.getRotateInstance(Math.atan2(y, x)); polygon.transform(transform); return polygon; }
private Path2D.Double makeStar(int r1, int r2, int vc) { int or = Math.max(r1, r2); int ir = Math.min(r1, r2); double agl = 0d; double add = 2 * Math.PI / (vc * 2); Path2D.Double p = new Path2D.Double(); p.moveTo(or * 1, or * 0); for (int i = 0; i < vc * 2 - 1; i++) { agl += add; int r = i % 2 == 0 ? ir : or; p.lineTo(r * Math.cos(agl), r * Math.sin(agl)); } p.closePath(); AffineTransform at = AffineTransform.getRotateInstance(-Math.PI / 2, or, 0); return new Path2D.Double(p, at); }
private void paintTurtle(Graphics2D g) { if (turtle.isHidden()) { return; } Image image = getImage(); int xCenter = image.getWidth(null) / 2; int yCenter = image.getHeight(null) / 2; int x = turtle.getX() - xCenter; int y = turtle.getY() - yCenter; AffineTransform rotate = AffineTransform.getRotateInstance( Math.toRadians(turtle.getHeadingInDegrees()), xCenter, yCenter); AffineTransform move = AffineTransform.getTranslateInstance(x, y); move.concatenate(rotate); g.drawImage(image, move, null); }
public void fly() { if (!isFlying()) { return; } setRotation(getRotation() % 360); if (remainingFuel <= 0) { setFlying(false); return; } remainingFuel -= fuelConsumptionRate; remainingFuel = (remainingFuel < 0) ? 0 : remainingFuel; float theRot = (float) Math.toRadians(getRotation()); float xVec, yVec; xVec = (float) Math.sin(theRot) * 10000; yVec = (float) Math.cos(theRot) * (-800); if (getRotation() <= 180 || getRotation() >= 270) { yVec = (yVec > -600) ? -600 : yVec; } else { yVec -= 200; } this.applyForce(xVec, yVec - 200); sprayImage = sprayImage.getFlippedCopy(true, false); theRot = (float) Math.toRadians(getRotation()); sprayImage.setRotation(theRot); float oldX = getVisualX(); float oldY = getVisualY(); float newX; float newY; try { AffineTransform transformer = AffineTransform.getRotateInstance(theRot, oldX, oldY); Point2D before = new Point2D.Double(oldX, oldY + 60); Point2D after = new Point2D.Double(); after = transformer.transform(before, after); newX = (float) after.getX(); newY = (float) after.getY(); sprayX = newX; sprayY = newY; } catch (Exception e) { System.out.println(e); } }
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) { Graphics2D g = (Graphics2D) g1.create(); double dx = x2 - x1, dy = y2 - y1; double angle = Math.atan2(dy, dx); int len = (int) Math.sqrt(dx * dx + dy * dy); AffineTransform at = AffineTransform.getTranslateInstance(x1, y1); at.concatenate(AffineTransform.getRotateInstance(angle)); g.transform(at); // Draw horizontal arrow starting in (0, 0) g.drawLine(0, 0, len, 0); g.fillPolygon( new int[] {len, len - ARR_SIZE, len - ARR_SIZE, len}, new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4); }