《java程序设计》 黄晓彤译 第八章 数组和集合
书上的例子但是不知道为什么不能运行?不知道哪里错了。 程序都没有错误了
下面是 PieChart.java 文件:
//表示一个统计饼图
import java.awt.*;public class PieChart {
//默认特性
private static final Color[] DEFAULT_PIE_COLOR={
Color.red,Color.blue,Color.magenta,Color.black,
Color.green,Color.orange,Color.cyan,Color.pink,
Color.gray,Color.yellow,Color.DARK_GRAY,Color.WHITE };
private static final int DEFAULT_RADIUS = 115; //默认半径
private static final int DEFAULT_LINE_SPACING = 20; //图例相邻两行间默认的间隔像素
private static final Point DEFAULT_PIE_LOCATION //饼状图外接正方形的左上角的默认位置
= new Point(10,10);
private static final Point DEFAULT_LEGEND_LOCATION //图例外接正方形的左上角的默认位置
= new Point(265,35);
//每个饼图的特征
private double[] data ; //表示数据
private String[] name; //数据图例名字
private Color[] color; //数据表示颜色
private int radius; //饼图半径
private Point pieLocation; //饼图位置
private Point legendLocation; //图例位置
private int lineSpacing; //图例间间隙
//PieChart():默认构造方法
public PieChart(double[] d, String[] s ){
this(d, s, DEFAULT_PIE_COLOR);
}
//PieChart()使用构造方法中的数据 名字 颜色
public PieChart(double[] d,String[] s,Color[] c){
if((d.length == 0) || (d.length > s.length)
|| (d.length > c.length)){
System.err.println("Piechart: invalid data");
System.exit(1);
}
data = d;
name = s;
color = c;
radius = DEFAULT_RADIUS;
pieLocation = DEFAULT_PIE_LOCATION;
legendLocation = DEFAULT_LEGEND_LOCATION;
lineSpacing = DEFAULT_LINE_SPACING;
}
//getSampleSize()返回成员数据值
public int getSampleSize(){
return data.length;
}
//getColor()返回该类索代表数据集的第i值的关联颜色
public Color getColor(int i){
return color[i];
}
//getData()返回该类索代表数据集的第i值
public double getData(int i){
return data[i];
}
//getName()返回该类索代表数据集的第i值的关联图例的名字
public String getName(int i){
return name[i];
}
//getRadius()返回饼图的半径
public int getRadius(){
return radius;
}
//getPielocation()返回饼图外接正方形的左上角位置
public Point getPieLocation(){
return pieLocation;
}
//getLegendLocation()返回图例开始显示的位置
public Point getLegendLocation(){
return legendLocation;
}
//getLineSpacing()以像素为单位返回相邻图例各行的间距
public int getLineSpacing(){
return lineSpacing;
}
//paint()给图和图例着色 paint()显示整个饼状图
public void paint(Graphics g){
//获得饼图的特性
int r = getRadius();
Point pie = getPieLocation();
Point legend = getLegendLocation(); //
int spacing = getLineSpacing();
//画图表
paintPie(g,pie.x,pie.y,r);
paintLegend(g,legend.x,legend.y,spacing);
}
//给饼图着色 paintPie()只显示饼状图的饼
public void paintPie(Graphics g,int x,int y,int r){
final int CIRCLE_DEGREES = 360;
//确定成员值
int n =getSampleSize();
//计算值之和
double dataSum = 0;
for(int i=0;i<n;i++){
dataSum += getData(i);
}
//从起始原点画一小块
int startAngle = 0;
for(int i=0;i<n;i++){
//饼图分几块画
double percent= getData(i) / dataSum;
int arcAngle = (int)Math.round(percent * CIRCLE_DEGREES);
//给块设置颜色
Color c = getColor(i);
g.setColor(c);
//给块着色
g.fillArc(x,y,2*r,2*r,startAngle,arcAngle);
//纪录下一块的起始点
startAngle += arcAngle;
}
}
//为饼图的图例着色 paintLegend()显示饼状图的相关图例
public void paintLegend(Graphics g,int x,int y,int delta){
//确定成员值
int n = getSampleSize();
for(int i= 0 ; i<n;++i){
//启动当前图例行
Color c = getColor(i);
String s = getName(i)+":"+getData(i);
//为行着色
g.setColor(c);
g.drawString(s,x,y);
//从下一行启动y坐标位置
y += delta;
}
}
}
书上的例子但是不知道为什么不能运行?不知道哪里错了。 程序都没有错误了
下面是 PieChart.java 文件:
//表示一个统计饼图
import java.awt.*;public class PieChart {
//默认特性
private static final Color[] DEFAULT_PIE_COLOR={
Color.red,Color.blue,Color.magenta,Color.black,
Color.green,Color.orange,Color.cyan,Color.pink,
Color.gray,Color.yellow,Color.DARK_GRAY,Color.WHITE };
private static final int DEFAULT_RADIUS = 115; //默认半径
private static final int DEFAULT_LINE_SPACING = 20; //图例相邻两行间默认的间隔像素
private static final Point DEFAULT_PIE_LOCATION //饼状图外接正方形的左上角的默认位置
= new Point(10,10);
private static final Point DEFAULT_LEGEND_LOCATION //图例外接正方形的左上角的默认位置
= new Point(265,35);
//每个饼图的特征
private double[] data ; //表示数据
private String[] name; //数据图例名字
private Color[] color; //数据表示颜色
private int radius; //饼图半径
private Point pieLocation; //饼图位置
private Point legendLocation; //图例位置
private int lineSpacing; //图例间间隙
//PieChart():默认构造方法
public PieChart(double[] d, String[] s ){
this(d, s, DEFAULT_PIE_COLOR);
}
//PieChart()使用构造方法中的数据 名字 颜色
public PieChart(double[] d,String[] s,Color[] c){
if((d.length == 0) || (d.length > s.length)
|| (d.length > c.length)){
System.err.println("Piechart: invalid data");
System.exit(1);
}
data = d;
name = s;
color = c;
radius = DEFAULT_RADIUS;
pieLocation = DEFAULT_PIE_LOCATION;
legendLocation = DEFAULT_LEGEND_LOCATION;
lineSpacing = DEFAULT_LINE_SPACING;
}
//getSampleSize()返回成员数据值
public int getSampleSize(){
return data.length;
}
//getColor()返回该类索代表数据集的第i值的关联颜色
public Color getColor(int i){
return color[i];
}
//getData()返回该类索代表数据集的第i值
public double getData(int i){
return data[i];
}
//getName()返回该类索代表数据集的第i值的关联图例的名字
public String getName(int i){
return name[i];
}
//getRadius()返回饼图的半径
public int getRadius(){
return radius;
}
//getPielocation()返回饼图外接正方形的左上角位置
public Point getPieLocation(){
return pieLocation;
}
//getLegendLocation()返回图例开始显示的位置
public Point getLegendLocation(){
return legendLocation;
}
//getLineSpacing()以像素为单位返回相邻图例各行的间距
public int getLineSpacing(){
return lineSpacing;
}
//paint()给图和图例着色 paint()显示整个饼状图
public void paint(Graphics g){
//获得饼图的特性
int r = getRadius();
Point pie = getPieLocation();
Point legend = getLegendLocation(); //
int spacing = getLineSpacing();
//画图表
paintPie(g,pie.x,pie.y,r);
paintLegend(g,legend.x,legend.y,spacing);
}
//给饼图着色 paintPie()只显示饼状图的饼
public void paintPie(Graphics g,int x,int y,int r){
final int CIRCLE_DEGREES = 360;
//确定成员值
int n =getSampleSize();
//计算值之和
double dataSum = 0;
for(int i=0;i<n;i++){
dataSum += getData(i);
}
//从起始原点画一小块
int startAngle = 0;
for(int i=0;i<n;i++){
//饼图分几块画
double percent= getData(i) / dataSum;
int arcAngle = (int)Math.round(percent * CIRCLE_DEGREES);
//给块设置颜色
Color c = getColor(i);
g.setColor(c);
//给块着色
g.fillArc(x,y,2*r,2*r,startAngle,arcAngle);
//纪录下一块的起始点
startAngle += arcAngle;
}
}
//为饼图的图例着色 paintLegend()显示饼状图的相关图例
public void paintLegend(Graphics g,int x,int y,int delta){
//确定成员值
int n = getSampleSize();
for(int i= 0 ; i<n;++i){
//启动当前图例行
Color c = getColor(i);
String s = getName(i)+":"+getData(i);
//为行着色
g.setColor(c);
g.drawString(s,x,y);
//从下一行启动y坐标位置
y += delta;
}
}
}
书上的例子但是不知道为什么不能运行?不知道哪里错了。 程序都没有错误了
下面是 PieChart.java 文件:
//表示一个统计饼图
import java.awt.*;public class PieChart {
//默认特性
private static final Color[] DEFAULT_PIE_COLOR={
Color.red,Color.blue,Color.magenta,Color.black,
Color.green,Color.orange,Color.cyan,Color.pink,
Color.gray,Color.yellow,Color.DARK_GRAY,Color.WHITE };
private static final int DEFAULT_RADIUS = 115; //默认半径
private static final int DEFAULT_LINE_SPACING = 20; //图例相邻两行间默认的间隔像素
private static final Point DEFAULT_PIE_LOCATION //饼状图外接正方形的左上角的默认位置
= new Point(10,10);
private static final Point DEFAULT_LEGEND_LOCATION //图例外接正方形的左上角的默认位置
= new Point(265,35);
//每个饼图的特征
private double[] data ; //表示数据
private String[] name; //数据图例名字
private Color[] color; //数据表示颜色
private int radius; //饼图半径
private Point pieLocation; //饼图位置
private Point legendLocation; //图例位置
private int lineSpacing; //图例间间隙
//PieChart():默认构造方法
public PieChart(double[] d, String[] s ){
this(d, s, DEFAULT_PIE_COLOR);
}
//PieChart()使用构造方法中的数据 名字 颜色
public PieChart(double[] d,String[] s,Color[] c){
if((d.length == 0) || (d.length > s.length)
|| (d.length > c.length)){
System.err.println("Piechart: invalid data");
System.exit(1);
}
data = d;
name = s;
color = c;
radius = DEFAULT_RADIUS;
pieLocation = DEFAULT_PIE_LOCATION;
legendLocation = DEFAULT_LEGEND_LOCATION;
lineSpacing = DEFAULT_LINE_SPACING;
}
//getSampleSize()返回成员数据值
public int getSampleSize(){
return data.length;
}
//getColor()返回该类索代表数据集的第i值的关联颜色
public Color getColor(int i){
return color[i];
}
//getData()返回该类索代表数据集的第i值
public double getData(int i){
return data[i];
}
//getName()返回该类索代表数据集的第i值的关联图例的名字
public String getName(int i){
return name[i];
}
//getRadius()返回饼图的半径
public int getRadius(){
return radius;
}
//getPielocation()返回饼图外接正方形的左上角位置
public Point getPieLocation(){
return pieLocation;
}
//getLegendLocation()返回图例开始显示的位置
public Point getLegendLocation(){
return legendLocation;
}
//getLineSpacing()以像素为单位返回相邻图例各行的间距
public int getLineSpacing(){
return lineSpacing;
}
//paint()给图和图例着色 paint()显示整个饼状图
public void paint(Graphics g){
//获得饼图的特性
int r = getRadius();
Point pie = getPieLocation();
Point legend = getLegendLocation(); //
int spacing = getLineSpacing();
//画图表
paintPie(g,pie.x,pie.y,r);
paintLegend(g,legend.x,legend.y,spacing);
}
//给饼图着色 paintPie()只显示饼状图的饼
public void paintPie(Graphics g,int x,int y,int r){
final int CIRCLE_DEGREES = 360;
//确定成员值
int n =getSampleSize();
//计算值之和
double dataSum = 0;
for(int i=0;i<n;i++){
dataSum += getData(i);
}
//从起始原点画一小块
int startAngle = 0;
for(int i=0;i<n;i++){
//饼图分几块画
double percent= getData(i) / dataSum;
int arcAngle = (int)Math.round(percent * CIRCLE_DEGREES);
//给块设置颜色
Color c = getColor(i);
g.setColor(c);
//给块着色
g.fillArc(x,y,2*r,2*r,startAngle,arcAngle);
//纪录下一块的起始点
startAngle += arcAngle;
}
}
//为饼图的图例着色 paintLegend()显示饼状图的相关图例
public void paintLegend(Graphics g,int x,int y,int delta){
//确定成员值
int n = getSampleSize();
for(int i= 0 ; i<n;++i){
//启动当前图例行
Color c = getColor(i);
String s = getName(i)+":"+getData(i);
//为行着色
g.setColor(c);
g.drawString(s,x,y);
//从下一行启动y坐标位置
y += delta;
}
}
}不知道为什么就是不能运行,Run 那里没有选择运行java应用程序的选项,只有个 运行配置选项,
求指教。
import java.awt.*;
import javax.swing.*;
import java.io.*;public class MusicChart { //main()
public static void main(String srgs) throws IOException {
JFrame w= new JFrame("Music Preferences");
w.setSize(450,280);
w.setVisible(true);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = w.getContentPane();
c.setBackground(Color.white);
Graphics g = c.getGraphics();
double[] choice = {3.2, 10.5, 3.4, 12.1, 11.4, 6.7,
22.4, 10.6, 17.7 };
String[] genre = {"Classical","Country","jazz","Pop",
"Rap and Hip_Hop","Religious","Rock",
"Ueban and R&B","Other"};
System.out.println("Hit enter when ready");
System.in.read();
PieChart chart = new PieChart(choice,genre);
chart.paint(g);
}
}
(就是MUsic Preference,然后里面的东西就是一片空白了。)
至于里面的东西就不能显示了,
前面也有例子是这样的。