账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    BufferedImage什么时候用getGraphics什么时候又用createGraphics()
    25
    0

    我知道一个是创建 Graphics2D对象,一个是获取一个 Graphics2D对象。

    那两个有什么区别?

    我看代码里都有

    public void init(){

     int width=480,hight=720;  
     image = new BufferedImage(width,hight,BufferedImage.TYPE_INT_RGB);  
     //获取图形上下文 
     graphics = (Graphics2D)image.getGraphics();  

    }

    java Graphics2D 画图

    (1):创建一个Graphics2D,可以画到BufferedImage中。

    BufferedImage buf=new
    BufferedImage(img.getWidth(this),img.getHeight(this),BufferedImage.TYPE_INT_ARGB);

    创建一个临时Graphics2D对象。

    Graphics tmpG=buf.createGraphics();

    将图像画入临时缓冲中:

    tmpG.drawImage(img,10,10,this);

    JAVA---Graphics2D类


    看起来都是初始化阶段使用的,之间也没什么区别麻

    谢谢。

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 孤单心事 普通会员 1楼

      在Java中,BufferedImagecreateGraphics()是两种用于绘制图像的类,它们在以下情况下使用:

      1. BufferedImage

      2. 作为绘图对象:

      当你创建一个BufferedImage对象时,你首先创建了一个Graphics2D对象,这是Java图像处理库中的一个对象,用于在二维图形上进行绘图操作。BufferedImage对象提供了以下方法用于绘制图像:

       - `getGraphics()`:返回当前的`Graphics2D`对象,你可以通过调用`Graphics2D.getInstance()`方法来获取它,如以下示例:
      
           ```java
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           Graphics2D g = image.getGraphics();
           // 绘制图像
           g.drawImage(image, 0, 0, width, height, null);
           g.dispose();
           ```
      
       - `createGraphics()`:创建一个新的`Graphics2D`对象,可以用于在`BufferedImage`上绘制新的图像。`createGraphics()`方法接受一些参数,如`src`(可选),表示要绘制的图像的源,`width`和`height`(可选)表示图像的宽度和高度,`format`(可选)表示图像的格式(例如,`RGB`,`BGR`等),`alpha`(可选)表示透明度,`paint`(可选)表示在绘图上下文中调用的绘图方法(例如,`drawImage`,`fillRect`等)。
      
       - 使用`createGraphics()`方法的示例:
      
           ```java
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           g = image.createGraphics();
           g.drawImage(image, 0, 0, width, height, null);
           g.dispose();
           ```
      
      1. createGraphics()

      2. 在创建BufferedImage对象时使用:

      在创建BufferedImage对象时,如果你不提供getGraphics()方法的参数,createGraphics()方法将创建一个新的Graphics2D对象,该对象将被存储在BufferedImage对象的Graphics属性中。例如:

           ```java
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           image.createGraphics();
           // 将当前的`Graphics`属性设置为新创建的`Graphics2D`对象
           image.setGraphics(g);
           ```
      
      • BufferedImage对象的getGraphics()方法中使用:

      BufferedImage对象的getGraphics()方法中,你可以在一个图形上下文中使用Graphics属性,如以下示例:

           ```java
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           g = image.getGraphics();
           // 绘制图像
           g.drawImage(image, 0, 0, width, height, null);
           g.dispose();
           image.setGraphics(g);
           // 在另一个图形上下文中使用`Graphics`属性
           g = image.getGraphics();
           g.drawImage(image, 0, 0, width, height, null);
           g.dispose();
           ```
      

      注意,getGraphics()方法和createGraphics()方法在创建BufferedImage对象时是默认开启的,即创建一个既保留了当前Graphics属性,也保存了新创建的Graphics2D对象的图形。如果你在绘制图像后想获取当前的Graphics属性,你可以调用getGraphics()方法;如果你想创建一个新的Graphics2D对象,并在图形上下文中使用它,你可以调用createGraphics()方法。

    更多回答
    扫一扫访问手机版