在Java applet中淡入淡出效果

时间:2011-05-29 02:43:56

标签: java applet

我正在使用节点和边缘制作这个java applet图形。我想在单击一个节点时实现对新节点检索的淡入和淡出效果,但我不知道如何编写和实现代码。 为了澄清,例如动物节点检索虎和狮子节点。所以当点击动物时,节点和附着的边缘逐渐消失,老虎和狮子节点逐渐出现。

提前谢谢

3 个答案:

答案 0 :(得分:1)

example使用AlphaComposite进行淡出。或者,此example根据饱和度组成一个颜色表。

答案 1 :(得分:0)

你需要写一个动画循环。每次循环都会改变边缘和/或节点的颜色。您可以使用Alpha淡入淡出或淡入淡出背景。如果您搜索“java applet animation loop”,网上有很多教程。一篇好文章是here

答案 2 :(得分:0)

Dear Friend here is the code that helps your problem

Kindly refer through it

/*Arpana*/

 import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.ColorSpace;
import javax.swing.border.TitledBorder;


public class ImageColorEffect extends JFrame implements ActionListener {
  DisplayPanel displayPanel;
  JRadioButton brightButton, darkButton, contrastIButton, contrastDButton,
  reverseButton, changeButton, resetButton;

  public ImageColorEffect() {
  super("Color Effect Example");
  Container container = getContentPane();
  container.setSize(500,500);
  displayPanel = new DisplayPanel();
  container.add(displayPanel);
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(3, 2));
  panel.setBorder(new TitledBorder("Click a Radio Button"));
  ButtonGroup group = new ButtonGroup();

  brightButton = new JRadioButton("Brightness",false);
  panel.add(brightButton);
  group.add(brightButton);
  brightButton.addActionListener(this);

  darkButton = new JRadioButton("Darkness",false);
  panel.add(darkButton);
  group.add(darkButton);
  darkButton.addActionListener(this);

  contrastIButton = new JRadioButton("Contrast in",false);
  panel.add(contrastIButton);
  group.add(contrastIButton);
  contrastIButton.addActionListener(this);

  contrastDButton = new JRadioButton("Contrast de",false);
  panel.add(contrastDButton);
  group.add(contrastDButton);
  contrastDButton.addActionListener(this);

  reverseButton = new JRadioButton("Negative",false);
  panel.add(reverseButton);
  group.add(reverseButton);
  reverseButton.addActionListener(this);

  changeButton = new JRadioButton("ChangeColor",false);
  panel.add(changeButton);
  group.add(changeButton);
  changeButton.addActionListener(this);

  resetButton = new JRadioButton("Reset",false);
  panel.add(resetButton);
  group.add(resetButton);
  resetButton.addActionListener(this);

  container.add(BorderLayout.NORTH, panel);

  addWindowListener(new WindowEventHandler());
  setSize(displayPanel.getWidth(), displayPanel.getHeight() + 25);
  show();
  }
 class WindowEventHandler extends WindowAdapter {
  public void windowClosing(WindowEvent e) {
  System.exit(0);
  }
  }
 public static void main(String arg[]) {
  new ImageColorEffect();
  }
 public void actionPerformed(ActionEvent e) {
  JRadioButton rbutton = (JRadioButton) e.getSource();
  if (rbutton.equals(brightButton)) {
  displayPanel.brightenLUT();
  displayPanel.applyFilter();
  displayPanel.repaint();
  } else if (rbutton.equals(darkButton)) {
  displayPanel.darkenLUT();
  displayPanel.applyFilter();
  displayPanel.repaint();
  } else if (rbutton.equals(contrastIButton)) {
  displayPanel.contrastIncLUT();
  displayPanel.applyFilter();
  displayPanel.repaint();
  } else if (rbutton.equals(contrastDButton)) {
  displayPanel.contrastDecLUT();
  displayPanel.applyFilter();
  displayPanel.repaint();
  } else if (rbutton.equals(reverseButton)) {
  displayPanel.reverseLUT();
  displayPanel.applyFilter();
  displayPanel.repaint();
  } else if (rbutton.equals(changeButton)) {
  displayPanel.repaint();
  displayPanel.grayOut();
  } else if (rbutton.equals(resetButton)) {
  displayPanel.reset();
  displayPanel.repaint();
  }
  }
  }
class DisplayPanel extends JPanel {
  Image disImage;
  BufferedImage image;
  Graphics2D graph;
  LookupTable lookup;

  DisplayPanel() {
  setBackground(Color.black); 
  loadImage();
  setSize(disImage.getWidth(this), disImage.getWidth(this)); 
  createBufferedImage();
  }
public void loadImage() {
  disImage = Toolkit.getDefaultToolkit().getImage(
  "Desert.jpg");
  MediaTracker media = new MediaTracker(this);
  media.addImage(disImage, 1);
  try {
  media.waitForAll();
  } catch (Exception e) {}

  if (disImage.getWidth(this) == -1) {
  System.out.println("file not found");
  System.exit(0);
  }
  }
public void createBufferedImage() {
  image = new BufferedImage(disImage.getWidth(this), disImage
  .getHeight(this), BufferedImage.TYPE_INT_ARGB);

  graph = image.createGraphics();
  graph.drawImage(disImage, 0, 0, this);
  }
 public void brightenLUT() {
  short brighten[] = new short[256];
  for (int i = 0; i < 256; i++) {
  short pixelValue = (short) (i + 10);
  if (pixelValue > 255)
  pixelValue = 255;
  else if (pixelValue < 0)
  pixelValue = 0;
  brighten[i] = pixelValue;
  }
  lookup = new ShortLookupTable(0, brighten);
  }
public void darkenLUT() {
  short brighten[] = new short[256];
  for (int i = 0; i < 256; i++) {
  short pixelValue = (short) (i - 10);
  if (pixelValue > 255)
  pixelValue = 255;
  else if (pixelValue < 0)
  pixelValue = 0;
  brighten[i] = pixelValue;
  }
  lookup = new ShortLookupTable(0, brighten);
  }
 public void contrastIncLUT() {
  short brighten[] = new short[256];
  for (int i = 0; i < 256; i++) {
  short pixelValue = (short) (i * 1.2);
  if (pixelValue > 255)
  pixelValue = 255;
  else if (pixelValue < 0)
  pixelValue = 0;
  brighten[i] = pixelValue;
  }
  lookup = new ShortLookupTable(0, brighten);
  }
public void contrastDecLUT() {
  short brighten[] = new short[256];
  for (int i = 0; i < 256; i++) {
  short pixelValue = (short) (i / 1.2);
  if (pixelValue > 255)
  pixelValue = 255;
  else if (pixelValue < 0)
  pixelValue = 0;
  brighten[i] = pixelValue;
  }
  lookup = new ShortLookupTable(0, brighten);
  }
public void reverseLUT() {
  byte reverse[] = new byte[256];
  for (int i = 0; i < 256; i++) {
  reverse[i] = (byte) (255 - i);
  }
  lookup = new ByteLookupTable(0, reverse);
  }
public void reset() {
  graph.setColor(Color.black);
  graph.clearRect(0, 0, image.getWidth(this), image.getHeight(this));
  graph.drawImage(disImage, 0, 0, this);
  }
  public void grayOut() {
  ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace
  .getInstance(ColorSpace.CS_GRAY), null);
  colorConvert.filter(image, image);
  }
 public void applyFilter() {
  LookupOp lop = new LookupOp(lookup, null);
  lop.filter(image, image);
  }
 public void update(Graphics g) {
  g.clearRect(0, 0, getWidth(), getHeight());
  paintComponent(g);
  }
 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2D = (Graphics2D) g;
  g2D.drawImage(image, 0, 0, this);
  }
}