java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.awt arrow Color Composite
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3911
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Color Composite E-mail
User Rating: / 7
PoorBest 

Image

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ColorComposite extends JFrame {
  MyCanvas canvas;

  JTextField textField;

  float alphaValue = 0.65f;

  public ColorComposite() {
    super();
    Container container = getContentPane();

    canvas = new MyCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();

    JLabel label = new JLabel("Color-Composite: ");

    JSlider slider = new JSlider(JSlider.HORIZONTAL, 010065);
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSlider tempSlider = (JSlidere.getSource();
        alphaValue = (float) (tempSlider.getValue() 100.0);
        textField.setText(Float.toString(alphaValue));
        canvas.repaint();
      }
    });

    textField = new JTextField("0.65"4);

    panel.add(label);
    panel.add(slider);
    panel.add(textField);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setSize(450,450);
    setVisible(true);
  }

  public static void main(String arg[]) {
    new ColorComposite();
  }

  class MyCanvas extends JLabel {
    Rectangle2D rec1, rec2, rec3, rec4, rec5;

    MyCanvas() {
      rec1 = new Rectangle2D.Float(252575150);
      rec2 = new Rectangle2D.Float(1252510075);
      rec3 = new Rectangle2D.Float(7512512575);
      rec4 = new Rectangle2D.Float(22512512575);
      rec5 = new Rectangle2D.Float(15050125175);

      setBackground(Color.white);
      setSize(400225);
    }

    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2Dg;

      AlphaComposite ac = AlphaComposite.getInstance(
          AlphaComposite.SRC_OVER, alphaValue);
      g2D.setComposite(ac);

      g2D.setStroke(new BasicStroke(5.0f));
      g2D.draw(rec1);

      GradientPaint gp = new GradientPaint(125f25f, Color.yellow, 225f,
          100f, Color.blue);
      g2D.setPaint(gp);
      g2D.fill(rec2);

      BufferedImage bi = new BufferedImage(55,
          BufferedImage.TYPE_INT_RGB);
      Graphics2D big = bi.createGraphics();
      big.setColor(Color.magenta);
      big.fillRect(0055);
      big.setColor(Color.black);
      big.drawLine(0055);
      Rectangle r = new Rectangle(0055);

      TexturePaint tp = new TexturePaint(bi, r);

      g2D.setPaint(tp);
      g2D.fill(rec3);

      g2D.setColor(Color.green);
      g2D.fill(rec4);
      g2D.setColor(Color.red);
      g2D.fill(rec5);
    }
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.