Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need Help Coding This In Java - using arrayList For the next checkpoint, you will write an application that will parse data from a text

Need Help Coding This In Java - using arrayList

For the next checkpoint, you will write an application that will parse data from a text file that contains instructions for drawing some simple graphics. Each line of the file describes a polyline, that is, a sequence of points with line segments drawn between them. We'll encapsulate the data for one polyline in an object of type Polyline, and we'll draw everything using an application called Plotter. The code for these is provided for you in a jar file.

To start out, let's try an example to see how it works. First import the jar file polyline_plotter.jar into your project and add it to the build path (just as you would a specchecker). Then, try running the code TestPlotter.java The main method looks like this:

 public static void main(String[] args) { // make a red square using default line width of one pixel Polyline pl = new Polyline("red"); pl.addPoint(new Point(100, 100)); pl.addPoint(new Point(200, 100)); pl.addPoint(new Point(200, 200)); pl.addPoint(new Point(100, 200)); pl.addPoint(new Point(100, 100)); // make a blue triangle with a line width of 2 pixels Polyline pl2 = new Polyline("blue", 2); pl2.addPoint(new Point(250, 100)); pl2.addPoint(new Point(400, 350)); pl2.addPoint(new Point(100, 350)); pl2.addPoint(new Point(250, 100)); // make some green zig-zags with a thick line Polyline pl3 = new Polyline("green", 6); pl3.addPoint(new Point(100, 400)); pl3.addPoint(new Point(200, 450)); pl3.addPoint(new Point(300, 400)); pl3.addPoint(new Point(400, 450)); // plot the three polylines using the plotter Plotter plotter = new Plotter(); plotter.plot(pl); plotter.plot(pl2); plotter.plot(pl3); } 

Drawing takes place on a window that is roughly 500 by 500 pixels. Points are specified using integer coordinates. The upper-left corner is (0, 0), where x-coordinates increase from left to right and y-coordinates increase from top to bottom.

To construct a Polyline object, we specify one of several possible colors, and optionally a line width in pixels. Then we use the addPoint method to add points to the polyline. The plotter will start at the first point given and draw a line segment between each successive pair of points. (The Point class comes from the package java.awt, see the import statements at the top of the TestPlotter.java file.)

An ArrayList is also useful whenever you are reading input because you don't have to know in advance how many items there will be.

Instead of hard-coding the color and point data for the polylines as in the TestPlotter example, we would like to be able to read the data from a text file. Suppose that each line of the file corresponds to one polyline, and consists of the following:

An optional integer line width

A color, which is a string such as "red" or "blue" (see the polyline javadoc for details on recognized colors)

A sequence of integers representing coordinates of the points, where each pair is one point, x followed by y (there is always an even number of integers following the color name)

A file may also contain blank lines, which should be ignored, and comments, which should be ignored. Any line starting with the pound sign character "#" is considered a comment.

For example, the text file describing the output from our test program could look like this (you can find it here too):

# draws a red square red 100 100 200 100 200 200 100 200 100 100 # draws a blue triangle with line width of 2 pixels 2 blue 250 100 400 350 100 350 250 100 # draws a green zig-zag with a 6-pixel line 6 green 100 400 200 450 300 400 400 450 

(Note: a simple way to tell whether a line is blank is to first use the String method trim(), which removes all leading and trailing whitespace, and then check whether the length is zero.)

Write a program that will read files of the above format and plot the polylines using the plotter. You can refer to the sample code QuizAverager as a model for the file-reading loop. Start with a helper method that parses just one line and returns the corresponding Polyline object. Then write a helper method that opens and reads the file, and returns an ArrayList of Polyline objects:

 private static ArrayList readFile(String filename) throws FileNotFoundException { }  

Then the main method can iterate over the list, and call plot() for each Polyline.

Then the main method can iterate over the list, and call plot() for each Polyline.

Then, try it on the file hello.txt. here:

# mystery drawing! 4 red 105 153 105 152 105 155 103 161 100 174 97 188 91 212 89 233 88 257 88 275 88 297 88 315 88 328 88 337 88 344 88 350 89 353 89 354 89 355 red 90 253 91 252 94 252 98 252 104 252 114 252 123 252 133 252 146 252 157 252 165 252 173 252 178 252 181 252 183 252 184 252 185 252 186 252 188 252 190 252 4 red 196 169 196 171 193 179 191 186 188 198 186 209 185 222 185 234 185 247 185 257 185 265 185 274 185 283 184 293 183 301 183 307 182 314 182 318 182 321 182 325 181 328 181 330 181 332 181 334 181 335 181 336 181 337 181 338 181 340 181 342 181 346 181 347 181 348 181 348 180 348 180 349 180 350 180 351 180 352 2 green 218 312 219 312 225 313 227 313 232 313 237 313 240 313 243 313 245 311 246 309 246 306 246 303 246 299 246 297 246 295 246 293 246 291 246 290 246 289 246 288 245 287 242 285 239 284 234 283 230 283 226 283 223 283 220 283 218 284 215 286 213 288 209 291 206 293 204 296 202 298 201 299 201 300 201 302 201 305 201 309 201 314 201 318 203 324 204 328 207 333 210 337 213 340 216 343 220 344 224 346 229 346 233 346 238 346 241 346 245 346 247 346 249 346 250 346 251 346 252 346 253 345 255 344 255 344 4 blue 292 192 292 193 292 198 291 209 289 222 287 239 285 257 285 273 285 291 285 306 285 320 284 334 284 347 284 358 284 365 284 372 284 377 284 379 284 381 4 magenta 341 194 341 195 341 199 341 204 341 211 340 225 340 236 340 251 339 265 339 280 339 293 339 305 339 315 339 325 339 332 339 337 339 344 339 351 339 353 339 355 339 357 2 orange 404 301 402 301 400 301 399 301 397 301 394 301 391 301 388 301 386 301 385 301 383 303 383 307 382 311 382 316 382 322 382 329 382 335 382 343 382 349 382 354 384 359 387 365 390 368 393 371 397 374 403 375 409 376 416 376 424 376 432 373 438 368 443 363 448 357 451 350 454 344 454 340 455 335 455 331 455 327 453 323 451 317 448 311 445 306 443 303 440 301 436 299 432 297 429 297 426 297 424 297 423 297 422 297 421 297 419 296 417 296 416 296 416 296 415 296 414 296 413 296 412 296 412 296 411 296 411 297 410 297 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Publishing With Filemaker Pro On The Web

Authors: Maria Langer

1st Edition

0201696657, 978-0201696653

More Books

Students also viewed these Databases questions

Question

Assessment of skills and interests.

Answered: 1 week ago

Question

Psychological, financial, and career counseling.

Answered: 1 week ago