package awt; import java.awt.*; import java.awt.geom.*; import javax.swing.*; import awt.DrawCircle.DrawPanel; public class DrawFrame extends JFrame{ public DrawFrame() { super(); initialize(); } private void initialize() { this.setSize(300,200); //设置窗体大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭模式 setContentPane(new CanvasPanel());//设置窗体面板为绘图面板对象 this.setTitle("绘图实例");//设置标题 } public static void main(String[]args) { new DrawFrame().setVisible(true); } class CanvasPanel extends JPanel{ public void paint(Graphics g) { super.paint(g); Graphics2D g2=(Graphics2D)g;//强制转换成2D类型 g.setColor(Color.cyan);//setColor方法设置颜色 Shape[] shapes=new Shape[4]; //声明圆形和矩形对象 shapes[0]=new Ellipse2D.Double(5,5,100,100); shapes[1]=new Rectangle2D.Double(110,5,100,100); shapes[2]=new Rectangle2D.Double(15,15,80,80); shapes[3]=new Ellipse2D.Double(120,15,80,80); for(Shape shape:shapes) { Rectangle2D bounds=shape.getBounds2D(); if(bounds.getWidth()==80) g2.fill(shape);//填充图形 else g2.draw(shape);//绘制图形 } } } }
绘制出来的图片效果如下 图片性状 颜色 大小 位置都可以自己调节 主要用awt这个包做