package com.han;
import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Graphics_4 extends JFrame {
public Graphics_4() {
// TODO Auto-generated constructor stub
Canvas canvas = new Canvas() {
@Override
public void paint(Graphics g) {
// super.paint(g); // for the one containing children components, this is necessary.
int[] xPoints = { 10, 50, 10, 50 };
int[] yPoints = { 10, 10, 50, 50 };
// int nPoints = 3;
int nPoints = 4;
g.drawPolygon(xPoints, yPoints, nPoints);
int[] xPoints2 = { 60, 100, 100, 60 };
int[] yPoints2 = { 10, 10, 50, 50 };
int nPoints2 = 4;
g.drawPolygon(xPoints2, yPoints2, nPoints2);
g.drawRect(110, 10, 40, 40);
g.drawRoundRect(160, 10, 40, 40, 10, 10);
}
};
this.add(canvas); // add()方法最早出现在Container中,所以我们无法在Canvas中添加组件。
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Graphics_4 frame = new Graphics_4();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Draw polygon");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Java Graphics_4
最新推荐文章于 2025-09-09 08:37:13 发布