当前位置: 技术问答>java相关
这个applet为什么显示不出来(附源码)
来源: 互联网 发布时间:2015-10-14
本文导语: 源代码编译通过,但是在用IE或appletviewer浏览时,什么都不显示,这是为什么,请各位高手帮忙。 =====源代码(Chart.java)====== import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class Chart ext...
源代码编译通过,但是在用IE或appletviewer浏览时,什么都不显示,这是为什么,请各位高手帮忙。
=====源代码(Chart.java)======
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class Chart extends JApplet
{
public void init()
{
String v = getParameter("values");
if (v == null) return;
int n = Integer.parseInt(v);
double[] values = new double[n];
String[] names = new String[n];
int i;
for (i = 0; i values[i]) maxValue = values[i];
}
if (maxValue == minValue) return;
int panelWidth = getWidth();
int panelHeight = getHeight();
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D titleBounds = titleFont.getStringBounds(title, context);
double titleWidth = titleBounds.getWidth();
double top = titleBounds.getHeight();
double y = -titleBounds.getY();
double x = (panelWidth - titleWidth) / 2;
g2.setFont(titleFont);
g2.drawString(title, (float)x, (float)y);
LineMetrics labelMetrics = labelFont.getLineMetrics("", context);
double bottom = labelMetrics.getHeight();
y = panelHeight - labelMetrics.getDescent();
g2.setFont(labelFont);
double scale = (panelHeight - top - bottom) / (maxValue - minValue);
int barWidth = panelWidth / values.length;
for (int i = 0; i = 0)
y1 += (maxValue - values[i]) * scale;
else
{
y1 += maxValue * scale;
height = -height;
}
Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height);
g2.setPaint(Color.red);
g2.fill(rect);
g2.setPaint(Color.black);
g2.draw(rect);
Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);
double labelWidth = labelBounds.getWidth();
x = i * barWidth + (barWidth - labelWidth) / 2;
g2.drawString(names[i], (float)x, (float)y);
}
}
private double[] values;
private String[] names;
private String title;
}
======源代码(Chart.htm)======
ChartTest
if your browser could show java, you would see a applet here.
=====源代码(Chart.java)======
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class Chart extends JApplet
{
public void init()
{
String v = getParameter("values");
if (v == null) return;
int n = Integer.parseInt(v);
double[] values = new double[n];
String[] names = new String[n];
int i;
for (i = 0; i values[i]) maxValue = values[i];
}
if (maxValue == minValue) return;
int panelWidth = getWidth();
int panelHeight = getHeight();
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D titleBounds = titleFont.getStringBounds(title, context);
double titleWidth = titleBounds.getWidth();
double top = titleBounds.getHeight();
double y = -titleBounds.getY();
double x = (panelWidth - titleWidth) / 2;
g2.setFont(titleFont);
g2.drawString(title, (float)x, (float)y);
LineMetrics labelMetrics = labelFont.getLineMetrics("", context);
double bottom = labelMetrics.getHeight();
y = panelHeight - labelMetrics.getDescent();
g2.setFont(labelFont);
double scale = (panelHeight - top - bottom) / (maxValue - minValue);
int barWidth = panelWidth / values.length;
for (int i = 0; i = 0)
y1 += (maxValue - values[i]) * scale;
else
{
y1 += maxValue * scale;
height = -height;
}
Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height);
g2.setPaint(Color.red);
g2.fill(rect);
g2.setPaint(Color.black);
g2.draw(rect);
Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);
double labelWidth = labelBounds.getWidth();
x = i * barWidth + (barWidth - labelWidth) / 2;
g2.drawString(names[i], (float)x, (float)y);
}
}
private double[] values;
private String[] names;
private String title;
}
======源代码(Chart.htm)======
ChartTest
if your browser could show java, you would see a applet here.
|
client端就是browser需要javax.swing package才能运行的。没有的话当然不能运行了。你或者在测试机器上装jre(版本1.2以上),或者你这样写让其自动下载插件。
|
用插件拉,一般的ie里面的java版本不够高不能看得到的了