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

时间:2015-04-19 23:26:57

标签: java

我正在尝试显示我的java applet的饼图,但只显示了文本。我已经检查了我的代码和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中的applet查看器中运行良好,但不会在浏览器中加载。任何帮助将不胜感激:)。

1 个答案:

答案 0 :(得分:2)

更改此

<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)。