简体   繁体   中英

Changing the co-ordinate system in Acm(java)

I am trying to create a circle with co ordinate (0,0) at its center but I'm unable to shift the origin from top left corner to the center. Is there any way I can do this?

在此处输入图像描述

Here's the picture showing the origin at top left but I want it in center.

public class circle extends GraphicsProgram{
    
    public int size =200;
  
    
    
    @Override
    public void run(){
        setSize(300, 300);

        GOval circle = new GOval(0,0,size*2,size*2);
      
        add(circle);
    }
}

There's no specific function call you can make with the ACM libraries that will change the coordinate system. Instead, you can work out the math to determine where the object should be placed in order to center it in the screen.

We can imagine the window as a rectangle whose upper-left corner is at (0, 0) and whose lower-right corner is at ( getWidth() , getHeight() ). If you want to center a box of dimensions W × H, the picture looks like this:

(0, 0)    getWidth()/2      getWidth()/2
      *-----------------+-----------------+
      |                 |                 |
      | (x, y)    W/2   |   W/2           |
      |      *----------+----------+      | getHeight() / 2
      |      |          |          | H/2  |
      +------+----------+----------+------+
      |      |          |          | H/2  |
      |      +----------+----------+      |  getHeight() / 2
      |                 |                 |
      |                 |                 |
      +-----------------+-----------------*
                                          (getWidth(), getHeight())

Based on this picture, can you mathematically work out what x and y are? You can then punch those coordinates in as the first two arguments to the GOval constructor.

For more practice with ACM graphics math, check out this packet of problems and this follow-up .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM