Java 图形-如何使用方法创建(intx,inty,intwidth,intheight)和转换(intx,inty)?

Java 图形-如何使用方法创建(intx,inty,intwidth,intheight)和转换(intx,inty)?,java,graphics,awt,translate,java-2d,Java,Graphics,Awt,Translate,Java 2d,我试图做我的计算机科学作业,但我被卡住了,因为我试图使用以下方法 公共图形创建(整数x、整数y、整数宽度、整数高度) 这将基于此图形对象创建新的图形对象,但具有新的平移和剪裁区域 参数: x-x坐标 y-y坐标 宽度-剪切矩形的宽度 高度-剪切矩形的高度。 公共摘要无效翻译(int x,int y) 将图形上下文的原点转换为当前坐标系中的点(x,y) 有人能解释并举例说明如何使用它们吗 我正试着这么做 public Graphics drawPlayer1() { myPencil

我试图做我的计算机科学作业,但我被卡住了,因为我试图使用以下方法

  • 公共图形创建(整数x、整数y、整数宽度、整数高度)

    这将基于此图形对象创建新的图形对象,但具有新的平移和剪裁区域

    参数:

    • x-x坐标
    • y-y坐标
    • 宽度-剪切矩形的宽度
    • 高度-剪切矩形的高度。
  • 公共摘要无效翻译(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(-620300,40,40)时,它向我抛出了一个nullPointerException

    如果还没做完的话,你可以过去。

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

    public Graphics create(int x,int y,int width,int height)
    
    我可以帮你。我认为Graphics2D的其他属性也是独立的。这意味着在第二个图形2d上设置绘画不会影响主图形

    public abstract void translate(int x,int y)
    

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

    在这一点上我和Andrew是一致的,我从未使用过
    图形#创建(int,int,int,int)
    。我确实使用了
    图形#创建

    基本上,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);
    
            }
    
        }
    
    }
    


    你应该查阅甲骨文官方文档。我看了api网站,看不懂。当我尝试使用create(intx,inty,intwidth,intheight)时,我一直得到一个nullPointerException“我如何使用这个方法…”当你尝试使用它时发生了什么?至于零件,1)为什么需要使用它?在10多年的开发过程中,我从未调用过这种方法。2) 我认为你引用的文档说明了这一点。“我一直得到一个nullPointerException”为了更快地获得更好的帮助,请发布一段代码。当调用该方法时,程序就卡住了。我是一名高中生,我只是想在GameLand完成这个作业。我正试图移动我在画板上画的东西,请说明运行该程序时出现了什么问题。我们不需要通读评论来理解问题所在。在有人回答你的问题后,在很大程度上编辑你的问题,这会使答案看起来很糟糕。不是被否决的选民,而是认为这会是一个很好的评论。我做了。。这就是我看到这两种方法的地方。。我只是无法理解“解释和示例”,我认为Java教程是正确的地方。对于任何其他特定的问题,都应该在问题中提及。@SwapnilS“我认为Java教程是正确的地方”-是的,你可能是对的,但这仍然不是一个评论