简体   繁体   English

java小程序中的淡入淡出效果

[英]Fade in and fade out effect in java applet

I am making this java applet graphics with nodes and edges.我正在制作带有节点和边缘的 java 小程序图形。 I want to implement the fade-in and fade out effect on retrieval of the new nodes when one node is clicked, but I don't know how to write and implement the code.我想在点击一个节点时实现对新节点检索的淡入淡出效果,但我不知道如何编写和实现代码。 To clarify, for example animal node retrieves tiger and lion nodes.为了澄清,例如动物节点检索老虎和狮子节点。 so when animal is clicked the node and the edge attached to it gradually fades away and the tiger and lion nodes gradually appear.所以当动物被点击时,节点和连接到它的边缘逐渐消失,老虎和狮子节点逐渐出现。

Thank you in advance先感谢您

This example uses AlphaComposite to do the fading.示例使用AlphaComposite进行淡入淡出。 Alternatively, this example composes a color table based on saturation.或者,此示例根据饱和度组成一个颜色表。

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);
  }
}

You'll need to write an animation loop.您需要编写一个 animation 循环。 Each time through the loop change the color of your edge and/or node.每次循环都会改变边缘和/或节点的颜色。 You can use either an alpha fade or a color fade to background.您可以使用 alpha 渐变或颜色渐变到背景。 There are lots of tutorials on the web if you search for "java applet animation loop".如果您搜索“java applet animation loop”,则有很多关于 web 的教程。 One good article is here .一篇好文章在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM