0% found this document useful (0 votes)
36 views

CONTOH Koding Program 2 Bahasa Java

This document contains Java code that demonstrates different types of alpha compositing. It creates a JPanel with a paintComponent method that draws text labels for the source and destination shapes. The drawComposite method loops through different AlphaComposite constants, creates a buffered image demonstrating each composite type by overlaying a red circle on a blue rectangle, and draws the images with labels to the graphics context. The main method creates a JFrame to display the JPanel.

Uploaded by

sayudi gpi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

CONTOH Koding Program 2 Bahasa Java

This document contains Java code that demonstrates different types of alpha compositing. It creates a JPanel with a paintComponent method that draws text labels for the source and destination shapes. The drawComposite method loops through different AlphaComposite constants, creates a buffered image demonstrating each composite type by overlaying a red circle on a blue rectangle, and draws the images with labels to the graphics context. The main method creates a JFrame to display the JPanel.

Uploaded by

sayudi gpi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CONTOH Koding program Java (2)

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package latihan;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.*;

import javax.swing.*;

/**

* @author 0000

*/

public class Pertemuan extends JPanel{

public Pertemuan9(){

this.setPreferredSize(new Dimension(700,500));

this.setBackground(Color.white);

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);

g2.drawString("Destination (Rectangle)",40,20);

g2.setColor(Color.RED);

g2.drawString("Source (Circle)",300,20);

g2.setColor(Color.BLACK);

drawComposite(g2,0.5f);

public void drawComposite(Graphics2D g2,float alpha){

//jenis jenis alphha composite

int[] alphaComp = {

AlphaComposite.SRC,AlphaComposite.DST_IN,

AlphaComposite.DST_OUT,AlphaComposite.DST_OVER,

AlphaComposite.SRC_IN,AlphaComposite.SRC_OVER,

AlphaComposite.SRC_OUT,AlphaComposite.CLEAR,

};

String [] caption = {

"SRC","DST_IN","DST_OUT","DST_OVER","SRC_IN","SRC_OVER","SRC_OUT","CLEAR"

};

//ambil object alpha composite

int x,y;

x = y = 40;

AlphaComposite ac;

AlphaComposite tac = AlphaComposite.getInstance(AlphaComposite.SRC,1.0f);

for(int j=0;j<alphaComp.length;j++){

//nilai alpha composite

ac = AlphaComposite.getInstance(alphaComp[j],alpha);
//begin : composite demo

BufferedImage buffImg = new BufferedImage(75,80,BufferedImage.TYPE_INT_ARGB);

Graphics2D gbi = buffImg.createGraphics();

gbi.setColor(Color.BLUE);

gbi.fillRect(0, 0, 50, 50);

gbi.setColor(Color.RED);

gbi.setComposite(ac);

gbi.fillOval(25, 0, 50, 50);

//End : composite demo

//display image

g2.drawImage(buffImg,null,x,y);

g2.drawString(caption[j],x, y+70);

x+=80;

public static void main(String[] args) {

JFrame frame = new JFrame("AlphaComposite");

frame.add(new Pertemuan9());

frame.setSize(800,500);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You might also like