问题描述:有一个算法程序(内存消耗比较大),用的Jframe框架。
现在想把它放到网上,给别人用。
解决办法:提供一个.class文件,使别人可以把它下载下来使用。
这样就要把这个程序变为applet程序,
问题:1.请问可以把它嵌到applet中吗?
2.如果不可以,能否提出一个新的解决办法?
3.如果有热心人,我可以把代码拿出来,请高手帮忙弄的。
现在想把它放到网上,给别人用。
解决办法:提供一个.class文件,使别人可以把它下载下来使用。
这样就要把这个程序变为applet程序,
问题:1.请问可以把它嵌到applet中吗?
2.如果不可以,能否提出一个新的解决办法?
3.如果有热心人,我可以把代码拿出来,请高手帮忙弄的。
如果不这样,使用applet是不是好一点啊?我不懂的,别见笑啊
applet文件类/**
@version 1.32 2004-07-27
@author Cay Horstmann
*/import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;/**
Shows an animated bouncing ball.
*/
public class BounceThread extends Applet
{
public BounceThread()
{
JFrame frame = new BounceFrame();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A runnable that animates a bouncing ball.
*/
class BallRunnable implements Runnable
{
/**
Constructs the runnable.
@aBall the ball to bounce
@aPanel the component in which the ball bounces
*/
public BallRunnable(Ball aBall, Component aComponent)
{
ball = aBall;
component = aComponent;
} public void run()
{
try
{
for (int i = 1; i <= STEPS; i++)
{
ball.move(component.getBounds());
component.repaint();
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
System.out.println("被中断了");
}
} private Ball ball;
private Component component;
public static final int STEPS = 1000;
public static final int DELAY = 5;
}/**
A ball that moves and bounces off the edges of a
rectangle
*/
class Ball
{
/**
Moves the ball to the next position, reversing direction
if it hits one of the edges
*/
public void move(Rectangle2D bounds)
{
x += dx;
y += dy;
if (x < bounds.getMinX())
{
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX())
{
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY())
{
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY())
{
y = bounds.getMaxY() - YSIZE;
dy = -dy;
}
} /**
Gets the shape of the ball at its current position.
*/
public Ellipse2D getShape()
{
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
} private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1;
}/**
The panel that draws the balls.
*/
class BallPanel extends JPanel
{
/**
Add a ball to the panel.
@param b the ball to add
*/
public void add(Ball b)
{
balls.add(b);
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls)
{
g2.fill(b.getShape());
}
} private ArrayList<Ball> balls = new ArrayList<Ball>();
}/**
The frame with panel and buttons.
*/
class BounceFrame extends JFrame
{
/**
Constructs the frame with the panel for showing the
bouncing ball and Start and Close buttons
*/
public BounceFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setTitle("BounceThread"); panel = new BallPanel();
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start",
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
addBall();
}
});
addButton(buttonPanel, "Close",
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
} /**
Adds a button to a container.
@param c the container
@param title the button title
@param listener the action listener for the button
*/
public void addButton(Container c, String title, ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
} /**
Adds a bouncing ball to the canvas and starts a thread
to make it bounce
*/
public void addBall()
{
Ball b = new Ball();
panel.add(b);
Runnable r = new BallRunnable(b, panel);
Thread t = new Thread(r);
t.start();
} private BallPanel panel;
public static final int DEFAULT_WIDTH = 450;
public static final int DEFAULT_HEIGHT = 350;
public static final int STEPS = 1000;
public static final int DELAY = 3;
}
jsp页面<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:plugin code="BounceThread.class" height="300" width="300" codebase="http://localhost:8080/allWebTest/applet/" type="applet"></jsp:plugin>
</body>
</html>
把编译好的类文件提出来,放在服务器可访问到得路径,我把它放在了项目路径下的applet文件加下
我在eclipse里面建立的项目。html文件放在bin下面。编译后产生的bin下面的包下面
如
项目名
-src
---pakage
----xx.javaEclipseWorkspace\Gao\bin 放置html
EclipseWorkspace\Gao\bin\pakage 存放.class下面是将应用程序转化为applet的详细步骤
1)创建一个HTML页面,并用适当的标记加载applet代码.
2)创建一个JApplel类的子类.将该子类标记为public.否则,不能加载applet。
3)删去应用程序中的main方法。不要为应用程序构造框架窗口。因为应用程序将显示在浏览 器中.
4)将所有初始化代码从框架窗口的构造器中移到applet的init方法中.不需要明确地构造 applet对象,浏览器负责实例化井调用init方法.
5)删除对setSize的调用。对于applet来说,确定大小是通过HTML中的widlh和height参数完成的。
6)删除对setDefaultCloseOperation的调用。applet不能被关闭,当浏览器退出肘,它将终止运行.
7)如果应用程序调用setTitle.则删除这个调用. applet不能有标题栏. (可以 使用HTML的title标记来为网页设置标题。)
8)不要调用setVisible(true). applet 会自动地显示出来.