1. Trang chủ
  2. » Công Nghệ Thông Tin

Java 3D ppsx

6 332 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

public void drawPie(Graphics2D g, PieValue[] slices){ // Góc trên, trái và chiều dài, rộng của hình chữ nhật int top = 100, left = 100, width = 200, height = 200; // total: tổng độ lớn của các cung double total = 0.0D; // Xác định total thông qua duyệt qua các cung trong mảng for(int j=0; j<slices.length; j++) { total += slices[j].value; } // curValue: giá trị của cung hiện tại double curValue = 0.0D; // startAngle: góc bắt đầu của cung hiện tại int startAngle = 0; // Duyệt qua các cung trong mảng và vẽ từng cung một for(int j=0; j<slices.length; j++) { // Tìm góc bắt đầu startAngle = (int)(curValue*360/total); // Tìm góc kết thúc int sweepAngle = (int)(slices[j].value*360/total); // Đảm bảo cho cung cuối cùng luôn khít với cung đầu tiên if(j == slices.length-1) { sweepAngle = 360 - startAngle; } // Thiết lập màu vẽ từng cung g.setColor(slices[j].color); // Vẽ hình chữ nhật bao quanh biểu đồ (cho dễ nhìn) // g.drawRect(top, left, width, height); // Vẽ cung tròn (tiếp giáp với hình chữ nhật có góc trên // là 100,100; chiều rộng là 200, chiều cao là 200 g.fillArc(top, left, width, height, startAngle, sweepAngle); // Góc để vẽ nhãn int textAngle = (int)((startAngle+sweepAngle)*3.14/180); // r: bán kính của một cung tròn int r = width/2; // ox, oy: gốc tọa độ mới int ox = left + (width/2); int oy = top + (height/2); // x,y: tọa độ vẽ nhãn int x = (int)(r*Math.cos(textAngle)); int y = (int)(r*Math.sin(textAngle)); g.setColor(Color.black); // Vẽ nhãn g.drawString(String.valueOf(slices[j].value), ox + x, oy - y); // Tăng giá trị hiện tại để vẽ cung tiếp theo curValue += slices[j].value; } } package chart; import java.awt.*; import java.awt.event.*; import javax.swing.*; /*+ + | Lop PieValue | + +*/ class PieValue { double value; Color color; public PieValue(double value, Color color) { this.value = value; this.color = color; } } /*+ + | Lop Pie | + +*/ public class Pie extends JFrame { PieValue[] slices = new PieValue[4]; /*+ + | Constructor | + +*/ Pie() { // Cac gia tri thu nghiem slices[0] = new PieValue(25, Color.red); slices[1] = new PieValue(33, Color.green); slices[2] = new PieValue(20, Color.pink); slices[3] = new PieValue(15, Color.blue); setSize(400,400); setVisible(true); } /*+ + | Ham ve | + +*/ public void paint(Graphics g) { drawPie((Graphics2D)g, slices); } /*+ + | Ham ve do thi | + +*/ public void drawPie(Graphics2D g, PieValue[] slices){ int top = 100, left = 100, width = 200, height = 200; double total = 0.0D; for(int j=0; j<slices.length; j++) { total += slices[j].value; } double curValue = 0.0D; int startAngle = 0; for(int j=0; j<slices.length; j++) { startAngle = (int)(curValue*360/total); int sweepAngle = (int)(slices[j].value*360/total); if(j == slices.length-1) { sweepAngle = 360 - startAngle; } g.setColor(slices[j].color); g.fillArc(top, left, width, height, startAngle, sweepAngle); int textAngle = (int)((startAngle+sweepAngle)*3.14/180); int r = width/2; int ox = left + (width/2); int oy = top + (height/2); int x = (int)(r*Math.cos(textAngle)); int y = (int)(r*Math.sin(textAngle)); g.setColor(Color.black); g.drawString(String.valueOf(slices[j].value), ox + x, oy - y); curValue += slices[j].value; } } /*+ + | Ham main() | + +*/ public static void main(String args[]) { Pie app = new Pie(); // Dinh nghia su kien khi ket thuc chuong trinh app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } } public void drawPie(Graphics2D g, PieValue3D[] slices){ // Góc trên, trái và chiều dài, rộng của hình chữ nhật int top = 100, left = 150, width = 200, height = 100; double total = 0.0D; for(int j=0; j<slices.length; j++) { total += slices[j].value; } // // Vẽ bóng của các cung tròn // // Độ sâu của bóng int depth = 10; double curValue = 0.0D; for(int x = depth; x>=1; x ) { int startAngle = 0; for(int j=0; j<slices.length; j++) { startAngle = (int)(curValue*360/total); int sweepAngle = (int)(slices[j].value*360/total); if(j == slices.length-1) { sweepAngle = 360 - startAngle; } // Vẽ cung với màu tối g.setColor(Color.darkGray); // Chiều rộng lớn hơn chiều cao width > height g.fillArc(top, left + x, width, height, startAngle, sweepAngle); curValue += slices[j].value; } } // curValue = 0.0D; int startAngle = 0; for(int j=0; j<slices.length; j++) { startAngle = (int)(curValue*360/total); int sweepAngle = (int)(slices[j].value*360/total); if(j == slices.length-1) { sweepAngle = 360 - startAngle; } g.setColor(slices[j].color); // Chiều rộng nhỏ hơn chiều cao width > height g.fillArc(top, left, width, height, startAngle, sweepAngle); curValue += slices[j].value; } } . vẽ cung tiếp theo curValue += slices[j].value; } } package chart; import java. awt.*; import java. awt.event.*; import javax.swing.*; /*+ + | Lop PieValue | + +*/ class PieValue { double value; Color. windowClosing(WindowEvent e) { System.exit(0); } } ); } } public void drawPie(Graphics2D g, PieValue3D[] slices){ // Góc trên, trái và chiều dài, rộng của hình chữ nhật int top = 100, left = 150,

Ngày đăng: 05/07/2014, 11:20

Xem thêm: Java 3D ppsx

TỪ KHÓA LIÊN QUAN

w