intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Java 3D

Chia sẻ: Le Trung Dung | Ngày: | Loại File: DOC | Số trang:6

104
lượt xem
23
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Java 3D

  1. 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
  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];
  3. /*+---------------+ | 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
  4. 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); } } ); } }
  5. 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=1; x--) { int startAngle = 0; for(int j=0; j height g.fillArc(top, left + x, width, height, startAngle, sweepAngle); curValue += slices[j].value; } }
  6. //-------------------------------- curValue = 0.0D; int startAngle = 0; for(int j=0; j height g.fillArc(top, left, width, height, startAngle, sweepAngle); curValue += slices[j].value; } }
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2