Graphics - How do I use the method create(int x, int y, int width, int height) and translate(int x, int y)?

后端 未结 3 1681
有刺的猬
有刺的猬 2021-01-28 12:15

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

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

相关标签:
3条回答
  • 2021-01-28 12:51

    You can go through the java tutorial for 2D graphics and javadocs if not done already.

    0 讨论(0)
  • 2021-01-28 13:01

    It's getting late for me but I'll give it a quick shot. A Graphics (or Graphics2D) instance is an abstraction of an graphics device (e.g. printer, screen, etc.) It has a bounds. Let's say you want to draw into only a specific area of the device and you want the code to always be relative to (0,0) (e.g. a game where a sprite moves across the screen). The sprite will always be the same but its location will be different. One way to achieve this is to create a Graphics2D that restricts output to a subset of the main Graphics2D. That's what

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

    will do for you. I think other attributes of the Graphics2D are independent as well. This means setting the Paint on the second Graphics2D will not affect the main one.

    public abstract void translate(int x,int y)
    

    is all about moving the orgin (but not the direction of the axis). By default the origin will be the upper-left corner of the device. This can be changed to be anywhere within the device. Using the above example of a sprite moving across the screen just call translate where you want it drawn, and then draw it.

    0 讨论(0)
  • 2021-01-28 13:04

    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.

    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

    enter image description here

    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);
    
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题