我一直在网站和互联网上搜索如何将使用GUI的java程序转换为applet。我已经找到了一般教程,比如基本程序,但是我使用OpenCV(http://opencv.org/)来生成图像,然后JFrame将其转换为GUI。我已设法将所有代码放入一个类文件中,以便我可以对其进行修改,以使其像applet一样运行(例如摆脱" main"方法和使用public void init()),然后有整个尝试将其放入HTML代码的过程,我只尝试直接标记,并尝试创建一个jar文件并将其与deployJava.js一起使用(java部署小程序的脚本)。我已经设法从浏览器页面中获取任何东西。我只需要加载html页面就可以运行它,最多只需点击一个按钮(但仅在必要时)。从未发现的异常到安全异常,再到InvocationTargetException。那么如何帮助我将这个程序放到网上呢?
import java.applet.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.lang.Math;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Point;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
public class BookOfFateProgram extends JFrame{
public BookOfFateProgram(){
//Creating the base image
System.loadLibrary("opencv_java246");
Mat image = Mat.zeros(500, 500, CvType.CV_32F);
Scalar white = new Scalar(255,255,255);
Scalar blackColor = new Scalar(0,0,0);
Point topLeftPoint = new Point(0,0);
Point bottomRightPoint = new Point(500,500);
Core.rectangle(image, topLeftPoint, bottomRightPoint, white, -1);
//Generating the random table to create the pattern
Mat table = new Mat(4,4,CvType.CV_32F);
Random rand = new Random();
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
table.put(i, j, Math.round(rand.nextDouble())+1);
}
}
System.out.println("table =" + table.dump());
System.out.println();
//Separating the table rows
Mat m1 = table.row(0);
Mat m2 = table.row(1);
Mat m3 = table.row(2);
Mat m4 = table.row(3);
System.out.println("m1 =" + m1.dump());
System.out.println("m2 =" + m2.dump());
System.out.println("m3 =" + m3.dump());
System.out.println("m4 =" + m4.dump());
System.out.println();
//Separating the table columns
Mat d1 = table.col(0).t();
Mat d2 = table.col(1).t();
Mat d3 = table.col(2).t();
Mat d4 = table.col(3).t();
System.out.println("d1 =" + d1.dump());
System.out.println("d2 =" + d2.dump());
System.out.println("d3 =" + d3.dump());
System.out.println("d4 =" + d4.dump());
System.out.println();
//Finding the XORs of the pairs of rows
Mat n1 = m1.mul(m2);
for(int i=0; i<4; i++){
double[] a = n1.get(0, i);
if(a[0]==4 || a[0]==1){
n1.put(0, i, 2);
}else{
n1.put(0, i, 1);
}
}
Mat n2 = m3.mul(m4);
for(int i=0; i<4; i++){
double[] a = n2.get(0, i);
if(a[0]==4 || a[0]==1){
n2.put(0, i, 2);
}else{
n2.put(0, i, 1);
}
}
System.out.println("n1 =" + n1.dump());
System.out.println("n2 =" + n2.dump());
System.out.println();
//Finding the XORs of the pairs of columns
Mat e1 = d1.mul(d2);
for(int i=0; i<4; i++){
double[] a = e1.get(0, i);
if(a[0]==4 || a[0]==1){
e1.put(0, i, 2);
}else{
e1.put(0, i, 1);
}
}
Mat e2 = d3.mul(d4);
for(int i=0; i<4; i++){
double[] a = e2.get(0, i);
if(a[0]==4 || a[0]==1){
e2.put(0, i, 2);
}else{
e2.put(0, i, 1);
}
}
System.out.println("e1 =" + e1.dump());
System.out.println("e2 =" + e2.dump());
System.out.println();
//Finding the XOR of the XORs of rows
Mat x = n1.mul(n2);
for(int i=0; i<4; i++){
double[] a = x.get(0, i);
if(a[0]==4 || a[0]==1){
x.put(0, i, 2);
}else{
x.put(0, i, 1);
}
}
System.out.println("x =" + x.dump());
//Finding XOR of the XORs of columns
Mat y = e1.mul(e2);
for(int i=0; i<4; i++){
double[] a = y.get(0, i);
if(a[0]==4 || a[0]==1){
y.put(0, i, 2);
}else{
y.put(0, i, 1);
}
}
System.out.println("y =" + y.dump());
System.out.println();
//Finding the final XOR and pattern
Mat z = x.mul(y);
for(int i=0; i<4; i++){
double[] a = z.get(0, i);
if(a[0]==4 || a[0]==1){
z.put(0, i, 2);
}else{
z.put(0, i, 1);
}
}
System.out.println("z =" + z.dump());
//All possible circles
Point x1 = new Point(100,75);
Point x2 = new Point(125,75);
Point x3 = new Point(150,75);
Point x4 = new Point(100,125);
Point x5 = new Point(125,125);
Point x6 = new Point(150,125);
Point x7 = new Point(100,175);
Point x8 = new Point(125,175);
Point x9 = new Point(150,175);
Point x10 = new Point(100,225);
Point x11 = new Point(125,225);
Point x12 = new Point(150,225);
Point y1 = new Point(350,75);
Point y2 = new Point(375,75);
Point y3 = new Point(400,75);
Point y4 = new Point(350,125);
Point y5 = new Point(375,125);
Point y6 = new Point(400,125);
Point y7 = new Point(350,175);
Point y8 = new Point(375,175);
Point y9 = new Point(400,175);
Point y10 = new Point(350,225);
Point y11 = new Point(375,225);
Point y12 = new Point(400,225);
Point z1 = new Point(225,275);
Point z2 = new Point(250,275);
Point z3 = new Point(275,275);
Point z4 = new Point(225,325);
Point z5 = new Point(250,325);
Point z6 = new Point(275,325);
Point z7 = new Point(225,375);
Point z8 = new Point(250,375);
Point z9 = new Point(275,375);
Point z10 = new Point(225,425);
Point z11 = new Point(250,425);
Point z12 = new Point(275,425);
//Drawing the patterns
double[] a = x.get(0, 0);
if(a[0]==2){
Core.circle(image, x1, 10, blackColor, -1);
Core.circle(image, x3, 10, blackColor, -1);
}else{
Core.circle(image, x2, 10, blackColor, -1);
}
a = x.get(0, 1);
if(a[0]==2){
Core.circle(image, x4, 10, blackColor, -1);
Core.circle(image, x6, 10, blackColor, -1);
}else{
Core.circle(image, x5, 10, blackColor, -1);
}
a = x.get(0, 2);
if(a[0]==2){
Core.circle(image, x7, 10, blackColor, -1);
Core.circle(image, x9, 10, blackColor, -1);
}else{
Core.circle(image, x8, 10, blackColor, -1);
}
a = x.get(0, 3);
if(a[0]==2){
Core.circle(image, x10, 10, blackColor, -1);
Core.circle(image, x12, 10, blackColor, -1);
}else{
Core.circle(image, x11, 10, blackColor, -1);
}
double[] b = y.get(0, 0);
if(b[0]==2){
Core.circle(image, y1, 10, blackColor, -1);
Core.circle(image, y3, 10, blackColor, -1);
}else{
Core.circle(image, y2, 10, blackColor, -1);
}
b = y.get(0, 1);
if(b[0]==2){
Core.circle(image, y4, 10, blackColor, -1);
Core.circle(image, y6, 10, blackColor, -1);
}else{
Core.circle(image, y5, 10, blackColor, -1);
}
b = y.get(0, 2);
if(b[0]==2){
Core.circle(image, y7, 10, blackColor, -1);
Core.circle(image, y9, 10, blackColor, -1);
}else{
Core.circle(image, y8, 10, blackColor, -1);
}
b = y.get(0, 3);
if(b[0]==2){
Core.circle(image, y10, 10, blackColor, -1);
Core.circle(image, y12, 10, blackColor, -1);
}else{
Core.circle(image, y11, 10, blackColor, -1);
}
double[] c = z.get(0, 0);
if(c[0]==2){
Core.circle(image, z1, 10, blackColor, -1);
Core.circle(image, z3, 10, blackColor, -1);
}else{
Core.circle(image, z2, 10, blackColor, -1);
}
c = z.get(0, 1);
if(c[0]==2){
Core.circle(image, z4, 10, blackColor, -1);
Core.circle(image, z6, 10, blackColor, -1);
}else{
Core.circle(image, z5, 10, blackColor, -1);
}
c = z.get(0, 2);
if(c[0]==2){
Core.circle(image, z7, 10, blackColor, -1);
Core.circle(image, z9, 10, blackColor, -1);
}else{
Core.circle(image, z8, 10, blackColor, -1);
}
c = z.get(0, 3);
if(c[0]==2){
Core.circle(image, z10, 10, blackColor, -1);
Core.circle(image, z12, 10, blackColor, -1);
}else{
Core.circle(image, z11, 10, blackColor, -1);
}
LoadImage("C:/Users/Sean P Meadows/Pictures/test.jpg", image);
}
public void LoadImage(String imgStr, Mat m){
Highgui.imwrite(imgStr,m);
JFrame frame = new JFrame("Book of Fate");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
ImageIcon image = new ImageIcon(imgStr);
frame.setSize(image.getIconWidth()+10, image.getIconHeight()+35);
JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
frame.getContentPane().add(label1);
frame.validate();
frame.setVisible(true);
}
public static void main(String[] args){
BookOfFateProgram b = new BookOfFateProgram();
}
}
<!DOCTYPE html>
<html>
<head>
<title>The Book of Fate</title>
<link rel="icon" href="file:///C:/Users/Sean%20P%20Meadows/Documents/OPENCV_Project/favicon.ico" type="image/vnd.microsoft.icon">
<style>
</style>
</head>
<body>
<script src="https://www.java.com/js/deployJava.js"></script>
<script>
var dir = location.href.substring(0, location.href.lastIndexOf('/')+1);
var url = dir + "BookOfFate_webstart.jnlp";
deployJava.createWebStartLaunchButton(url, '1.7.0');
</script>
</body>
</html>
修改
所以我已经完成了HTML文件中的.jnlp文件和Deployment Toolkit脚本。在询问文件位置时,我不能100%确定如何正确格式化两个文件中的路径名。一切都在我的本地机器上。当我在浏览器中打开HTML并选择按钮时,它无法启动应用程序,例外是
这是.jnlp
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<information>
<title>Book Of Fate</title>
<vendor>Sean Meadows</vendor>
<icon href="file:///C:/Users/Sean%20P%20Meadows/Documents/OPENCV_Project/favicon.ico"/>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="BookOfFateJWS.jar" main="true"/>
</resources>
<application-desc name="Book Of Fate" main-class="BookOfFateProgram" width="500" height="500">
</application-desc>
<update check="background"/>
</jnlp>
和html脚本调用:
<script src="https://www.java.com/js/deployJava.js"></script>
<script>
var dir = location.href.substring(0, location.href.lastIndexOf('/')+1);
var url = dir + "BookOfFate_webstart.jnlp";
deployJava.createWebStartLaunchButton(url, '1.7.0');
</script>