Title: More Composition Examples
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

More Composition Examples

This example is similar to a previous example. The big difference is that instead of drawing directly into the Graphics2D screen space, we create a BufferedImage that we draw into. Once we have the bufferedImage, we then draw it onto the Graphics2D screen 4 times.

FadeOut2.java



import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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



public class FadeOut2 extends JFrame implements ActionListener{

    private Image imageSun, imageCloud;
    private float alpha = 1f;
    Timer timer;

    FadeOut2()
    {
        loadImages();
        setSize(500, 350);
        setTitle("FadeOut2");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        timer = new Timer(80, this);
        timer.setInitialDelay(500);
        timer.start();
    }

    private void loadImages() {
        try {

            imageSun = ImageIO.read(new File("sun.png"));
            imageCloud = ImageIO.read(new File("cloud.png"));

        } catch (IOException ex) {

            System.out.println("Error reading in images: +ex");
        }
    }
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();

        BufferedImage buffImg = new BufferedImage(220, 140,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D gbi = buffImg.createGraphics();

        AlphaComposite ac = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha);

        gbi.drawImage(imageCloud, 0, 0, null);
        gbi.setComposite(ac);
        gbi.drawImage(imageSun, 40, 30, null);

        g2d.drawImage(buffImg, 20, 20, null);
        g2d.drawImage(buffImg, 20, 175, null);
        g2d.drawImage(buffImg, 250, 20, null);
        g2d.drawImage(buffImg, 250, 175, null);

        gbi.dispose();
        g2d.dispose();

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        alpha += -0.01f;

        if (alpha <= 0)
        {
            alpha = 0;
            timer.stop();
        }
        repaint();
    }

    public static void main(String[] args)
    {
        FadeOut2 fo = new FadeOut2();
    }


}








SpotLight

The follow steps occur on this one:

1. Create a bufferedImage to draw into
2. Draw a white circle onto the bufferedImage at the location of your mouse
3. Draw our penguins.png image onto bufferedImage with the SRC_ATOP rule. This only draws in areas that have already been drawn on (i.e. the white circle area).
4. Draw the penguins.png again with a weak transparency value (0.1f) using SRC_OVER to show a faint image.
5. Then draw the created bufferedImage to the screen Graphics2D surface

Note that the penguins.png image is drawn twice. By commenting out one of them you can see what the other one does by itself.

SpotLight.java

penguins.png


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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

public class SpotLight extends JFrame{

    private final int RADIUS = 35;
    private Image image;
    private int iw;
    private int ih;
    private int x;
    private int y;
    private boolean mouseIn;

    SpotLight()
    {
        loadImage();
        setSize(500, 320);
        setTitle("SpotLight");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        iw = image.getWidth(null);
        ih = image.getHeight(null);

        MyMouseAdapter mma = new MyMouseAdapter();

        addMouseMotionListener(mma);
        addMouseListener(mma);
        setVisible(true);
    }
    private void loadImage() {
        try {

            image = ImageIO.read(new File("penguins.png"));

        } catch (IOException ex) {

            System.out.println("Error reading in images: +ex");
        }
    }
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();
        Dimension d = getSize();
        g2d.setColor(new Color(230,230,230));
        g2d.fillRect(0, 0, d.width, d.height);

        int midX = (d.width - iw) / 2;
        int midY = (d.height - ih) / 2;

        BufferedImage bufferedImage = new BufferedImage(d.width,
                d.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d_bufferedImage= bufferedImage.createGraphics();

        if (mouseIn) {
            g2d_bufferedImage.setPaint(Color.white);
            g2d_bufferedImage.fillOval(x - RADIUS, y - RADIUS, RADIUS * 2,
                    RADIUS * 2);
            // Because of the way SrcAtop works, the image will only be drawing
            // where we have drawn our ellipse background
            g2d_bufferedImage.setComposite(AlphaComposite.SrcAtop);
            g2d_bufferedImage.drawImage(image, midX, midY, iw, ih, this);
        }
        // Now we draw over the whole Image the same image but with a very transparent alpha (0.1)
        g2d_bufferedImage.setComposite(AlphaComposite.SrcOver.derive(0.1f));
        g2d_bufferedImage.drawImage(image, midX, midY, iw, ih, this);
        g2d_bufferedImage.dispose();

        g2d.drawImage(bufferedImage, 0, 0, d.width, d.height, this);
        System.out.println("paint");
        g2d.dispose();
    }

    private class MyMouseAdapter extends MouseAdapter {

        @Override
        public void mouseExited(MouseEvent e) {
            mouseIn = false;
            repaint();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            mouseIn = true;
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {

            x = e.getX();
            y = e.getY();

            repaint();
        }
    }

    public static void main(String[] args)
    {
        SpotLight sl = new SpotLight();
    }

}



Waiting.java

Cute example with a bit of review of previous concepts: translate, rotate, composite


import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Waiting extends JFrame implements ActionListener{


    Timer timer;
    private final int NUMBER_OF_LINES = 8;
    private final int STROKE_WIDTH = 3;
    private int count=0;

    // A 2 dimensional table of transparency alpha values.
    // A column represents the alpha value used for one particular line
    // A row represents the alpha used for all of the lines drawn at a given time
    // The row used will cycle from top to bottom and then repeat

    private final double[][] trs = {
        {0.0, 0.15, 0.30, 0.5, 0.65, 0.80, 0.9, 1.0},
        {1.0, 0.0, 0.15, 0.30, 0.5, 0.65, 0.8, 0.9},
        {0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65, 0.8},
        {0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65},
        {0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5},
        {0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3},
        {0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15},
        {0.15, 0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0}
    };

    Waiting()
    {
        setSize(400, 200);
        setTitle("Waiting");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        timer = new Timer(80, this);
        timer.setInitialDelay(200);
        timer.start();
    }

    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        int width = getWidth();
        int height = getHeight();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.black);

        g2d.setStroke(new BasicStroke(STROKE_WIDTH, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND));
        g2d.translate(width / 2, height / 2);

        for (int i = 0; i < NUMBER_OF_LINES; i++) {

            float alpha = (float) trs[count % NUMBER_OF_LINES][i];
            AlphaComposite acomp = AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, alpha);
            g2d.setComposite(acomp);

            g2d.rotate(Math.PI / 4f);
           //Note I can draw into negative region now because of the Translation
            g2d.drawLine(0, -10, 0, -40);
        }

        g2d.dispose();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        count +=1;
        repaint();
    }

    public static void main(String[] args)
    {
        Waiting w = new Waiting();
    }


}



Last Updated: June 20, 2015 11:30 PM