我已经成功构建了一个JComponent饼图,它位于它自己独立的类IAPieChart.java中。 PieChart逻辑位于一个扩展JComponent的静态内部类中。 JApplet本身构建良好并显示'我在这里!'表示使用此代码构建和添加面板:
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()以及其他在搜索中看起来不错的东西,而我只是选择了。过去几周我一直表现不错,但这项任务一直困扰着我。我希望你能提供帮助。
答案 0 :(得分:2)
饼图未显示为PieChart
没有任何首选尺寸。因此,使用默认JPanel
的包含FlowLayout
不会显示它。应getPreferredSize
使用setPreferredSize
:
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
更新
在您修改的小程序中,您正在扩展未覆盖Applet
的旧AWT paintComponent
,而是使用JApplet
。
答案 1 :(得分:0)
我已尽最大努力重做程序,使用给出的建议,图片仍然不会显示在applet中。在摆脱实例化错误后,我得到了applet以便成功构建,但饼图不会显示。我希望有人能帮忙解决这个问题,以便我可以继续使用这个程序。
以下是我现在所拥有的:
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);
}
}