-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCenterSquare.java
48 lines (40 loc) · 1.24 KB
/
CenterSquare.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* This exercise is meant to center a square.
* Or get a notion how algorithms work to center objects
* no the canvas.
*/
import acm.program.*;
import acm.graphics.*;
public class CenteringSquare extends GraphicsProgram{
public void run () {
int x = getWidth()/2 - BASE/2;
int y = getHeight()/2 - HEIGHT/2;
GRect square = new GRect (x, y, BASE, HEIGHT);
add (square);
}
private static final int BASE = 100;
private static final int HEIGHT = 100;
}
/* Other algorithms I came across
* 1.
* float x = getWidth()/2 - VARIABLE_WIDTH/2;
* float y = getHeight()/2 - VARIABLE_HEIGHT/2;
* OBJECT circle = new OBJECT.Float(x, y, VARIABLE_WIDTH, VARIABLE_HEIGHT);
*
* 2. For centereing an object accorgind with a width and height variable.
* float x = (width-width of OBJECT) /2;
* float y = (height-height of OBJECT) /2;
*
* 3.
* int x=(getWidth()-objectlWidth)/2;
* int y=(getHeight()-objectHeight)/2;
*
* 4.
* Dimension size = getSize();
* Ellipse2D circle = new Ellipse2D.Float(
* (size.width - 200) / 2, // -200 due to the width/height of the circle
* (size.height - 200) / 2,
* 200, 200);
*
* source: https://stackoverflow.com/questions/8724429/centre-an-image-in-the-middle-of-a-panel
*/