0

独自の別のクラス IAPieChart.java にある JComponent 円グラフを正常に作成しました。PieChart ロジックは、JComponent を拡張する静的内部クラスにあります。JApplet 自体は正常にビルドされ、「here I am!」と表示されます。このコードを使用してパネルが作成および追加されたことを示します。

public class IAPieChartApplet extends JApplet {

     JPanel controls;
     JPanel chartPanel;
     JComponent pieChart;

    /**
     * Initialization method that will be called after the Applet is loaded into
     * the browser.
     */

    public void init() {

        // Build the Pie Chart Panel
        buildPieChartPanel();

        // Build the controls panel
        buildControlsPanel();

        //Set the Layout
        setLayout(new FlowLayout());

        //getContentPane().add(new PieChart(), controls);
        //add(chartPanel);
        //add(controls);

    }



    private void buildPieChartPanel(){

        // Build the panel JPanel
        chartPanel = new JPanel();
        pieChart = new PieChart();
        JLabel label = new JLabel("Here I Am!");
        chartPanel.add(pieChart);
        chartPanel.add(label);

    }

    private void buildControlsPanel() {

        controls = new JPanel();
        JLabel here = new JLabel("Here I Am");
        controls.add(here);

    }

}

この IAPieChartApplet.java ファイルを実行すると、ラベルが表示されますが、PieChart は表示されません。

ラベルあり、円グラフなし

ここでメソッド ブレークポイントを設定し、静的な PieChart クラスにステップ インします。

private void buildPieChartPanel(){

        // Build the panel JPanel
        chartPanel = new JPanel();
        pieChart = new PieChart();
        JLabel label = new JLabel("Here I Am!");
        chartPanel.add(pieChart);
        chartPanel.add(label);

    }

Debug は私をここまで連れて行き、それからクラスの外に出ます。PieChart クラスの残りのロジックをステップ実行します。これは PieChart 静的クラスのコードで、これも正常に動作します。デバッグは、IAPieChart 配列まで進み、メソッドから戻ります。そして、これが「おそらく」表示されない理由です。

ここに画像の説明を入力

これは PieChart クラスのコードです。

public static class PieChart extends JComponent { 

        IAPieChart[] pieValue = {new IAPieChart(2, Color.green),
                                new IAPieChart(4, Color.orange),
                                new IAPieChart(4, Color.blue),
                                new IAPieChart(3, Color.red)

        };

        public void paintComponent(Graphics g) {

            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g; 
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_ON);
            drawPie((Graphics2D) g, getBounds(),  pieValue);

        }

        void drawPie(Graphics2D g, Rectangle area, IAPieChart[] pieValue){

            double sum = 0.0;
            for (int i = 0; i < pieValue.length; i++) {

                sum += pieValue[i].arcValue;
            }
            // DONT NEED endPoint to make the pieChart (Mishadoff's sample). 
            // needs double endPoint = 0.0D for (phcoding's sample).
            double endPoint =  0.0D;
            int arcStart = 0; 
            for (int i = 0; i < pieValue.length; i++){

                /////////THIS IS THE OLD STATEMENT////////
                //endPoint += (int) (endPoint * 360 / sum);
                arcStart = (int) (endPoint * 360 / sum);

                // this statement makes the pieChart.
                int radius = (int) (pieValue[i].arcValue * 360/ sum);
                g.setColor(pieValue[i].color);
                //g.fillArc(area.x, area.y, area.width, area.height, arcStart , radius);
                g.fillArc(area.x, area.y, area.width, area.height, arcStart , radius);

                ///////THIS IS THE OLD STATEMENT////
                //arcStart += pieValue[i].arcValue;
                endPoint += pieValue[i].arcValue;

                // this statement will make the pieChart.
                //arcStart += radius;
            }

        }
    }   // END PieChart class.*

getContentPane() や repaint() など、検索で良さそうなものは何でも使ってみましたが、選択肢がありません。ここ数週間、私は順調に進んでいますが、この任務は私を左右に悩ませてきました。お役に立てれば幸いです。

4

2 に答える 2

0

与えられた推奨事項を使用して、できる限りプログラムをやり直しましたが、グラフィックはまだアプレットに表示されません。インスタンス化エラーを取り除いた後、アプレットを正常にビルドできましたが、円グラフは表示されません。このプログラムで他の機能に進むことができるように、誰かがこれを手伝ってくれることを願っています.

これが私が今持っているものです:

package iapiechart;

import java.applet.Applet;
import java.awt.*;
import javax.swing.JComponent;

public class IAPieChart2 extends Applet
{
   double arcValue;        // passes a value for the calculation of the arc.
    Color color;            // holds value for color (expressed as an integer
    public IAPieChart2(){};
    public IAPieChart2(double value, Color color){

        arcValue = value;
        this.color = color;
    } 

    public  class PieChart extends JComponent { 

        public PieChart(){};

        IAPieChart[] pieValue = {new IAPieChart(2, Color.green),
                                new IAPieChart(4, Color.orange),
                                new IAPieChart(4, Color.blue),
                                new IAPieChart(3, Color.red)

        };
        @Override
    public void paintComponent(Graphics g) {

            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g; 
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_ON);
            drawPie((Graphics2D) g, getBounds(),  pieValue);

        }
    void drawPie(Graphics2D g, Rectangle area, IAPieChart[] pieValue){

            double sum = 0.0;
            for (int i = 0; i < pieValue.length; i++) {

                sum += pieValue[i].arcValue;
            }
            // DONT NEED endPoint to make the pieChart (Mishadoff's sample). 
            // needs double endPoint = 0.0D for (phcoding's sample).
            double endPoint =  0.0D;
            int arcStart = 0; 
            for (int i = 0; i < pieValue.length; i++){

                /////////THIS IS THE OLD STATEMENT////////
                //endPoint += (int) (endPoint * 360 / sum);
                arcStart = (int) (endPoint * 360 / sum);

                // this statement makes the pieChart.
                int radius = (int) (pieValue[i].arcValue * 360/ sum);
                g.setColor(pieValue[i].color);
                //g.fillArc(area.x, area.y, area.width, area.height, arcStart , radius);
                g.fillArc(area.x, area.y, area.width, area.height, arcStart , radius);

                endPoint += pieValue[i].arcValue;

            }
    }
    }
    @Override
    public void init(){

        IAPieChart2 iap2 = new IAPieChart2();
        PieChart pc = new PieChart();
        setPreferredSize(new Dimension(300, 200));
        iap2.getPreferredSize();
        super.add(pc);
        setVisible(true);

    }

}
于 2013-04-14T09:57:14.743 に答える