Kelas : SE 05-01
Nim : 1201220025
Jawab :
Example :
Langkah-langkah:
1. Tentukan titik awal dan akhir (Titik awal (x0, y0) = (0, 0)
➢ dx = x1 - x0 = 82
➢ dy = y1 - y0 = 34
➢ gcd(dx, dy) = 2
73. /**
74. * Draws a coordinate system (according to the window coordinates).
75. *
76. * @param xmax x-coordinate to which the x-axis should extend.
77. * @param ymax y-coordinate to which the y-axis should extend.
78. * @param g2d Graphics2D object for drawing.
79. */
80. public static void drawSimpleCoordinateSystem(int xmax, int ymax,
81. Graphics2D g2d)
82. {
83. int xOffset = 30;
84. int yOffset = 50;
85. int step = 20;
86. String s;
87. //Remember the actual font.
88. Font fo = g2d.getFont();
89. //Use a small font.
90. g2d.setFont(new Font("ARIAL",Font.PLAIN,9));
91. //x-axis.
92. g2d.drawLine(xOffset,yOffset,xmax,yOffset);
93. //Marks and labels for the x-axis.
94. for (int i=xOffset+step; i<=xmax; i=i+step)
95. {
96. g2d.drawLine(i,yOffset-2,i,yOffset+2);
97. g2d.drawString(String.valueOf(i),i-7,yOffset-7);
98. }
99.
100. //y-axis.
101. g2d.drawLine(xOffset,yOffset,xOffset,ymax);
102.
103. //Marks and labels for the y-axis.
104. s=" "; //for indention of numbers < 100
105. for (int i=yOffset+step; i<=ymax; i=i+step)
106. {
107. g2d.drawLine(xOffset-2,i,xOffset+2,i);
108. if (i>99){s="";}
109. g2d.drawString(s+String.valueOf(i),xOffset-25,i+5);
110. }
111.
112. //Reset to the original font.
113. g2d.setFont(fo);
114. }
115.
116. public static void main(String[] argv)
117. {
118. GeneralPathCar f = new GeneralPathCar();
119. f.setTitle("General Path Car");
120. f.setSize(500,500);
121. f.setVisible(true);
122. f.setLocationRelativeTo(null);
123. }
124. }