Exercise Write a Java application that reads graphical instructions from a file
ID: 3600208 • Letter: E
Question
Exercise Write a Java application that reads graphical instructions from a file called drawing.txt and executes them Detailed Requirement The file drawing.txt may contain any number of instructions; it might even be empty, in which case an appropriate message should be displayed Each line in drawing.txt represents 1 graphical instruction. A graphical instruction has the following format: instruction (parameter1, parameter2, etc.). Your application should first parse the file drawing.txt line by line and indicate all lines that contairn invalid instructions using the following message: "Instruction in line N is not valid ". Once the whole file is parsed, if at least 1 invalid instruction is detected, the program stops with a message "Program stops: Syntax errors detected!!"; otherwise, the instructions will be executed. The list of valid instructions is as follow: move (x, y): translate the current position (xc, yc) by (x, y) (i.e. xc-xc+x; yc-yc+y). setColor (r, g, b): sets the current drawing color to the rgb color (r,g,b); r, g and b should be in the range [0, 255] setBackground(r, g, b): sets the current background color to the rgb color (r.g,b); r, g and b shoulo be in the range [0, 255] Fill(status); status-true/false: if status-true the 2D object will be solid otherwise just line drawing. line(x1, y1, x2, y2): draw a line from (x1+xc, y1+yc) to (x2+xc, y2+yc) rect(x1,y1, w, h): draws a rectangle whose top left corner is (x1+xc, y1+yc) and width and height are (w, h). The rectangle will be solid only if fill status is true oval(x1,y1, x2, y2): draws an oval whose top left corner is (x1+xc, y1+yc) and width and height are (w, h). The oval will be solid only if fill status is trueExplanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileExample1 {
private static String FILENAME = "D:\test.txt";
public static void main(String []args) {
BufferedReader bread = null;
FileReader fread = null;
try {
//br = new BufferedReader(new FileReader(FILENAME));
fread = new FileReader(FILENAME);
bread = new BufferedReader(fread);
String sCurrentLine;
while ((sCurrentLine = bread.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bread != null)
bread.close();
if (fread != null)
fread.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.