图形 - 我如何使用的方法创建(INT X,INT Y,INT宽度,高度INT)和翻译(INT X,int y)对? [英] Graphics - How do I use the method create(int x, int y, int width, int height) and translate(int x, int y)?

查看:618
本文介绍了图形 - 我如何使用的方法创建(INT X,INT Y,INT宽度,高度INT)和翻译(INT X,int y)对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做我的计算机科学功课,但我坚持,因为我是尝试使用下面的方法。

I was trying to do my computer science homework but I am stuck as I was trying to use the following methods.


  1. 公共图形创建(INT X,INT Y,INT宽度,高度INT)

这是创建一个新的Graphics对象基于此Graphics对象,但是使用新的转换和剪贴区域。

That Creates a new Graphics object based on this Graphics object, but with a new translation and clip area.

参数:


  • X - x坐标

  • Ÿ - y坐标

  • 宽度 - 剪贴矩形的宽度

  • 的高度 - 剪贴矩形的高度结果。
     

公共抽象无效翻译(INT X,int y)对

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

That Translates the origin of the graphics context to the point (x, y) in the current coordinate system.

谁能解释并给出如何使用它们的例子吗?

Can anyone explain and give examples of how to use them?

我试图做到这一点..

i was trying to do this..

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

和它给我一个NullPointerException异常,当涉及到p1.create(-620,300,40,40);

and it threw me a nullPointerException when it comes to p1.create(-620,300,40,40);

推荐答案

我和安德鲁我在这一个,我从来没有使用图形#创建(INT,INT,INT,INT)。我使用图形#创建虽然。

I'm with Andrew on this one, I've never used Graphics#create(int, int, int, int). I do use Graphics#create though.

基本上,创建方法将创建一个新的图形上下文,它是原始的副本。这使您可以与出影响原有操作的副本。这一点很重要,如果你的显卡是不能(容易)撤消执行操作。

Basically, the create method will create a new graphics context which is a copy of the original. This allows you to manipulate the copy with out effecting the original. This is important if you are performing operations on the graphics that can't be (easily) undone.

翻译简单的0的图形上下文到新的位置。 Swing的绘画过程中做到这一点为它绘制的每个组件。基本上,前油漆被调用时,图形上下文转换为元件的位置,这意味着该组件中的所有绘画是从为0x0完成

Translate simple "zeros" the graphics context to the new location. The Swing painting process does this for each component it paints. Basically, before paint is called, the graphics context is translated to the components position, meaning that all painting within the component is done from 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);

        }

    }

}

这篇关于图形 - 我如何使用的方法创建(INT X,INT Y,INT宽度,高度INT)和翻译(INT X,int y)对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆