java 画图的问题
知道Graphics类是抽象类,但是通过JPanel.getGraphics()或JComponent.getGraphics()可以得到Graphics类的一个实例,但是JPanel.getGraphics()或JComponent.getGraphics()是怎么实现的呢?
或者说明怎样用另外的类来得到一个抽象类的实例,谢谢了。
另外下面的程序中的public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillArc(x,y, d, d,0,180);
g.setColor(Color.WHITE);
g.fillArc(x,y, d, d,180,180);
g.setColor(Color.BLACK);
g.fillArc(x+d/2, y+d/4, d/2, d/2,180,180);
g.setColor(Color.WHITE);
g.fillArc(x, y+d/4, d/2, d/2,0,180);
g.fillOval(x+d*3/4-5, y+d/2-5, 10,10);
g.setColor(Color.BLACK);
g.fillOval(x+d/4-5, y+d/2-5, 10,10);
}
这段代码中Graphics没有实例化,但是为什么可以调用它的方法呢?
急需知道,拜托了,谢谢。
import javax.swing.*;
import java.awt.*;
public class Dao extends JFrame
{
int width=400;
int height=300;
public Dao()
{
setSize(width,height);//设置框架大小
setTitle("道");//设置框架标题
//将框架显示在屏幕正中
Toolkit kit= Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int x=(screenSize.width-width)/2;
int y=(screenSize.height-height)/2;
setLocation(x,y);//设置框架位置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
Dao frame=new Dao();
DrawPanel drawPanel=new DrawPanel( );
//把其它组件添加到面板中;
frame.setContentPane(drawPanel);
frame.setVisible(true);
}
}
class DrawPanel extends JPanel
{
int x=100;
int y=40;
int d=200;
public DrawPanel()
{
setBackground(Color.GRAY);
}
//在面板中绘制太极图形;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillArc(x,y, d, d,0,180);
g.setColor(Color.WHITE);
g.fillArc(x,y, d, d,180,180);
g.setColor(Color.BLACK);
g.fillArc(x+d/2, y+d/4, d/2, d/2,180,180);
g.setColor(Color.WHITE);
g.fillArc(x, y+d/4, d/2, d/2,0,180);
g.fillOval(x+d*3/4-5, y+d/2-5, 10,10);
g.setColor(Color.BLACK);
g.fillOval(x+d/4-5, y+d/2-5, 10,10);
}
}