gpt4 book ai didi

Java,在 JApplet 中为静态图像添加动画

转载 作者:行者123 更新时间:2023-12-02 04:13:16 26 4
gpt4 key购买 nike

我正在做一项作业,基本上已经完成了作业,这只是一个简单的程序,用于创建图像映射并向热点添加一些音频,我还有几天时间,打算尝试一下并对图像进行“动画化”。

这是一张汽车仪表板的图片,当用户单击点火开关时,我想看看是否有办法让图像抖动一秒钟。我尝试在此处和 google 上查找,但每次搜索时,我都会收到 90 年代有关 applet 的文章,而不是 JApplet 的文章。

如果您能为我指明如何“动画”图像的正确方向,甚至指向可能有教程的地方,我将不胜感激!

如果您想了解我在说什么并且无论如何都可以帮助我,这是我的代码。

public class ImageMap extends JApplet implements MouseListener{
private Image pic;
private Container contentPane;
private JLabel directions;
private Rectangle horn;
private Rectangle vent;
private Rectangle doorLocksUpper;
private Rectangle window;
private Rectangle radio;
private Rectangle ignition;
private int x, y;
private AudioClip hornSound, airSound, radioClip, lockSound1, lockSound2, ignitionSound;

public void init() {
pic = getImage(getCodeBase(), "CarDash.jpg");
horn = new Rectangle(250, 142, 105,104);
vent = new Rectangle(514, 159, 204, 72);
doorLocksUpper = new Rectangle(80, 167, 104, 58);
window = new Rectangle(122, 243, 88, 55);
radio = new Rectangle(514, 234, 176, 171);
ignition = new Rectangle(465, 217, 42, 43);

directions = new JLabel("CLICK ON: Horn, Door Locks, Air Vents, Radio & Ignition Push Start");

//Create components
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(directions, BorderLayout.NORTH);
contentPane.addMouseListener(this);
}


//Display image on applet window
public void paint(Graphics g) {
g.drawImage(pic, 0, 40, this);

}

public void mouseClicked(MouseEvent me) {
//Play horn clip when car horn is clicked.
if(horn.contains(me.getX(), me.getY())) {
play(getCodeBase(), "HornSound.wav");
}
if(vent.contains(me.getX(), me.getY())) {
play(getCodeBase(), "AirVent.wav");
}
if(ignition.contains(me.getX(), me.getY())) {
play(getCodeBase(), "Ignition.wav");
}
if(doorLocksUpper.contains(me.getX(), me.getY())) {
play(getCodeBase(), "DoorLocks.wav");
}
if(radio.contains(me.getX(), me.getY())) {
play(getCodeBase(), "BrownSugar.mid");
}
if(window.contains(me.getX(), me.getY())) {
play(getCodeBase(), "Window.wav");
}
}
public void mouseReleased(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {}
}

最佳答案

这是一个非常快速且有用的提示,不要直接绘制到像小程序这样的顶级容器。它使您陷入单一用例,这并不总是能让修改或重用变得更容易。

相反,首先使用诸如 JPanel 之类的东西作为您的主要容器,这样您就可以将其添加到您喜欢的任何容器中,例如小程序、窗口或其他容器。

虽然可以通过多种方法在 Swing 中制作动画,但最简单(通常也是最安全)的方法之一是使用 Swing 计时器

此示例仅使用 Swing 计时器,设置为每 16 毫秒更新一次(这对于我的捕获软件来说太快了:P),当您单击面板时启动。定时器只是更新一个隔离正值和负值的偏移值。 Timer 使用 LocalTime 来计算 Timer 运行的时间长度,一秒后停止

Shake it up baby

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.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalTime;
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 ShakeAnimation {

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

public ShakeAnimation() {
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 ShakePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class ShakePane extends JPanel {

private Timer timer;
private LocalTime startTime;
private int xOffset = 0;
private int delta = 8;

private BufferedImage img;

public ShakePane() {
try {
img = ImageIO.read(getClass().getResource("Duke-Thumbs.jpg"));
} catch (IOException exp) {
exp.printStackTrace();
}
timer = new Timer(16, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Duration duration = Duration.between(startTime, LocalTime.now());
if (duration.getSeconds() >= 1) {
timer.stop();
xOffset = 0;
} else {
xOffset *= -1;
}
repaint();
}
});
addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
if (!timer.isRunning()) {
xOffset = delta;
startTime = LocalTime.now();
timer.start();
}
}

});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

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

}

}

参见How to use Swing Timers了解更多详情

关于Java,在 JApplet 中为静态图像添加动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577986/

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