请教,如何用java创建如下饼图,本人新手,越简单越好,有注释更好
展开全部
java有第三方的jar包可烂山以支持,jfreechar,可以到网上搜索相关资料,饼状图,柱状图。。都有。
用的时贺备候挺简单的禅历毁。
用的时贺备候挺简单的禅历毁。
追问
不用jfreechar怎么做
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import javax.swing.*;
public class AmericanPie {
public AmericanPie(String title) {
JFrame frame = new JFrame(title);
setLAF(); //设置风格
frame.getContentPane().add(new PiePanel());
init(frame);
}
class PiePanel extends JPanel {
private int width=200; //宽
private int height=200; //高
private int originX; //起始座标
private int originY;
@Override
public void paintComponent(Graphics g){
originX=(this.getSize().width/2)-(width/2);
originY=(this.getSize().height/2)-(height/2);
Graphics2D g2d=(Graphics2D) g;
g2d.setColor(Color.red);
g2d.fillArc(originX, originY,
width, height,
0, 72);
g2d.setColor(Color.BLUE);
g2d.fillArc(originX, originY,
width, height,
72, 36);
g2d.setColor(Color.GREEN);
g2d.fillArc(originX, originY,
width, height,
108, 108);
g2d.setColor(Color.WHITE);
g2d.fillArc(originX, originY,
width, height,
216, 144);
g2d.setColor(Color.black);
g2d.drawString("Projects -- 20%",originX+(width*7/州游8),originY+(height/6));
g2d.drawString("Quizzes -- 10%"蚂迹毁,originX+(width/2),originY);
g2d.drawString("Midterms -- 30%", originX-(width/4), originY+(height/3));
g2d.drawString("Final -- 40%",originX+(width*5/8),originY+(height*15/16));
}
}
private void setLAF() {
try {
UIManager
.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception ignore) {
}
}
}
public void init(JFrame f) {
f.setAlwaysOnTop(true);
f.setSize(new Dimension(400, 400));
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null); //窗口居闷备中
f.setVisible(true);
}
public static void main(String[] args) {
new AmericanPie("美国派7");
}
}
import javax.swing.*;
public class AmericanPie {
public AmericanPie(String title) {
JFrame frame = new JFrame(title);
setLAF(); //设置风格
frame.getContentPane().add(new PiePanel());
init(frame);
}
class PiePanel extends JPanel {
private int width=200; //宽
private int height=200; //高
private int originX; //起始座标
private int originY;
@Override
public void paintComponent(Graphics g){
originX=(this.getSize().width/2)-(width/2);
originY=(this.getSize().height/2)-(height/2);
Graphics2D g2d=(Graphics2D) g;
g2d.setColor(Color.red);
g2d.fillArc(originX, originY,
width, height,
0, 72);
g2d.setColor(Color.BLUE);
g2d.fillArc(originX, originY,
width, height,
72, 36);
g2d.setColor(Color.GREEN);
g2d.fillArc(originX, originY,
width, height,
108, 108);
g2d.setColor(Color.WHITE);
g2d.fillArc(originX, originY,
width, height,
216, 144);
g2d.setColor(Color.black);
g2d.drawString("Projects -- 20%",originX+(width*7/州游8),originY+(height/6));
g2d.drawString("Quizzes -- 10%"蚂迹毁,originX+(width/2),originY);
g2d.drawString("Midterms -- 30%", originX-(width/4), originY+(height/3));
g2d.drawString("Final -- 40%",originX+(width*5/8),originY+(height*15/16));
}
}
private void setLAF() {
try {
UIManager
.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception ignore) {
}
}
}
public void init(JFrame f) {
f.setAlwaysOnTop(true);
f.setSize(new Dimension(400, 400));
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null); //窗口居闷备中
f.setVisible(true);
}
public static void main(String[] args) {
new AmericanPie("美国派7");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |