Java Image texturePaintHorizontal(JComponent parent, Graphics g, Image image, Rectangle areaToPaint)

Here you can find the source of texturePaintHorizontal(JComponent parent, Graphics g, Image image, Rectangle areaToPaint)

Description

Paint a single texture horizontally across the screen.

License

Open Source License

Parameter

Parameter Description
g a parameter
image a parameter
areaToPaint a parameter

Declaration

public static void texturePaintHorizontal(JComponent parent, Graphics g, Image image, Rectangle areaToPaint) 

Method Source Code


//package com.java2s;
/* //from  w  w w.ja v  a 2 s. c om
 *  LEGAL STUFF
 * 
 *  This file is part of Combo Cards.
 *  
 *  Combo Cards is Copyright 2008-2010 Artless Entertainment
 *  
 *  Set? is a registered trademark of Set Enterprises. 
 *  
 *  This project is in no way affiliated with Set Enterprises, 
 *  but the authors of Combo Cards are very grateful for
 *  them creating such an excellent card game.
 *  
 *  Combo Cards is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *   
 *  Combo Cards is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with Combo Cards.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;

public class Main {
    /**
     * Paint a single texture horizontally across the screen.
     *
     * @param g
     * @param image
     * @param areaToPaint
     */
    public static void texturePaintHorizontal(JComponent parent, Graphics g, Image image, Rectangle areaToPaint) {
        int imageWidth = image.getWidth(parent);
        int imageHeight = image.getHeight(parent);

        int cols = areaToPaint.width / imageWidth;

        for (int x = 0; x < cols; x++) {
            g.drawImage(image, x * imageWidth + areaToPaint.x, areaToPaint.y, parent);
        }

        //Draw the last partial image in.
        int remainingWidth = areaToPaint.width - cols * imageWidth;

        if (remainingWidth > 0) {
            BufferedImage temp = new BufferedImage(remainingWidth, imageHeight, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics2D g2 = temp.createGraphics();
            g2.drawImage(image, 0, 0, parent);
            temp.flush();

            g.drawImage(temp, areaToPaint.width + areaToPaint.x - remainingWidth, areaToPaint.y, parent);
        }
    }
}

Related

  1. showImage(final String title, final Image image)
  2. showImage(Image image)
  3. showImage(Image image, String title)
  4. sizeToImage(JComponent component, Image image)
  5. swapColor(Image image, Color imageColor, Color newColor)
  6. usesAlpha(Image image)
  7. verifyImageSizeBigger(byte[] imageData, int maxDim)