Graphic arts. How to use create (int x, int y, int width, int height) and translate (int x, int y) method?

I tried to do my homework in computer science, but I got stuck when I tried to use the following methods.

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

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

    Parameters:

    • x is the x coordinate.
    • y is the y coordinate.
    • width - the width of the clipping rectangle.
    • height - the height of the clipping rectangle.
  • public abstract void translate(int x,int y)

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

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

I tried to do it.

 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 

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

0
source share
3 answers

I'm with Andrew on this, I never used Graphics#create(int, int, int, int) . I am using Graphics#create , though.

Basically, the create method will create a new graphic context, which is a copy of the original. This allows you to manipulate the copy without the effect of the original. This is important if you are performing operations on graphics that cannot be (easily) undone.

Translate the simple β€œzeros” of the graphics context to a new location. The Swing painting process does this for every component that it draws. In principle, before calling paint graphics context is transferred to the position of the components, which means that all painting inside 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 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 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); } } } 
+1
source

You can go through the java tutorial for 2D graphics and javadocs if you haven’t already.

0
source

I'm getting late, but I'll give him a quick shot. An instance of Graphics (or Graphics2D) is an abstraction of a graphics device (such as a printer, screen, etc.). It has borders. Suppose you want to use only a specific area of ​​the device and want the code to always be relative to (0,0) (for example, a game in which a sprite moves around 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 limits output to a subset of the main Graphics2D. Something that

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

will do for you. I think the other Graphics2D attributes are also independent. This means that the Paint setting in the second Graphics2D will not affect the main one.

 public abstract void translate(int x,int y) 

- all about moving the orgine (but not in the direction of the axis). By default, the source will be the upper left corner of the device. This can be changed anywhere on the device. Using the above example of a sprite moving around the screen, just call up the translation where you want to draw it, and then draw it.

0
source

Source: https://habr.com/ru/post/1497807/


All Articles