你是对的,你永远不知道面板的repaint() 什么时候会被执行。为了避免组件中出现任何不需要的视图,我会在后台线程中处理图像。这样,我不会太在意(当然我会)图像处理需要多长时间。最后,在处理完图像后,我会将它分享给 GUI(返回 EDT 线程)。
值得一提的是,在 Swing 中在后台运行任务的工具是 Swing Worker. Swing worker 将允许您在后台执行长时间任务,然后在适当的线程中更新 GUI(EDT - 事件调度线程)。
我创建了一个示例,其中框架由图像和“处理图像”按钮组成。
当按下按钮时,worker 启动。它处理图像(在我的例子中将图像裁剪为 90%),最后用新图像“刷新”视图,既美观又简单。
另外,为了回答您的问题:
在原始实例上调用 setPixel 是否是线程安全的?
自定义线程还是需要在 Swing 事件队列中调用
避免与paintComponent读取冲突?
您不必担心在图像处理任务期间将使用什么方法。只是,不要在那里更新摆动组件。完成后更新它们。
预览:
源代码:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class TestImage extends JFrame {
private Scratch scratch;
private JButton crop;
public TestImage() {
super("Process image");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
try {
BufferedImage img = loadImage();
scratch = new Scratch(img);
getContentPane().add(scratch, BorderLayout.CENTER);
} catch (IOException e) {
e.printStackTrace();
}
crop = new JButton("Process image");
crop.addActionListener(e -> processImage());
getContentPane().add(crop, BorderLayout.PAGE_END);
setSize(500, 500);
setLocationRelativeTo(null);
}
private void processImage() {
crop.setEnabled(false);
crop.setText("Processing image...");
new ImageProcessorWorker(scratch, () -> {
crop.setEnabled(true);
crop.setText("Process image");
}).execute();
}
private BufferedImage loadImage() throws IOException {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File image = new File(desktop, "img.png");
return ImageIO.read(image);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TestImage().setVisible(true));
}
public static class Scratch extends JPanel implements ImageView {
private static final long serialVersionUID = -5546688149216743458L;
private BufferedImage image;
public Scratch(BufferedImage image) {
this.image = image;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
@Override
public BufferedImage getImage() {
return image;
}
@Override
public void setImage(BufferedImage img) {
this.image = img;
repaint(); //repaint the view after image changes
}
}
public static class ImageProcessorWorker extends SwingWorker<BufferedImage, Void> {
private ImageView view;
private Runnable restoreTask;
public ImageProcessorWorker(ImageView v, Runnable restoreViewTask) {
view = v;
restoreTask = restoreViewTask;
}
@Override
protected BufferedImage doInBackground() throws Exception {
BufferedImage image = view.getImage();
image = crop(image, 0.9d);
Thread.sleep(5000); // Assume it takes 5 second to process
return image;
}
/*
* Taken from
* https://stackoverflow.com/questions/50562388/how-to-crop-image-in-java
*/
public BufferedImage crop(BufferedImage image, double amount) throws IOException {
BufferedImage originalImage = image;
int height = originalImage.getHeight();
int width = originalImage.getWidth();
int targetWidth = (int) (width * amount);
int targetHeight = (int) (height * amount);
// Coordinates of the image's middle
int xc = (width - targetWidth) / 2;
int yc = (height - targetHeight) / 2;
// Crop
BufferedImage croppedImage = originalImage.getSubimage(xc, yc, targetWidth, // widht
targetHeight // height
);
return croppedImage;
}
@Override
protected void done() {
try {
BufferedImage processedImage = get();
view.setImage(processedImage);
if (restoreTask != null)
restoreTask.run();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
super.done();
}
}
public static interface ImageView {
BufferedImage getImage();
void setImage(BufferedImage img);
}
}