使用 JLabels 更新图像的正确方法 [英] Proper way to use JLabels to update an image

查看:32
本文介绍了使用 JLabels 更新图像的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 GUI,我对 Swing 和 awt 还很陌生.我正在尝试创建一个 gui,它在启动时将背景设置为图像,然后使用一种方法来创建各种幻灯片.我已经尝试过了,但我没有附加到代码中,所以我能够接受修订和/或全新的概念.

EDIT(9/15/13):我的幻灯片有问题,我似乎无法让它工作.

这是我当前的代码.

公共类 MainFrame 扩展 JFrame{JLabel backgroundL = null;私人 JLabel 背景L;BufferedImage backimg;布尔忙;双倍宽度;双高;公共 MainFrame() 抛出 IOException {initMainframe();}public void initMainframe() 抛出 IOException {//misc 设置代码,加载默认的 jpg 作为背景setTitle("Pemin's Aura");忙 = 真;String backgroundDir = "resources/frame/background.jpg";backimg = ImageIO.read(new File(backgroundDir));backgroundL = new JLabel(new ImageIcon(backimg));setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);刷新帧();设置可见(真);忙 = 假;}public void adjSize() {//尝试启动全屏模式GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);宽度 = this.getWidth();高度 = this.getHeight();设置可见(真);}public void setmastheadText() {//未完成的代码忙 = 真;忙 = 假;}公共无效设置背景(){添加(背景L);}public void refreshframe() {//应该刷新图像吗?setSize(2049, 2049);setSize(2048, 2048);}public void loadingscreen() 抛出 IOException, InterruptedException {//这是有问题的代码.如果(忙==假){忙 = 真;String backgroundDir1 = "resources/frame/background.jpg";String backgroundDir2 = "resources/frame/scr1.jpg";String backgroundDir3 = "resources/frame/scr2.jpg";BufferedImage backimg1 = ImageIO.read(new File(backgroundDir1));BufferedImage backimg2 = ImageIO.read(new File(backgroundDir2));BufferedImage backimg3 = ImageIO.read(new File(backgroundDir3));backgroundL = new JLabel(new ImageIcon(backimg1));线程睡眠(2000);设置背景();设置可见(真);backgroundL = new JLabel(new ImageIcon(backimg2));设置背景();设置可见(真);线程睡眠(2000);bakckgroundL = new JLabel(new ImageIcon(backimg3));设置背景();设置可见(真);if(backimg != null) {backgroundL = new JLabel(new ImageIcon(backimg));;}}忙 = 假;}//载入画面结束

解决方案

参见 .

<小时>

当我在这里时,另一个(更漂亮的)动画图像示例.它使用这张墨卡托陆地地图.图像可以水平平铺,因此可以根据需要向左/向右滚动.

import java.awt.*;导入 java.awt.event.*;导入 java.awt.geom.Point2D;导入 java.awt.image.BufferedImage;导入 javax.swing.*;导入 java.net.URL;导入 javax.imageio.ImageIO;公共课世界观{public static void main(String[] args) 抛出异常 {URL url = new URL("http://i.stack.imgur.com/P59NF.png");最终 BufferedImage bi = ImageIO.read(url);可运行 r = 新可运行(){@覆盖公共无效运行(){整数宽度 = 640;整数高度 = 316;Graphics2D g = bi.createGraphics();float[] floats = new float[]{0f, .4f, .55f, 1f};颜色[] 颜色 = 新颜色[]{新颜色(20, 20, 20, 0),新颜色(0, 10, 20, 41),新颜色(0, 10, 20, 207),新颜色(0, 10, 20, 230),};最终的 LinearGradientPaint gp2 = 新的 LinearGradientPaint(新的 Point2D.Double(320f, 0f),新的 Point2D.Double(0f, 0f),漂浮,颜色,MultipleGradientPaint.CycleMethod.REFLECT);最终的 BufferedImage 画布 = 新的 BufferedImage(bi.getWidth(), bi.getHeight() + 60,BufferedImage.TYPE_INT_RGB);final JLabel animationLabel = new JLabel(new ImageIcon(canvas));ActionListener 动画师 = new ActionListener() {整数 x = 0;整数 y = 30;@覆盖public void actionPerformed(ActionEvent e) {Graphics2D g = canvas.createGraphics();g.setColor(新颜色(55, 75, 125));g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());int offset = (x % bi.getWidth());g.drawImage(bi, offset, y, null);g.drawImage(bi, offset - bi.getWidth(), y, null);g.setPaint(gp2);g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());g.dispose();animationLabel.repaint();x++;}};Timer timer = new Timer(40, animator);定时器开始();JOptionPane.showMessageDialog(null, animationLabel);定时器停止();}};//应该在 EDT 上创建和更新 Swing GUI//http://docs.oracle.com/javase/tutorial/uiswing/concurrencySwingUtilities.invokeLater(r);}}

<小时>

这是该图像的一个版本,其中添加了赤道(位于图像中心南"44 个像素处).

I am creating a GUI, and am fairly new to swing and awt. I am trying to create a gui that, upon launch, sets the background to an image, then uses a method to create a slideshow of sorts. I have attempted it, and I am not attached to the code so I am able to take both revisions and/or whole new concepts.

EDIT(9/15/13): I am having trouble with the slideshow, I cant seem to get it to work.

Here is my current code.

public class MainFrame extends JFrame{

JLabel backgroundL = null;
private JLabel bakckgroundL;
BufferedImage backimg;
Boolean busy;
double width;
double height;

public MainFrame() throws IOException {
    initMainframe();
}



public void initMainframe() throws IOException { 

//misc setup code, loads a default jpg as background

    setTitle("Pemin's Aura");
    busy = true;
    String backgroundDir = "resources/frame/background.jpg";

    backimg = ImageIO.read(new File(backgroundDir));
    backgroundL = new JLabel(new ImageIcon(backimg));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    refreshframe();
    setVisible(true);
    busy = false;
}
public void adjSize() { // the attempted start of a fullscreen mode
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
    width = this.getWidth();
    height = this.getHeight();
    setVisible(true);
}

public void setmastheadText() {//unfinished code
busy = true;

busy = false;
}
public void setbackground() {
    add(backgroundL);
}
public void refreshframe() { //should refresh image?
    setSize(2049, 2049);
    setSize(2048, 2048);
}
public void loadingscreen() throws IOException, InterruptedException {

 //this is the code in question that is faulty.

    if (busy == false) {
    busy = true;

    String backgroundDir1 = "resources/frame/background.jpg";
    String backgroundDir2 = "resources/frame/scr1.jpg";
    String backgroundDir3 = "resources/frame/scr2.jpg";

    BufferedImage backimg1 = ImageIO.read(new File(backgroundDir1));
    BufferedImage backimg2 = ImageIO.read(new File(backgroundDir2));
    BufferedImage backimg3 = ImageIO.read(new File(backgroundDir3));

    backgroundL = new JLabel(new ImageIcon(backimg1));
    Thread.sleep(2000);
    setbackground();
    setVisible(true);
    backgroundL = new JLabel(new ImageIcon(backimg2));
    setbackground();
    setVisible(true);
    Thread.sleep(2000);
    bakckgroundL = new JLabel(new ImageIcon(backimg3));
    setbackground();
    setVisible(true);

    if(backimg != null) {
         backgroundL = new JLabel(new ImageIcon(backimg));;
        }
    }
    busy = false;
}//end of loading screen

解决方案

See ImageViewer for a working example of displaying images using a Swing based Timer.

See also How to use Swing Timers.


And while I'm here, another (prettier) example of animating an image. It uses this Mercator map of land masses. The image can be tiled horizontally, and therefore be scrolled left/right as needed.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import javax.swing.*;

import java.net.URL;
import javax.imageio.ImageIO;

public class WorldView {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://i.stack.imgur.com/P59NF.png");
        final BufferedImage bi = ImageIO.read(url);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                int width = 640;
                int height = 316;
                Graphics2D g = bi.createGraphics();

                float[] floats = new float[]{0f, .4f, .55f, 1f};
                Color[] colors = new Color[]{
                    new Color(20, 20, 20, 0),
                    new Color(0, 10, 20, 41),
                    new Color(0, 10, 20, 207),
                    new Color(0, 10, 20, 230),};
                final LinearGradientPaint gp2 = new LinearGradientPaint(
                        new Point2D.Double(320f, 0f),
                        new Point2D.Double(0f, 0f),
                        floats,
                        colors,
                        MultipleGradientPaint.CycleMethod.REFLECT);

                final BufferedImage canvas = new BufferedImage(
                        bi.getWidth(), bi.getHeight() + 60,
                        BufferedImage.TYPE_INT_RGB);

                final JLabel animationLabel = new JLabel(new ImageIcon(canvas));
                ActionListener animator = new ActionListener() {

                    int x = 0;
                    int y = 30;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Graphics2D g = canvas.createGraphics();
                        g.setColor(new Color(55, 75, 125));

                        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

                        int offset = (x % bi.getWidth());
                        g.drawImage(bi, offset, y, null);
                        g.drawImage(bi, offset - bi.getWidth(), y, null);

                        g.setPaint(gp2);
                        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

                        g.dispose();

                        animationLabel.repaint();

                        x++;
                    }
                };
                Timer timer = new Timer(40, animator);
                timer.start();
                JOptionPane.showMessageDialog(null, animationLabel);
                timer.stop();
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}


Here is a version of that image with the equator added (it is 44 pixels 'south' of the center of the image).

这篇关于使用 JLabels 更新图像的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆