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

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

问题描述

我正在创建一个GUI,而且是一个相当新的摇摆和awt。我正在尝试创建一个gui,在启动时,将背景设置为图像,然后使用方法创建各种幻灯片。我已经尝试过,而且我没有附加到代码中,所以我能够同时修改和/或全新的概念。



编辑(9/15/13):我在播放幻灯片时遇到问题,似乎无法让它发挥作用。



这是我当前的代码。

 公共类MainFrame扩展JFrame {

JLabel backgroundL = null;
private JLabel bakckgroundL;
BufferedImage backimg;
布尔值忙;
双倍宽度;
双倍高度;

public MainFrame()抛出IOException {
initMainframe();
}



public void initMainframe()抛出IOException {

// misc设置代码,加载默认jpg作为背景

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(){//尝试启动全屏模式
GraphicsEnvironment.getLocalGraphicsEnvironment()。getDefaultScreenDevice()。setFullScreenWindow(this);
width = this.getWidth();
height = this.getHeight();
setVisible(true);
}

public void setmastheadText(){//未完成的代码
busy = true;

busy = false;
}
public void setbackground(){
add(backgroundL);
}
public void refreshframe(){//应该刷新图像?
setSize(2049,2049);
setSize(2048,2048);
}
public void loadingscreen()抛出IOException,InterruptedException {

//这是有问题的代码有问题。

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;
} //加载结束屏幕


解决方案

有关使用基于Swing显示图像的工作示例,请参阅。






而我在这里是另一个(更漂亮的)动画图像的例子。它使用了墨卡托土地质量图。图像可以水平平铺,因此可以根据需要左/右滚动。



  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;

公共类WorldView {

public static void main(String [] args)抛出异常{
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};
颜色[]颜色=新颜色[] {
新颜色(20,20,20,0),
新颜色(0,10,20,41),
新颜色(0,10,20,207),
新颜色(0,10,20,230),};
final LinearGradientPaint gp2 = new LinearGradientPaint(
new Point2D.Double(320f,0f),
new Point2D.Double(0f,0f),
浮动,
颜色,
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 ++;
}
};
定时器计时器=新计时器(40,动画师);
timer.start();
JOptionPane.showMessageDialog(null,animationLabel);
timer.stop();
}
};
//应该在EDT上创建和更新Swing GUI
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.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天全站免登陆