简体   繁体   English

Java-摇摆-图形2D

[英]Java - Swing - Graphics 2D

public class Points extends JPanel {

  public void paintComponent(Graphics g) {
  super.paintComponent(g);

  Graphics2D g2d = (Graphics2D) g;

  g2d.drawLine(60, 20, 80, 90);
 }
}

I'm not really sure what's the Graphics2D g2d = (Graphics2D) g; 我不太确定Graphics2D g2d = (Graphics2D) g; supposed to do. 应该做的。

It's just a plain JPanel that's later added onto a JFrame. 这只是一个普通的JPanel,后来又添加到了JFrame中。

It would be really helpfull if anyone could give me some advice as I'm stuck at this line of the code for a long time now. 如果任何人都可以给我一些建议,那将是非常有帮助的,因为我在很长一段时间内一直停留在这一行代码中。

It's a problem of compatibility with older Java code. 这是与旧版Java代码兼容的问题。

Graphics2D , as explained in documentation, is a class that inherits from Graphics and provides some additional graphic features: in short Graphics2D is a more powerful Graphics . Graphics2D ,如文档解释,是从继承的类Graphics ,并提供一些额外的图形功能:在短Graphics2D是一个更强大的Graphics

Now, the method paintComponent(Graphics g) exists from before Graphics2D so even if with current Java the Graphics which is under the hood of a JPanel is a Graphics2D , the signature hasn't been changed to break existing code. 现在,方法paintComponent(Graphics g)Graphics2D之前存在,因此即使在当前的Java中,位于JPanel之下的Graphics2DGraphics2D ,也不会更改签名来破坏现有代码。

At runtime the g passed is a Graphics2D but you need to cast it so that you will be allowed to call more advanced operations on it. 在运行时,传递的gGraphics2D但是您需要对其进行强制转换,以便可以对其进行更高级的操作。

The statement 该声明

Graphics2D g2d = (Graphics2D) g;

just casts the Graphics object to a Graphics2D . 只是将Graphics对象转换为Graphics2D It's used to access the methods provided by Graphics2D . 它用于访问Graphics2D提供的方法。 In this case it is unnecessary as Graphics also has a drawLine method so if you don't have a requirement for the more advanced methods such as rotate and translate , you can use 在这种情况下,没有必要,因为Graphics也具有drawLine方法,因此,如果您不需要更高级的方法(例如rotatetranslate ,则可以使用

@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.drawLine(60, 20, 80, 90);
}

It casts the graphics context into a Graphics2D object. 它将图形上下文转换为Graphics2D对象。 This is useful because Graphics2D allows for rotations, transformations, antialiasing, etc not possible with a normal Graphics object. 这是很有用的,因为Graphics2D允许进行常规Graphics对象无法进行的旋转,变换,抗锯齿等。 All of the methods available in Graphics are still available to you when you use Graphics2D . 使用Graphics ,仍然可以使用“ Graphics2D可用的所有方法。

您正在将g强制转换为Graphics2D以便可以使用Graphics2D类中的高级功能。

That just casts your Graphics object into a Graphics2D object. 这只是将您的Graphics对象转换为Graphics2D对象。 Graphics2D has a lot of features that Graphics doesn't provide that are very useful for painting 2D graphics. Graphics2D具有Graphics无法提供的许多功能,这些功能对于绘制2D图形非常有用。 Check out the documentation for Graphics2D here: 在此处查看Graphics2D的文档:

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

Also, according to this other question , that cast should always be safe to do. 另外,根据另一个问题 ,该转换应该始终是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM