Graphic Rendering System 1.0
A Java-based graphic rendering system implementing various design patterns
Loading...
Searching...
No Matches
Line.java
Go to the documentation of this file.
1package com.example.graphics.model;
2
3import com.example.graphics.visitor.ShapeVisitor;
4
8public class Line implements Shape {
9 private int x1;
10 private int y1;
11 private int x2;
12 private int y2;
13
21 public Line(int x1, int y1, int x2, int y2) {
22 this.x1 = x1;
23 this.y1 = y1;
24 this.x2 = x2;
25 this.y2 = y2;
26 }
27
28 @Override
29 public int getX() {
30 return x1; // Return the first endpoint's x as the reference point
31 }
32
33 @Override
34 public int getY() {
35 return y1; // Return the first endpoint's y as the reference point
36 }
37
38 @Override
39 public void setPosition(int x, int y) {
40 // Calculate the offset and apply it to both endpoints
41 int dx = x - x1;
42 int dy = y - y1;
43
44 x1 = x;
45 y1 = y;
46 x2 += dx;
47 y2 += dy;
48 }
49
54 public int getX1() {
55 return x1;
56 }
57
62 public int getY1() {
63 return y1;
64 }
65
70 public int getX2() {
71 return x2;
72 }
73
78 public int getY2() {
79 return y2;
80 }
81
87 public void setPoint1(int x1, int y1) {
88 this.x1 = x1;
89 this.y1 = y1;
90 }
91
97 public void setPoint2(int x2, int y2) {
98 this.x2 = x2;
99 this.y2 = y2;
100 }
101
109 public void setLine(int x1, int y1, int x2, int y2) {
110 this.x1 = x1;
111 this.y1 = y1;
112 this.x2 = x2;
113 this.y2 = y2;
114 }
115
116 @Override
118 visitor.visit(this);
119 }
120
121 @Override
122 public Shape clone() {
123 return new Line(this.x1, this.y1, this.x2, this.y2);
124 }
125}
void setPosition(int x, int y)
Definition Line.java:39
Line(int x1, int y1, int x2, int y2)
Definition Line.java:21
void accept(ShapeVisitor visitor)
Definition Line.java:117
void setPoint2(int x2, int y2)
Definition Line.java:97
void setPoint1(int x1, int y1)
Definition Line.java:87
void setLine(int x1, int y1, int x2, int y2)
Definition Line.java:109