Write a Java class that reads through a text file of drawing commands and draws
ID: 3682676 • Letter: W
Question
Write a Java class that reads through a text file of drawing commands and draws the appropriate shapes onto JFrames. The following commands must be supported
FRAME width height // sets up a new frame with given width and height (both integers)
COLOR red green blue // sets the current “pen color” to the color with the given rgb components.
RECTANGE x y width height // draws a rectangle with upper left corner at x,y and given width and height (all // given in integers)
CIRCLE x y radius // draws a circle centered at x,y with given radius (as doubles)
ELLIPSE x y xradius yradius // draws an ellipse centered at x,y with semi-radii xradius and yradius (Note:
// these parameters are slightly different than that in Ellipse2D.Double’s
// constructor.
LINE x1 y1 x2 y2 // draws a line from x1,y1 to x2,y2 (as doubles)
A sample command paint_instructions.txt file is as follows:
FRAME 200 100 // open a frame, note: parser must ignore any comments
COLOR 255 0 0 // set color to red
RECTANGLE 20 30 40 20 // draw a red rectangle
COLOR 128 128 128 // set color to gray
CIRCLE 100 50 25 // draw a gray circle
FRAME 100 100 // open a second frame
COLOR 0 0 255 // set color to blue
ELLIPSE 50 50 30 20 // draw a blue ellipse
COLOR 0 255 0 // set color to green
LINE 10 20 90 80 // draw a green line
For simplicity, there should be only one command per line which may end in a comment as shown above.
Your program should read commands and set up appropriate GUI structures in the Java Swing environment. If a command has a syntax error, then your program should detect this and print out a message to the output console indicating the line number from your command file that had the error. Hint: there is an easy way to do this by catching runtime exceptions that are thrown in a try block.
Explanation / Answer
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class Read
{
public static void main(String[] args)
{
try
{
char currentChar = '.';
FileInputStream inputStream = new FileInputStream("test.txt");
while (currentChar != (char)-1 )
{
currentChar = (char)inputStream.read();
if (currentChar == 'b')
{
System.out.print("b");
}
else if (currentChar == 'w')
{
System.out.print("w");
}
else if (currentChar == '.')
{
System.out.print(".");
}
else if (currentChar == ' ')
{
System.out.print(" ");
}
}
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Implementing Java Swing User Interfaces :
Using the Swing GUI builder in JDeveloper, you can quickly and easily assemble the elements of a user interface (UI) for a Java application using Swing components. You construct the UI with components selected from the Components window, such as buttons, text areas, lists, dialogs, and menus. Then, you set the values of the component properties and attach event-handler code to the component events.
Included is a description of the UI debugger, which is used to debug user interfaces specifically for AWT and Swing-based client applications. The UI Debugger offers an alternative way of debugging a GUI application.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.