title: How do I rotate and draw a BufferedImage in Java?
body: <p>I'm trying to develop a small rendering thing for a small project. I've gotten to a place where I can setup a Graphics2D object and call things from a simple render() loop, but when attempting to draw an image in place the image crops itself awkwardly and begins to lose data.</p>
  <p>This is what I currently have in my draw function. Renderable is a pre-loaded BufferedImage.</p>
  <pre><code>    public void draw(Graphics2D g2d, int x, int y, int scale, int rotation) {
          scale = scale / 2;
          
          AffineTransform at = new AffineTransform();
          at.rotate(Math.toRadians(rotation), renderable.getWidth() / 2, renderable.getHeight() / 2);
          AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
          
          BufferedImage copy = op.filter(renderable, null);
                  
          int pX = (400 - ((copy.getWidth() * scale) / 2)) + x;
          int pY = (300 - ((copy.getHeight() * scale) / 2)) + y;      
          
          g2d.drawImage(copy, pX, pY, copy.getWidth() * scale, copy.getHeight() * scale, null);
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/55Mzi.png" rel="nofollow noreferrer">When the rotation is not divisible by 90</a></p>
  <p><a href="https://i.stack.imgur.com/HkM6x.png" rel="nofollow noreferrer">When the rotation is divisible by 0, 90, 180, or 270</a></p>
  <p>Is there something I am missing or should be doing differently? It appears to me that the image is losing data. The Graphics2D object passed into the function is the same one used to render the lines behind the checkered square and should cover the full Canvas, which is 800x600 pixels.</p>
  <p><strong>Update</strong></p>
  <pre><code>        int rotation = 0;
          
          while (EngineGlue.isValid()) {
              rotation = rotation + 15;
              if (rotation &gt;= 360) {
                  rotation = 0;
              }
              
              Graphics2D g2d = (Graphics2D)EngineGlue.getInstance().getCanvas().getBufferStrategy().getDrawGraphics();
              g2d.clearRect(0, 0, 800, 600);
              
              g2d.drawLine(0, 300, 800, 300);
              g2d.drawLine(400, 0, 400, 600);
                          
              //ta1.draw(g2d, -100, 0, 2, rotation);
              //ta2.draw(g2d, 100, 0, 3, -rotation);
              ta3.draw(g2d, 0, 0, 1, rotation);
              
              EngineGlue.getInstance().getCanvas().getBufferStrategy().show();
              Thread.sleep(150);
          }
  </code></pre>
  <pre><code>            frame = new JFrame(title);
              canvas = new Canvas();
              
              frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
              frame.setResizable(false);
              frame.setVisible(true);
              frame.add(canvas);
              frame.pack();
              frame.setSize(EngineGlue.Dimension.WIDTH.getValue(), EngineGlue.Dimension.HEIGHT.getValue());
              frame.setLocationRelativeTo(null);
              
              canvas.setSize(EngineGlue.Dimension.WIDTH.getValue(), EngineGlue.Dimension.HEIGHT.getValue());
              canvas.createBufferStrategy(3);
  </code></pre>