Lớp Graphics
- Trong Java, vẽ diễn ra qua đối tượng Graphics, instance của java.awt.Graphics.
- Mỗi Graphics có hệ tọa độ riêng, mọi phương thức vẽ String, đường thẳng, chữ nhật, tròn, đa giác.
- Truy cập Graphics qua phương thức paint(Graphics g).
- Dùng drawRoundRect() với tham số x, y, width, height, arcWidth, arcHeight để vẽ chữ nhật bo góc.
Ví dụ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RoundedRectangleTest extends JFrame {
public RoundedRectangleTest() {
setTitle("RoundedRectangle Test");
setSize(350, 275);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawRoundRect(10, 50, 150, 150, 50, 30); // to draw a rounded rectangle.
}
public static void main(String[] args) {
new RoundedRectangleTest();
}
}
Kết quả
