Graphics – 如何使用方法create(int x,int y,int width,int height)和translate(int x,int y)?

我正在尝试做我的计算机科学作业,但是当我试图使用以下方法时,我被卡住了。

  1. public Graphics create(int x,int y,int width,int height)

    即创建基于此Graphics对象的新Graphics对象,但具有新的转换和剪辑区域。

    参数:

    • x – x坐标。
    • y – y坐标。
    • width – 剪切矩形的宽度。
    • height – 剪切矩形的高度。
  2. public abstract void translate(int x,int y)

    将图形上下文的原点转换为当前坐标系中的点(x,y)。

任何人都可以解释并举例说明如何使用它们吗?

我试图这样做..

 public Graphics drawPlayer1() { myPencil.up(); myPencil.move(-620,300); myPencil.down(); myPencil.fillCircle(20); myPencil.up(); myPencil.move(-590,300); myPencil.drawString("Player1: " + player1); p1.create(-620,300,40,40); return p1; }//end drawPlayer1 

当它涉及到p1.create(-620,300,40,40)时,它给我一个nullPointerException;

我和安德鲁在这一个,我从来没有使用过Graphics#create(int, int, int, int) 。 我确实使用Graphics#create #create。

基本上,create方法将创建一个新的图形上下文,它是原始的副本。 这使您可以在不影响原件的情况下操纵副本。 如果您对无法(轻松)撤消的图形执行操作,这一点很重要。

将图形上下文的简单“零”翻译为新位置。 Swing绘画过程为它绘制的每个组件执行此操作。 基本上,在调用paint之前,图形上下文被转换为组件位置,这意味着组件内的所有绘制都是从0x0完成的

在此处输入图像描述

 public class TestGraphics01 { public static void main(String[] args) { new TestGraphics01(); } public TestGraphics01() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestGraphicsPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestGraphicsPane extends JPanel { @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); FontMetrics fm = g.getFontMetrics(); // This creates a "copy" the graphics context, it's translated // to the x, y position within the current graphics context // and has a width and height. If the width or height is outside // the current graphics context, then it is truncated... // It's kind of like clip, except what ever you do to this copy // does not effect the graphics context it came from... // This would be simular to setting the clipping region, just it // won't effect the parent Graphics context it was copied from... Graphics create = g.create(100, 100, 200, 200); create.setColor(Color.GREEN); create.fillRect(0, 0, 200, 200); create.setColor(Color.YELLOW); create.drawString("I'm inside...", 0, fm.getAscent()); create.dispose(); // But I remain uneffected... g.drawString("I'm outside...", 0, fm.getAscent()); // I will effect every thing draw afterwards... g.setColor(Color.RED); int y = 50 - (fm.getHeight() / 2) + fm.getAscent(); g.translate(50, y); g.drawString("I'm half way", 0, 0); // You must reset the translation if you want to reuse the graphics OR // you didn't create a copy... g.translate(-50, -y); y = 350 - (fm.getHeight() / 2) + fm.getAscent(); g.translate(300, y); g.drawString("I'm half way", 0, 0); // You must reset the translation if you want to reuse the graphics OR // you didn't create a copy... g.translate(-300, -y); } } } 

如果没有完成,你可以浏览2D图形和javadocs 的java教程 。

对我来说已经很晚了,但我会给它一个快速的镜头。 Graphics(或Graphics2D)实例是图形设备的抽象(例如打印机,屏幕等)。它有一个边界。 假设您想要仅绘制设备的特定区域,并且您希望代码始终相对于(0,0)(例如,精灵在屏幕上移动的游戏)。 精灵将始终相同,但其位置将不同。 实现此目的的一种方法是创建一个Graphics2D,将输出限制为主要Graphics2D的子集。 那是什么

 public Graphics create(int x,int y,int width,int height) 

会为你做的。 我认为Graphics2D的其他属性也是独立的。 这意味着在第二个Graphics2D上设置Paint不会影响主要的。

 public abstract void translate(int x,int y) 

是关于移动orgin(但不是轴的方向)。 默认情况下,原点将是设备的左上角。 这可以更改为设备内的任何位置。 使用上面的精灵在屏幕上移动的例子,只需在你想要绘制的地方调用translate,然后绘制它。