Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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-04-27 03:39:47, viewed 150 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   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class Java2DTest extends JComponent {

   public static void main(String[] args) throws IOException {
      JFrame f = new JFrame("Test");
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      final Java2DTest t = new Java2DTest();
      f.add(t);
      
      f.setLocationRelativeTo(null);
      f.setSize(800, 600);
      f.setVisible(true);
   }
   
   TintImage font;
   Tint red = new Tint(Color.red);
   Tint blue = new Tint(Color.blue);
   Tint blah = new Tint(new Color(100, 20, 100), 0.6f);
   
   public Java2DTest() throws IOException {
      URL u = Thread.currentThread().getContextClassLoader().getResource("res/ptsans_00.png");
      BufferedImage img = ImageIO.read(u);
      font = new TintImage(img);
   }
   
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D)g;
      font.setTint(red);
      g2d.drawImage(font.getImage(), 100, 100, null);
      font.setTint(blue);
      g2d.drawImage(font.getImage(), 200, 200, null);
      font.setTint(blah); //tint once, render multiple times
      g2d.drawImage(font.getImage(), 300, 300, null); 
      g2d.drawImage(font.getImage(), 500, 500, null);
      font.resetTint();
      g2d.drawImage(font.getImage(), 400, 400, null);
   }
   
   /**
    * Premultiplied tint information for the given colour and mix value.
    * 
    * Inspired by Romain Guy's ColorTintFilter image op.
    * 
    * @author davedes
    */
   class Tint {
      int[] preRed, preGreen, preBlue;
      
      public Tint(Color mixColor) {
         this(mixColor, 1f);
      }
      
      /**
       * The RGB values to apply to the tint.
       * @param mixColor the RGB values to mix
       * @param mixValue the value between 0.0 and 1.0, i.e. amount to mix RGB
       */
      public Tint(Color mixColor, float mixValue) {
         int mix_r = (int) (mixColor.getRed()   * mixValue);
           int mix_g = (int) (mixColor.getGreen() * mixValue);
           int mix_b = (int) (mixColor.getBlue()  * mixValue);
           
           float factor = 1.0f - mixValue;
           preRed   = new int[256];
           preGreen = new int[256];
           preBlue  = new int[256];
           for (int i = 0; i < 256; i++) {
               int value = (int) (i * factor);
               preRed[i]   = value + mix_r;
               preGreen[i] = value + mix_g;
               preBlue[i]  = value + mix_b;
           }
      }
   }
   
   /**
    * A tintable image wrapper.
    * @author davedes
    */
   class TintImage {
      BufferedImage image;
      int[] original;
      int[] buf;
      
      public TintImage(BufferedImage image) {
         //convert it... 
         if (image.getType()!=BufferedImage.TYPE_INT_ARGB) {
            BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
            convertedImage.createGraphics().drawRenderedImage(image, null);
            image = convertedImage;
         }
         this.image = image;
         
         //get original pixels (e.g. white, for bitmap fonts)
         //and make a copy that we can modify freely
         buf = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
         original = new int[buf.length]; //copy the original data
         System.arraycopy(buf, 0, original, 0, buf.length);
      }
      
      /**
       * Returns the image used for rendering (which might be a copy of the one given at instantiation). 
       */
      public BufferedImage getImage() {
         return image;
      }
      
      /**
       * Resets the image to its original colours.
       */
      public void resetTint() {
         System.arraycopy(original, 0, buf, 0, buf.length);
      }
      
      /**
       * Tints the image by mixing the original colour (untinted) with the new 
       * tint value.
       *  
       * Fully transparent pixels will be ignored.
       * 
       * @param tint the tint to apply
       */
      void setTint(Tint tint) {
         for (int i = 0; i < buf.length; i++) {
               int argb = original[i];
               buf[i] = (argb & 0xFF000000) |
                        tint.preRed[(argb >> 16)   & 0xFF] << 16 |
                        tint.preGreen[(argb >> 8)  & 0xFF] <<  8 |
                        tint.preBlue[argb & 0xFF];
           }
      }
   }
}





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.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (67 views)
2014-11-03 18:54:52

moogie (82 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!