Java绘图

绘图可以在应用程序中绘制图片、文字、图形等,在Java中主要可以使用Graphics和Graphics2D进行绘图,其中Graphics类是所有图形上下文的抽象基类,而Graphics2D就继承了Graphics类。而Graphics功能没有Graphics2D强大,Graphics2D是Graphics的扩展。

构造一个新的Graphics2D对象,因为Graphics2D是抽象类,所以无法直接创建对象,因很多绘图对象是Graphics类的实例,所以这里使用了强制转换。

public void paint(Graphics gp){

          super.paint(gp);

          Graphics2D gp2d = (Graphics2D) gp;

使用Graphics2D进行绘制图形

首先创建一个Demo1类继承JFrame类成为窗体,以便显示效果,在Demo1里创建一个内部类Plot类,该类继承JPanel类(容器,把图形放在这个容器里),

public class Demo1 extends JFrame{

    private static final long serialVersionUID = 1L;

    //内部类 创建绘图面板

    class Plot extends JPanel{
         private static final long serialVersionUID = 1L;
}

Plot内部类重写panit()方法,再创建一个BasicStroke对象,Graphics2D可以调用setStroke方法设置笔画的属性,而这个方法要接受一个Stroke接口的实现类作为参数,这样可以通过不同的构造方法创建笔画属性的不同对象。

class Plot extends JPanel{

      public void paint(Graphics gp){
          //笔画属性,
          BasicStroke bs = new BasicStroke(10.1f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL);
          super.paint(gp);
          Graphics2D gp2d = (Graphics2D) gp;

}
}

paint方法里面进行绘图,(设置绘图颜色后,后面绘图都是采取这颜色作为前景色,如果需要更改为其他颜色的话,可以再次调用setColor()方法进行再次设置)

class Plot extends JPanel{

      private static final long serialVersionUID = 1L;

        //绘制方法
      public void paint(Graphics gp){
          //笔画属性,
          BasicStroke bs = new BasicStroke(10.1f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL);

          super.paint(gp);

          Graphics2D gp2d = (Graphics2D) gp;
           //设置画笔颜色
          gp2d.setColor(Color.blue);

          //第一个点的x坐标,第一个点的y坐标,第二个点的x坐标,第二点的坐标
          gp2d.drawLine(30, 30, 80, 30);

          //绘制矩形的x坐标,绘制矩形的y坐标,矩形的宽度,矩形的高度
          gp2d.drawRect(10,50,100,50);

          //绘制矩形的x坐标,绘制矩形的y坐标,矩形的宽度,矩形的高度,4个角弧度的水平直径,4个角弧度的垂直直径
          gp2d.drawRoundRect(10, 120, 100, 70, 12, 12);

          gp2d.setColor(Color.black);

          gp2d.fillOval(20, 130, 30, 20);

          gp2d.fillOval(70, 130, 30, 20);

          gp2d.setColor(Color.gray);

          gp2d.fillArc(35, 160, 40, 25, 100, 270);

          Color c1 = new Color(22, 147, 140);//创建红绿蓝不透明的srgb颜色

          gp2d.setColor(c1);

          gp2d.drawOval(10, 200, 100, 60);//绘制椭圆的边框。

          //Graphics2D d=(Graphics2D) gp;

          gp2d.fill3DRect(150, 30, 100, 80, false);

          gp2d.fillOval(150,130, 100, 80);

          gp2d.setStroke(bs);//设置笔画属性

          gp2d.drawLine(300, 80, 300, 200);

      }

   }

Demo类定义一个初始化窗体的方法,设置窗体大小,关闭窗体模式以及设置内容窗格等

//初始化窗体
   private void initialize(){

      this.setSize(800, 600);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭模式

      setContentPane(new Plot());

      this.setTitle("绘图小Demo");

      this.setLocationRelativeTo(null);//窗体居中

      this.setVisible(true);//设置窗体的可见性

   
   }

Demo类构造器 调用初始化方法

public Demo1(){
      super();
      initialize();
   }

主方法 创建实例

public static void main(String[] args) {
      new Demo1();
   }

效果:

绘制图片:在图片上绘制字符串并输出成图片格式

public static void main(String[] args) throws FileNotFoundException, IOException {

      // TODO Auto-generated method stub

      //获取图片缓冲区

       BufferedImage bi = new BufferedImage(80,40,BufferedImage.TYPE_INT_RGB);

       //得到绘制坏境(这张图片的笔)

       Graphics gh= (Graphics)bi.getGraphics();

       gh.setColor(Color.WHITE);//设置颜色

       gh.fillRect(0, 0, 80, 40);//填充整张图片(其实就是设置背景色)

       gh.setColor(Color.lightGray);

       //gh.drawRoundRect(0, 0, 130, 70, 130, 70);//绘制一个圆形边框

       gh.drawRect(0, 0, 80-1, 40-1);//绘制一个四边形边框

       gh.setFont(new Font("Bernard MT",Font.BOLD,18));//字体样式 字体格式 字体大小

       gh.setColor(Color.BLUE);//设置字体颜色

       gh.drawString(getNum(10,100), 22, 25);//向图片上写随机字符串

       ImageIO.write(bi, "PNG", new FileOutputStream("D:/test.png"));//把图片输出到指定路径
}

随机生成字符串方法

public static String getNum(int num1,int num2){

       //返回num1到num2之间的随机数
      Integer rand = num1 + (int)(Math.random()*(num2-num1));

      //System.out.println(rand);

      int a = (int)(Math.random()*100);

      //返回a~z随机的字符

      char char1 = (char)('a'+Math.random()*('z'-'a'+1));

      //返回A~Z随机的字符

      char char2 = (char)('a'+Math.random()*('Z'-'A'));

      return rand.toString()+char1+char2;

   }

运行输出

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值