Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaJFrame中为图像添加可见性效果_Java_Swing - Fatal编程技术网

在JavaJFrame中为图像添加可见性效果

在JavaJFrame中为图像添加可见性效果,java,swing,Java,Swing,我使用Java的JFrame来显示.jpeg图像。当选中复选框时,我需要启动图像的水平滑动效果。因此,基本上当选中复选框时,图像将开始从左向右滑动,需要几秒钟,完成后,将永远重新开始,直到复选框被取消选中。如何添加此功能 编辑:实际上我并不是说图片本身在移动;我的意思是图片是稳定和静态的,但是图像将开始以水平滑动效果从左到右可见,然后重新启动。我希望这足够清楚 假设下面是显示图像和复选框的代码(来自Java教程): 最简单的解决方案是使用javax.swing.Timer,例如 import j

我使用Java的JFrame来显示
.jpeg
图像。当选中复选框时,我需要启动图像的水平滑动效果。因此,基本上当选中复选框时,图像将开始从左向右滑动,需要几秒钟,完成后,将永远重新开始,直到复选框被取消选中。如何添加此功能

编辑:实际上我并不是说图片本身在移动;我的意思是图片是稳定和静态的,但是图像将开始以水平滑动效果从左到右可见,然后重新启动。我希望这足够清楚

假设下面是显示图像和复选框的代码(来自Java教程):


最简单的解决方案是使用
javax.swing.Timer
,例如

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SlidingAnimation {

    public static void main(String[] args) {
        new SlidingAnimation();
    }

    public SlidingAnimation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private int x = 0;

        private Timer timer;
        private long startTime = -1;
        private int playTime = 4000;

        public TestPane() {
            try {
                img = ImageIO.read(new File("..."));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    float progress = 0f;
                    if (startTime == -1) {
                        startTime = System.currentTimeMillis();
                    } else {
                        long currentTime = System.currentTimeMillis();
                        long diff = currentTime - startTime;

                        if (diff >= playTime) {
                            diff = 0;
                            startTime = -1;
                        }
                        progress = diff / (float)playTime;
                    }

                    x = (int)((getWidth() - img.getWidth()) * progress);
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth() * 2, img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int y = (getHeight() - img.getHeight()) / 2;
                g2d.drawImage(img, x, y, this);
                g2d.dispose();
            }
        }

    }

}

这是一个两秒钟的循环,它根据开始时间和当前时间之间的差值以及动画需要移动的总面积来计算当前位置。这确实使它灵活,但它是一个直线的线性动画,它没有很好的缓进和缓出,使动画更逼真的运动

对于更高级的动画效果,我强烈建议您看看

  • 。提供对核心的良好访问,以执行不寻常的操作,但也能够随时间更改对象属性
  • 。提供随时间更改对象属性的功能
  • 我没用过,但看起来很不错
您可能还想了解有关如何在Swing中完成自定义绘制的更多详细信息

已更新

所以,如果我理解你的评论,你想做一个交叉淡入淡出效果。现在有几种方法可以做到这一点,您可以使用
BufferedImage#subImage
获得原始图像的“裁剪”版本并显示出来,但是,IMHO,这并不能产生这么好的效果

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SlidingAnimation {

    public static void main(String[] args) {
        new SlidingAnimation();
    }

    public SlidingAnimation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        private Timer timer;
        private long startTime = -1;
        private int playTime = 4000;

        private float progress;

        public TestPane() {
            try {
                img = ImageIO.read(new File("..."));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (startTime == -1) {
                        startTime = System.currentTimeMillis();
                    } else {
                        long currentTime = System.currentTimeMillis();
                        long diff = currentTime - startTime;

                        if (diff >= playTime) {
                            diff = 0;
                            startTime = -1;
                        }
                        progress = diff / (float) playTime;
                    }

                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

        protected BufferedImage generateImage() {

            BufferedImage buffer = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = buffer.createGraphics();
            g2d.setBackground(new Color(0, 0, 0, 0));
            g2d.clearRect(0, 0, buffer.getWidth(), buffer.getHeight());
            g2d.drawImage(img, 0, 0, this);

            float startAt = progress - 0.05f;
            float endAt = progress + 0.05f;

            if (endAt <= 0.1f) {
                startAt = 0;
                endAt = Math.max(0.1f, progress);
            } else if (endAt >= 1f) {
                endAt = 1f;
                startAt = progress;
            }

            LinearGradientPaint lgp = new LinearGradientPaint(
                    new Point2D.Float(0, 0),
                    new Point2D.Float(img.getWidth(), 0),
                    new float[]{startAt, endAt},
                    new Color[]{new Color(0, 0, 0, 0), Color.RED});

            g2d.setPaint(lgp);

            g2d.setComposite(AlphaComposite.DstOut.derive(1f));
            g2d.fill(new Rectangle(0, 0, img.getWidth(), img.getHeight()));
            g2d.dispose();

            return buffer;

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int y = (getHeight() - img.getHeight()) / 2;
                int x = (getWidth() - img.getWidth()) / 2;
                g2d.drawImage(generateImage(), x, y, this);

                g2d.dispose();
            }
        }

    }

}
相反,你可以使用掩蔽技术,让你产生一个褪色的效果

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SlidingAnimation {

    public static void main(String[] args) {
        new SlidingAnimation();
    }

    public SlidingAnimation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        private Timer timer;
        private long startTime = -1;
        private int playTime = 4000;

        private float progress;

        public TestPane() {
            try {
                img = ImageIO.read(new File("..."));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (startTime == -1) {
                        startTime = System.currentTimeMillis();
                    } else {
                        long currentTime = System.currentTimeMillis();
                        long diff = currentTime - startTime;

                        if (diff >= playTime) {
                            diff = 0;
                            startTime = -1;
                        }
                        progress = diff / (float) playTime;
                    }

                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

        protected BufferedImage generateImage() {

            BufferedImage buffer = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = buffer.createGraphics();
            g2d.setBackground(new Color(0, 0, 0, 0));
            g2d.clearRect(0, 0, buffer.getWidth(), buffer.getHeight());
            g2d.drawImage(img, 0, 0, this);

            float startAt = progress - 0.05f;
            float endAt = progress + 0.05f;

            if (endAt <= 0.1f) {
                startAt = 0;
                endAt = Math.max(0.1f, progress);
            } else if (endAt >= 1f) {
                endAt = 1f;
                startAt = progress;
            }

            LinearGradientPaint lgp = new LinearGradientPaint(
                    new Point2D.Float(0, 0),
                    new Point2D.Float(img.getWidth(), 0),
                    new float[]{startAt, endAt},
                    new Color[]{new Color(0, 0, 0, 0), Color.RED});

            g2d.setPaint(lgp);

            g2d.setComposite(AlphaComposite.DstOut.derive(1f));
            g2d.fill(new Rectangle(0, 0, img.getWidth(), img.getHeight()));
            g2d.dispose();

            return buffer;

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int y = (getHeight() - img.getHeight()) / 2;
                int x = (getWidth() - img.getWidth()) / 2;
                g2d.drawImage(generateImage(), x, y, this);

                g2d.dispose();
            }
        }

    }

}

导入java.awt.AlphaComposite;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.LinearGradientPaint;
导入java.awt.Rectangle;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.geom.Point2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类幻灯片动画{
公共静态void main(字符串[]args){
新的滑动动画();
}
公共幻灯片动画(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
专用缓冲图像img;
私人定时器;
私有长startTime=-1;
专用int播放时间=4000;
私人浮动进度;
公共测试窗格(){
试一试{
img=ImageIO.read(新文件(“…”);
}捕获(IOEX异常){
例如printStackTrace();
}
计时器=新计时器(40,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(开始时间==-1){
startTime=System.currentTimeMillis();
}否则{
长currentTime=System.currentTimeMillis();
长差异=当前时间-开始时间;
如果(差异>=播放时间){
差异=0;
开始时间=-1;
}
进度=差异/(浮动)播放时间;
}
重新油漆();
}
});
timer.start();
}
@凌驾
公共维度getPreferredSize(){
返回img==null?新维度(200200):新维度(img.getWidth(),img.getHeight());
}
受保护的BuffereImage generateImage(){
BuffereImage buffer=新的BuffereImage(img.getWidth(),img.getHeight(),BuffereImage.TYPE_INT_ARGB);
Graphics2D g2d=buffer.createGraphics();
新颜色(0,0,0,0));
clearRect(0,0,buffer.getWidth(),buffer.getHeight());
g2d.drawImage(img,0,0,this);
float startAt=进度-0.05f;
浮动端=进度+0.05f;
如果(端部=1f){
endAt=1f;
startAt=进展;
}
LinearGradientPaint lgp=新的LinearGradientPaint(
新的Point2D.Float(0,0),
新的Point2D.Float(img.getWidth(),0),
新浮点[]{startAt,endAt},
新颜色[]{新颜色(0,0,0,0),Color.RED});
g2d.setPaint(lgp);
g2d.setComposite(AlphaComposite.DstOut.derivate(1f));
填充(新矩形(0,0,img.getWidth(),img.get