gpt4 book ai didi

java - 使用 JLabels 更新图像的正确方法

转载 作者:行者123 更新时间:2023-12-02 04:40:59 25 4
gpt4 key购买 nike

我正在创建一个 GUI,并且对 swing 和 awt 还很陌生。我正在尝试创建一个 gui,在启动时将背景设置为图像,然后使用一种方法来创建某种幻灯片。我已经尝试过,并且我不执着于代码,因此我能够接受修订和/或全新概念。

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

这是我当前的代码。

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

最佳答案

参见ImageViewer使用基于 Swing 的显示图像的工作示例 Timer .

另请参阅How to use Swing Timers .

<小时/>

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

Mercator Map of Land Masses

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("/image/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);
}
}
<小时/>

这是该图像的一个版本,添加了赤道(位于图像中心“以南”44 像素处)。

enter image description here

关于java - 使用 JLabels 更新图像的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com