【发布时间】:2019-04-04 08:28:42
【问题描述】:
我目前有一个使用 Vaadin 的上传组件的上传功能,但我不确定如何将文件上传的图像结果绘制到画布中,因为我是 Vaadin/Java 的新手。图像可以成功上传,但画布不会出现。我需要一个画布,因为我将使用它在随后上传的图像上绘制框。
这是我的代码:
package com.vaadin.starter.beveragebuddy.backend;
import com.vaadin.flow.component.dependency.HtmlImport;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MultiFileMemoryBuffer;
import com.vaadin.flow.router.Route;
import java.awt.Canvas;
public class MainLayout extends VerticalLayout {
private Canvas canvas;
public MainLayout() {
H2 title = new H2("Image Annotation Tool");
MultiFileMemoryBuffer buffer = new MultiFileMemoryBuffer();
Upload upload = new Upload(buffer);
upload.addSucceededListener(event -> {
// Component component = createComponent(event.getMIMEType(),
// event.getFileName(),
// buffer.getInputStream(event.getFileName()));
// showOutput(event.getFileName(), component, output);
});
add(upload);
}
public void Test() {
Frame frame = new Frame("Testing");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.add(new ImageCanvas());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class ImageCanvas extends Canvas {
private BufferedImage img;
public ImageCanvas() {
try {
img = ImageIO.read(new File("upload"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(1580, 800) : new Dimension(img.getWidth(), img.getHeight());
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (img != null) {
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g.drawImage(img, x, y, this);
}
}
}
}
非常感谢任何帮助,谢谢!
【问题讨论】:
-
vaadin 是哪个版本的?
-
@AndréSchild 版本 10 :)
标签: java canvas components vaadin