gpt4 book ai didi

java - 饼图小程序未显示在浏览器中

转载 作者:行者123 更新时间:2023-12-02 04:44:56 24 4
gpt4 key购买 nike

我试图显示我的 java 小程序的饼图,但只显示文本。我检查了我的代码和 html,似乎没有什么问题。这是我的代码(如果有帮助的话):

Java 文件:

package org.me.pie;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class PieChart extends Applet
{

/**
* Initialization method that will be called after the applet is loaded into
* the browser.
*/
@Override
public void init()
{

}

@Override
public void paint(Graphics g)
{

// source: http://www.rasmussenreports.com/public_content/politics/obama_administration/daily_presidential_tracking_poll


// approval + disapproval percentages
int approve = 46;
int disapprove = 53;

int total;

// add up the approval and disapproval
total = approve + disapprove;


float approvePercent = (approve * 100.0f) / total; // percent who approve
float disapprovePercent = (disapprove * 100.0f) / total; // percent who disapprove


int x = 250, y = 50, w = 200, h = 200; // size of the pie chart

int startAngle, degrees; // used to draw a pie slice


// draw the approval slice
startAngle = 0;
degrees = (int)(approvePercent * 360 / 100);

g.setColor(Color.YELLOW);
g.fillArc(x, y, w, h, startAngle, degrees);



// draw the disapproval slice
startAngle += degrees;
degrees = (int)(disapprovePercent * 360 / 100);

g.setColor(Color.RED);
g.fillArc(x, y, w, h, startAngle, degrees);



// draw the strings for color codes on who approves and disapproves
g.setColor(Color.BLACK);

g.setFont(new Font("TimesRoman", Font.BOLD, 16));
g.drawString("Yellow - Approve", 20, 80);

g.drawString("Red - Disapprove", 20, 100);

super.paint(g);
}
}

和 html:

   <!doctype html>
<html>
<head>
<title>Applet</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<object type="application/x-java-applet" width="200" height="400">
<param name="code" value="org.me.pie.PieChart">
<param name="archive" value="Week4.jar">
Applet failed to run. No Java plug-in was found.
</object>

</div>
</body>
</html>

enter image description here enter image description here

如您所见,它在 netbeans 的小程序查看器中运行良好,但在浏览器中无法加载。任何帮助将不胜感激:)。

最佳答案

改变这个

<object type="application/x-java-applet" width="200" height="400">

像这样的事情

<object type="application/x-java-applet" width="800" height="600">

可见区域为 w=200 h=400,但圆圈似乎是在该可见区域之外绘制的。

为了清楚起见,使用插入的值,您将像这样绘制圆圈

g.fillArc(250, 50, 200, 200, startAngle, degrees);

最小宽度应为 x + 宽度 (250 + 200)。最小高度应为 y + 高度 (50 + 200)。

关于java - 饼图小程序未显示在浏览器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com