0

私はコンピュータサイエンスの宿題をしようとしていましたが、次の方法を使おうとしていたために行き詰まりました。

  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がスローされました。

4

3 に答える 3

1

私はこれについてアンドリューと一緒です、私はこれまで使ったことがありませんGraphics#create(int, int, int, int)。私はGraphics#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);

        }

    }

}
于 2012-12-31T01:04:15.530 に答える
0

まだ行っていない場合は、2DグラフィックスjavadocsのJavaチュートリアルを実行できます。

于 2012-12-30T05:52:16.857 に答える
0

遅くなっていますが、簡単に説明します。グラフィックス(またはGraphics2D)インスタンスは、グラフィックスデバイス(プリンター、画面など)を抽象化したものです。これには限界があります。デバイスの特定の領域のみに描画し、コードを常に(0,0)に相対させたいとします(たとえば、スプライトが画面上を移動するゲーム)。スプライトは常に同じですが、その場所は異なります。これを実現する1つの方法は、出力をメインのGraphics2Dのサブセットに制限するGraphics2Dを作成することです。それが

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

あなたのためになります。Graphics2Dの他の属性も独立していると思います。つまり、2番目のGraphics2Dにペイントを設定しても、メインのペイントには影響しません。

public abstract void translate(int x,int y)

原点を動かすことがすべてです(軸の方向ではありません)。デフォルトでは、原点はデバイスの左上隅になります。これは、デバイス内のどこにでも変更できます。画面上を移動するスプライトの上記の例を使用して、描画したい場所にtranslateを呼び出してから、描画します。

于 2012-12-30T06:22:34.323 に答える