Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: davedes (posted 2012-08-21 22:48:37, viewed 239 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109  
//rotate example... http://www.java-gaming.org/topics/image-rotator-does-not-use-affinetransform/27149/view.html
   
   // shared arrays
   private final float[] BOUNDS = new float[4];
   private final float[] SEG_POINTS = new float[6];

   /**
    * Rotates the image and returns a new image that contains the rotated image
    * entirely.
    * 
    * @param image the image to use
    * @param radians the angle in radians to rotate
    * @return the new buffered image, of TYPE_4BYTE_ABGR using bilinear
    *         filtering and anti-aliased edges
    */
   public BufferedImage getRotatedImage(BufferedImage image, float radians) {
      return getRotatedImage(image, radians,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
   }

   /**
    * Rotates the image and returns a new image that contains the rotated image
    * entirely.
    * 
    * @param image the image to use
    * @param radians the angle in radians to rotate
    * @param interpolationHint
    *            the hint for interpolation, e.g.
    *            RenderingHints.VALUE_INTERPOLATION_BILINEAR
    * @param smoothEdge whether to force an anti-aliased edge for the rotated image
    * @return the new buffered image, of TYPE_4BYTE_ABGR
    */
   public BufferedImage getRotatedImage(BufferedImage image, double radians,
         Object interpolationHint, boolean smoothEdge) {
      // this is the size we will assume even if we add padding...
      float width = image.getWidth();
      float height = image.getHeight();

      final int pad = smoothEdge ? 4 : 0;

      // HACK: Java2D does not smooth the edges of rotated images
      // Usually you need to use TexturePaint and fillRect to achieve this...
      // But in my experience using sub-images with TexturePaint can lead to
      // color bleeding.
      // This may be slower but leads to better results.
      if (pad != 0) {
         // we could probably speed this up with System.arraycopy or
         // something...
         BufferedImage padded = new BufferedImage((int)width+pad*2, (int)height+pad*2, BufferedImage.TYPE_4BYTE_ABGR);
         padded.getGraphics().drawImage(image, pad, pad, null);
         image = padded;
      }

      // rotate the image around its center (using the actual image size)
      AffineTransform transform = AffineTransform.getRotateInstance(radians,
            image.getWidth() / 2f, image.getHeight() / 2f);

      // first we determine the bounding box. we can use Rectangle2D to
      // simplify this for us
      Rectangle2D.Float rect = new Rectangle2D.Float(0, 0, width, height);

      // puts => { minX, minY, maxX, maxY }
      getBounds(rect, transform, BOUNDS);

      // now we build a new empty BufferedImage from our bounds
      int outWidth = (int) (BOUNDS[2] - BOUNDS[0]);
      int outHeight = (int) (BOUNDS[3] - BOUNDS[1]);
      BufferedImage out = new BufferedImage(outWidth, outHeight, BufferedImage.TYPE_4BYTE_ABGR);
      Graphics2D g2d = (Graphics2D) out.getGraphics();
      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolationHint);

      // center the image
      g2d.translate(outWidth/2f - width/2f - pad, outHeight/2f - height/2f - pad);

      // draw the rotated image
      g2d.transform(transform);
      g2d.drawImage(image, null, null);
      return out;
   }

   /**
    * Determines the min and max points of a transformed rectangle, which can
    * be used to determine the bounding box.
    * 
    * @param input
    *            the input rectangle (image bounds)
    * @param transform
    *            the transformation to apply to the rectangle input
    * @param output
    *            the output float to put the positions into
    */
   public void getBounds(Rectangle2D.Float input, AffineTransform transform, float[] output) {
      PathIterator i = input.getPathIterator(transform);
      output[0] = input.x;
      output[1] = input.y;
      output[2] = input.x + input.width;
      output[3] = input.y + input.height;
      while (!i.isDone()) {
         int ret = i.currentSegment(SEG_POINTS);
         // we'll assume the path is a standard rect...
         if (ret == PathIterator.SEG_CLOSE)
            break;
         output[0] = Math.min(output[0], SEG_POINTS[0]);
         output[1] = Math.min(output[1], SEG_POINTS[1]);
         output[2] = Math.max(output[2], SEG_POINTS[0]);
         output[3] = Math.max(output[3], SEG_POINTS[1]);
         i.next();
      }
   }





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!