Advertisement
lawrencealan

Sub-pixel drawImage precision in Java

Jan 5th, 2012
2,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. /*
  2.  * SubpixelView.java
  3.  */
  4. package subpixel;
  5.  
  6. import com.sun.net.ssl.internal.ssl.Debug;
  7. import java.awt.Color;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.RenderingHints;
  11. import org.jdesktop.application.SingleFrameApplication;
  12. import org.jdesktop.application.FrameView;
  13. import java.awt.geom.AffineTransform;
  14. import java.awt.geom.Point2D;
  15. import java.awt.image.BufferedImage;
  16. import java.io.IOException;
  17. import java.net.URL;
  18. import javax.imageio.ImageIO;
  19. import javax.swing.JComponent;
  20.  
  21. /**
  22.  * The application's main frame.
  23.  */
  24. public class SubpixelView extends FrameView {
  25.  
  26.     public SubpixelView(SingleFrameApplication app) {
  27.         super(app);
  28.  
  29.         initComponents();
  30.        
  31.         /* create / add our component */
  32.         SubPixelCanvas d = new SubPixelCanvas(0, 0, 200, 200);
  33.         mainPanel.add(d);
  34.  
  35.         /* get ball image */
  36.         try {
  37.             d.image = ImageIO.read(new URL("http://icons.iconseeker.com/png/48/scrap/aqua-ball.png"));
  38.         } catch (IOException ex) {  /* */ }
  39.  
  40.         /* start the threaded repaint */
  41.         d.start();
  42.  
  43.     }
  44.  
  45.     class SubPixelCanvas extends JComponent implements Runnable {
  46.  
  47.         private Point2D.Float ball = new Point2D.Float(0, 0);
  48.         private double width, height;
  49.         private BufferedImage image, scene;
  50.         public Graphics2D g2d;
  51.  
  52.         private SubPixelCanvas(int x, int y, int w, int h) {
  53.             width = w;
  54.             height = h;
  55.            
  56.             /* setup the memory image, which supports sub-pixel (double) precision */
  57.             scene = new BufferedImage(w, h, 1);
  58.             g2d = scene.createGraphics();
  59.             g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  60.             g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  61.             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  62.            
  63.             /* setup the ui component */
  64.             this.setLocation(x, y);
  65.             this.setSize(w, h);
  66.         }
  67.  
  68.         /* overrides the paint method */
  69.         @Override
  70.         public void paint(Graphics g) {
  71.             /* clear scene buffer */
  72.             g2d.clearRect(0, 0, (int)width, (int)height);
  73.            
  74.             /* draw ball image to the memory image with transformed x/y double values */
  75.             AffineTransform t = new AffineTransform();
  76.             t.translate(ball.x, ball.y); // x/y set here
  77.             t.scale(1, 1); // scale = 1
  78.             g2d.drawImage(image, t, null);
  79.            
  80.             // draw the scene (double percision image) to the ui component
  81.             g.drawImage(scene, 0, 0, this);
  82.         }
  83.  
  84.         /* thread run function, all animation is done here */
  85.         public void run() {
  86.             double i = 0;
  87.             int speed = 5;
  88.             double maxX = width-image.getWidth();
  89.             double maxY = height-image.getHeight();
  90.             try {
  91.                 while(true) {
  92.                 while (i < maxX && i<maxY) {
  93.                     ball.setLocation(i, i);
  94.                     mainPanel.repaint();
  95.                     i += 0.33;
  96.                     Thread.sleep(speed);
  97.                 }
  98.                 while ((i -= 0.33) >0) {
  99.                     ball.setLocation(i, i);
  100.                     mainPanel.repaint();
  101.                     Thread.sleep(speed);
  102.                 }
  103.                 }
  104.             } catch (Exception ex) {
  105.                 Debug.println("...", ex.toString());
  106.             }
  107.  
  108.         }
  109.  
  110.         private void start() {
  111.             Thread t = new Thread(this);
  112.             t.start();
  113.         }
  114.     }
  115.  
  116.     /** This method is called from within the constructor to
  117.      * initialize the form.
  118.      * WARNING: Do NOT modify this code. The content of this method is
  119.      * always regenerated by the Form Editor.
  120.      */
  121.     @SuppressWarnings("unchecked")
  122.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  123.     private void initComponents() {
  124.  
  125.         mainPanel = new javax.swing.JPanel();
  126.  
  127.         mainPanel.setName("mainPanel"); // NOI18N
  128.         mainPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
  129.             public void componentShown(java.awt.event.ComponentEvent evt) {
  130.                 mainPanelComponentShown(evt);
  131.             }
  132.         });
  133.  
  134.         javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
  135.         mainPanel.setLayout(mainPanelLayout);
  136.         mainPanelLayout.setHorizontalGroup(
  137.             mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138.             .addGap(0, 400, Short.MAX_VALUE)
  139.         );
  140.         mainPanelLayout.setVerticalGroup(
  141.             mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  142.             .addGap(0, 300, Short.MAX_VALUE)
  143.         );
  144.  
  145.         setComponent(mainPanel);
  146.     }// </editor-fold>
  147.  
  148. private void mainPanelComponentShown(java.awt.event.ComponentEvent evt) {
  149. }
  150.     // Variables declaration - do not modify
  151.     private javax.swing.JPanel mainPanel;
  152.     // End of variables declaration
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement