Java vaadin中的缓冲图像
如何在vaadin中显示缓冲图像 我想用vaadin在图像视图中显示缓冲图像 所以,我在瓦丁的网站上看到了这个。但是,它没有完成 main.javaJava vaadin中的缓冲图像,java,vaadin,Java,Vaadin,如何在vaadin中显示缓冲图像 我想用vaadin在图像视图中显示缓冲图像 所以,我在瓦丁的网站上看到了这个。但是,它没有完成 main.java package com.example; import com.vaadin.server.StreamResource; import com.vaadin.server.StreamResource.StreamSource; import com.vaadin.ui.Layout; public class MainView {
package com.example;
import com.vaadin.server.StreamResource;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.ui.Layout;
public class MainView {
public MainView() {
// Create an instance of our stream source.
StreamSource imagesource = new MyImageSource();
// Create a resource that uses the stream source and give it
// a name. The constructor will automatically register the
// resource in the application.
StreamResource resource =
new StreamResource(imagesource, "myimage.png");
// Create an image component that gets its contents
// from the resource.
Layout.addComponent(new Image("Image title", resource);
}
}
MyImageSource.java
package com.example;
import com.vaadin.server.StreamResource.StreamSource;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class MyImageSource implements StreamSource {
ByteArrayOutputStream imagebuffer = null;
int reloads = 0;
// This method generates the stream contents
public InputStream getStream () {
// Create an image
BufferedImage image = new BufferedImage (400, 400,
BufferedImage.TYPE_INT_RGB);
Graphics2D drawable = image.createGraphics();
// Draw something static
drawable.setStroke(new BasicStroke(5));
drawable.setColor(Color.WHITE);
drawable.fillRect(0, 0, 400, 400);
drawable.setColor(Color.BLACK);
drawable.drawOval(50, 50, 300, 300);
// Draw something dynamic
drawable.setFont(new Font("Montserrat",
Font.PLAIN, 48));
drawable.drawString("Reloads=" + reloads, 75, 216);
reloads++;
drawable.setColor(new Color(0, 165, 235));
int x= (int) (200-10 + 150*Math.sin(reloads * 0.3));
int y= (int) (200-10 + 150*Math.cos(reloads * 0.3));
drawable.fillOval(x, y, 20, 20);
try {
// Write the image to a buffer
imagebuffer = new ByteArrayOutputStream();
ImageIO.write(image, "png", imagebuffer);
// Return a stream from the buffer
return new ByteArrayInputStream(
imagebuffer.toByteArray());
} catch (IOException e) {
return null;
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.vaadin.app</groupId>
<artifactId>app</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>app</name>
<prerequisites>
<maven>3</maven>
</prerequisites>
<properties>
<vaadin.version>8.0.5</vaadin.version>
<vaadin.plugin.version>8.0.5</vaadin.plugin.version>
<jetty.plugin.version>9.3.9.v20160517</jetty.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- If there are no local customisations, this can also be "fetch" or "cdn" -->
<vaadin.widgetset.mode>local</vaadin.widgetset.mode>
</properties>
<repositories>
<repository>
<id>vaadin-addons</id>
<url>https://maven.vaadin.com/vaadin-addons</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-server</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-push</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Exclude an unnecessary file generated by the GWT compiler. -->
<packagingExcludes>WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>update-theme</goal>
<goal>update-widgetset</goal>
<goal>compile</goal>
<!-- Comment out compile-theme goal to use on-the-fly theme compilation -->
<goal>compile-theme</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<!-- Clean up also any pre-compiled themes -->
<configuration>
<filesets>
<fileset>
<directory>src/main/webapp/VAADIN/themes</directory>
<includes>
<include>**/styles.css</include>
<include>**/styles.scss.cache</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<!-- The Jetty plugin allows us to easily test the development build by
running jetty:run on the command line. -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.plugin.version}</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- Vaadin pre-release repositories -->
<id>vaadin-prerelease</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<repositories>
<repository>
<id>vaadin-prereleases</id>
<url>https://maven.vaadin.com/vaadin-prereleases</url>
</repository>
<repository>
<id>vaadin-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>vaadin-prereleases</id>
<url>https://maven.vaadin.com/vaadin-prereleases</url>
</pluginRepository>
<pluginRepository>
<id>vaadin-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
4.0.0
my.vaadin.app
应用程序
战争
1.0-快照
应用程序
3.
8.0.5
8.0.5
9.3.9.v20160517
UTF-8
1.8
1.8
地方的
瓦丁插件
https://maven.vaadin.com/vaadin-addons
com.vaadin
瓦丁波姆
${vaadin.version}
聚甲醛
进口
javax.servlet
javax.servlet-api
3.0.1
假如
com.vaadin
瓦丁服务器
com.vaadin
瓦丁推
com.vaadin
瓦丁客户机
com.vaadin
瓦丁主题
org.apache.maven.plugins
maven战争插件
3.0.0
假的
WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**
com.vaadin
vaadin maven插件
${vaadin.plugin.version}
更新主题
更新WidgeSet
编译
编译主题
org.apache.maven.plugins
maven清洁插件
3.0.0
src/main/webapp/VAADIN/themes
**/styles.css
**/styles.scss.cache
org.eclipse.jetty
jetty maven插件
${jetty.plugin.version}
2.
瓦丁预释放
假的
瓦丁预释放
https://maven.vaadin.com/vaadin-prereleases
瓦丁快照
https://oss.sonatype.org/content/repositories/vaadin-snapshots/
假的
真的
瓦丁预释放
https://maven.vaadin.com/vaadin-prereleases
瓦丁快照
https://oss.sonatype.org/content/repositories/vaadin-snapshots/
假的
真的
我在新版本中有很多问题。所以,我现在转到旧版本
但是,我在图像布局上有错误。我如何解决这个问题?有两件事:
localhost:8080/从javascript调用java
,这与您定义的路由不匹配。当您在名为Main
的视图上有@Route
注释时,它将映射到根URL,因此请尝试仅使用localhost:8080
访问它add
方法的用途,在该方法中,您抛出了一个不支持操作异常
。我假设它是由您的IDE生成的,但是您正在扩展垂直布局
,它已经有一个add
方法,因此删除您的add
方法版本原因:java.lang.ClassCastException:com.example.MyImageSource无法转换为com.vaadin.flow.server.InputStreamFactory
您不能将MyImageSource
强制转换为InputStreamFactory
,因为它不是InputStreamFactory
不过你可以做一个。你所要做的就是
扩展StreamSource
更改为实现InputStreamFactory
public InputStream getStream()
更改为public InputStream createInputStream()
我遵循了以上两件事。但是,它没有完成。我编辑了我的问题。编辑了我的答案
com.example.MyImageSource无法转换为com.vaadin.flow.server.InputStreamFactory
请注意,您链接的教程是针对vaadin 8的。streamresourceapi从那时起就发生了变化,根据您的代码片段,您正在使用vaadin10或更高版本。以下是与Vaadin 14对应文档的链接:您使用的是什么版本的Vaadin?