?? rect.java
字號:
package rect;
import java.awt.image.*;
import java.awt.*;
import java.awt.geom.*;
import ajax.DataExchange;
public class Rect {
private String[] book ={"JAVA",".NET","C","PB","PASCAL"};
private int[] month={1,2,3,4,5,6};
private int[] sellQuantity = new int[month.length];
private Color[] bookColor = {
new Color(230,111, 71),
new Color(107,165,239),
new Color(200,200,100),
new Color(120,100,200),
new Color(220,100,220)};
//獲取銷售量
public int[] getData(int month){
DataExchange de=new DataExchange();
return de.getData(month);
}
public BufferedImage drawRectCube(){
int width = 500, height = 375;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 創(chuàng)建Java2D對象
Graphics2D g2d = image.createGraphics();
// 打開反鋸齒功能
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 填充整個背景
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
// 繪制陰影,由灰色漸進圓角矩形組成
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height, new Color(218, 214, 212), false);
g2d.setPaint(grayGP);
RoundRectangle2D.Float bgRR = new RoundRectangle2D.Float(5, 5, width-5, height-5 , 50, 50);
g2d.fill(bgRR);
// 繪制漸進藍色圓角矩形背景
GradientPaint blueGP = new GradientPaint(0, 0, new Color(14, 97, 147), 0, height, new Color(240, 243, 247), false);
g2d.setPaint(blueGP);
g2d.fillRoundRect(0, 0, width-5, height-5, 50, 50);
// 繪制深藍色圓角矩形輪廓
BasicStroke bs = new BasicStroke(1.2f);
g2d.setStroke(bs);
g2d.setPaint(new Color(55,71, 105));
g2d.drawRoundRect(0, 0, width-5, height-5, 50, 50);
// 繪制圖表繪圖區(qū)域背景的陰影效果
Rectangle2D.Float drawArea = new Rectangle2D.Float(63, 48, 400, 300);
g2d.setPaint(Color.GRAY);
g2d.fill(drawArea);
// 填充圖表繪圖區(qū)域背景
g2d.setPaint(Color.WHITE);
drawArea = new Rectangle2D.Float(60, 45, 400, 300);
g2d.fill(drawArea);
// 描繪圖表繪圖區(qū)域的輪廓
g2d.setPaint(Color.BLACK);
g2d.draw(drawArea);
// 繪制圖表標(biāo)題
String chartTitle = "2007年上半年計算機類圖書銷售統(tǒng)計圖";
g2d.setFont(new Font("華文隸書", Font.PLAIN, 25));
int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
g2d.setColor(Color.WHITE);
g2d.drawString(chartTitle, (width-stringLength)/2, 25);
// 創(chuàng)建虛線筆劃
float[] dashes = {3.f};
bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, 0);
g2d.setStroke(bs);
String str = "";
stringLength = 0;
g2d.setPaint(Color.BLACK);
g2d.setFont(new Font("宋體", Font.PLAIN, 12));
int mLength = month.length;
int mCell = 400/(mLength+1);//每個月之間的間距
for (int i = 1; i <= mLength; i++) {
// 繪制垂直方向虛線
g2d.drawLine(60+i * mCell, 45, 60+i * mCell, 345);
// 繪制橫軸上月份的說明文字
str += i + "月";
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 60+i*mCell-stringLength/2, 360);
// 重置月份說明文字
str = "";
}
g2d.setFont(new Font("Arial", Font.BOLD, 14));
str = "";
for (int i = 0; i <= 300; i += 30) {
// 繪制水平方向虛線
g2d.drawLine(60, 45+i, 460, 45+i);
// 繪制縱軸上銷售量的說明文字
str += 100-i/3;
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 55-stringLength, 45+i+5);
str = "";
}
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
g2d.setStroke(new BasicStroke());
Rectangle2D.Double bar ;
for (int i =0; i <month.length; i++) {
// 初始化繪制數(shù)據(jù)
int bookSales = 0;
sellQuantity = getData(i+1);
for (int j = 0; j < book.length; j++) {
bookSales = sellQuantity[j+1]*3;//1 + Math.random() * 295;
int x = 60+mCell*(i)+(mCell-30)+j*10;
int y = 345 - bookSales ;
// 填充直方圖陰影
bar = new Rectangle2D.Double(x+4, y+2, 10, bookSales-2);
g2d.setPaint(Color.GRAY);
g2d.fill(bar);
bar = new Rectangle2D.Double(x,y, 10, bookSales);
GradientPaint drawGP = new GradientPaint(x, y, bookColor[j], x, 345, bookColor[j].brighter(), false);
// 填充直方圖
g2d.setPaint(drawGP);
g2d.fill(bar);
// 描繪直方圖輪廓
g2d.setPaint(Color.BLACK);
g2d.draw(bar);
if(i==1) {
// 繪制圖例
g2d.setColor(bookColor[j]);
g2d.fillRect(418, 50+j * 12, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(book[j], 432, 60+j * 12);
}
}
}
return image;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -