Java工具類之Graphics,利用重寫paint()方法繪畫出一個坐標軸:
package huaxian;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class huaxian2 extends JFrame {
private JPanel jp;
private JFrame jf;
public static void main(String[] args) {
// TODO Auto-generated method stub
huaxian2 h2 = new huaxian2();
}
public huaxian2(){
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jp = new JPanel();
this.add(jp);
}
@Override
public void paint(Graphics g){
super.paint(g);
g.drawLine(50, 350, 498, 350);
g.drawLine(50, 50, 50, 350);
}
}
點擊運行結果: