Write a Java class that reads through a text file of drawing commands and draws
ID: 3682462 • 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
package project1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
class SetColor extends JComponent {
int red,green,blue;
SetColor(int red,int green,int blue){
this.red=red;
this.blue=blue;
this.green=green;
}
public void paint(Graphics g) {
Color c=new Color(red,green,blue);
g.setColor(c);
//g.drawRect (10, 10, 200, 200);
}
}
class DrawRect extends JComponent {
int a,b,c,d;
DrawRect(int a,int b,int c,int d){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
public void paint(Graphics g) {
g.drawRect (a, b, c, d);
}
}
class DrawCircle extends JComponent {
int a,b,c,d;
DrawCircle(int a,int b,int c){
this.a=a;
this.b=b;
this.c=c;
}
public void paint(Graphics g) {
g.drawOval(a,b,c,10);
}
}
class DrawEllipse extends JComponent {
int a,b,c,d;
DrawEllipse(int a,int b,int c,int d){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
public void paint(Graphics g) {
g.drawOval(a,b,c,d);
}
}
class DrawLine extends JComponent {
int a,b,c,d;
DrawLine(int a,int b,int c,int d){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
public void paint(Graphics g) {
g.drawOval(a,b,c,d);
}
}
public class Drawing extends JPanel{
public static void main(String[] args){
File fin=new File("d:\drawing_commands.txt");
BufferedReader br;
StringTokenizer st;
String line,command;
int width=0,height=0,red=0,green=0,blue=0,a=0,b=0,c=0,d=0;
JFrame frame=null;
SetColor sc=null;
DrawRect dr=null;
DrawCircle dc=null;
DrawEllipse de=null;
DrawLine dl=null;
try {
br = new BufferedReader(new FileReader(fin));
while ((line = br.readLine()) != null) {
System.out.println(line);
st = new StringTokenizer(line);
command=st.nextToken(" ");
System.out.println("Command : " +command);
switch(command){
case "FRAME":
if(st.hasMoreTokens()){
width=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
height=Integer.parseInt(st.nextToken(" "));
}
frame=new JFrame("Drawing");
frame.setSize(width, height);
frame.setVisible(true);
break;
case "COLOR":
if(st.hasMoreTokens()){
red=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
green=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
blue=Integer.parseInt(st.nextToken(" "));
}
sc=new SetColor(red,green,blue);
frame.getContentPane().add(sc);
break;
case "RECTANGLE":
if(st.hasMoreTokens()){
a=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
b=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
c=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
d=Integer.parseInt(st.nextToken(" "));
}
dr=new DrawRect(a,b,c,d);
frame.getContentPane().add(dr);
break;
case "CIRCLE":
if(st.hasMoreTokens()){
a=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
b=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
c=Integer.parseInt(st.nextToken(" "));
}
dc=new DrawCircle(a,b,c);
frame.getContentPane().add(dc);
break;
case "ELLIPSE":
if(st.hasMoreTokens()){
a=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
b=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
c=Integer.parseInt(st.nextToken(" "));
}
de=new DrawEllipse(a,b,c,d);
frame.getContentPane().add(de);
break;
case "LINE":
if(st.hasMoreTokens()){
a=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
b=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
c=Integer.parseInt(st.nextToken(" "));
}
if(st.hasMoreTokens()){
d=Integer.parseInt(st.nextToken(" "));
}
dl=new DrawLine(a,b,c,d);
frame.getContentPane().add(dl);
break;
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.