Question
Hey guys! Could you please help me with this coding assignment? I've done the beginning parts so I finished the Dot, DotException, and DotReader classes
Hey guys! Could you please help me with this coding assignment?
I've done the beginning parts so I finished the Dot, DotException, and DotReader classes but I need to finish the DotDisplay class. Below I'm pasted my already finished classes.
Dot class:
package dotlab;
public class Dot
{
private static String[] LEGAL_COLOR_NAMES =
{
"RED", "YELLOW", "BLUE", "CYAN", "GREEN", "MAGENTA", "ORANGE", "BLACK"
};
public String colorName;
public int x;
public int y;
public int radius;
public String getColorName()
{
return colorName;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getRadius()
{
return radius;
}
public Dot(String colorName, int x, int y, int radius)
{
boolean colorPresent = false;
for (int i = 0; i
{
if (colorName.equals(LEGAL_COLOR_NAMES[i]))
{
this.colorName = colorName;
colorPresent = true;
}
}
if(!colorPresent) {
throw new IllegalArgumentException("Invalid color name: "+ colorName);
}
this.x = x;
this.y = y;
this.radius = radius;
}
//overiding toString method so that it will print out the dot color,
//x and y coordinate, and radius in a readable format.
public String toString()
{
return colorName + "," + x + "," + y + "," + radius;
}
public static void main(String[] args)
{
Dot dot1 = new Dot("RED", 1, 1, 1);
System.out.println(dot1.toString());
}
}
DotException class:
package dotlab;
public class DotException extends Exception
{
public DotException(String arg)
{
super(arg);
}
}
DotReader class:
package dotlab;
import java.io.*;
public class DotReader
{
private BufferedReader br;
public DotReader(BufferedReader br)
{
this.br = br;
}
//returns type Dot, takes no arguments, and throws IOException
public Dot readDot() throws IOException, DotException
{
String sr = br.readLine();
if(sr == null)
{
return null;
}
String[] words = sr.split(",");
String name = words[0];
int x = Integer.parseInt(words[1]);
int y = Integer.parseInt(words[2]);
int radius = Integer.parseInt(words[3]);
Dot d = new Dot(name, x, y, radius);
if (words.length > 4 || words.length
{
DotException error =
new DotException("Dot size is not what is expected");
throw error;
}
return d;
}
}
Here are the instructions for the DotDisplay class that I need help finishing:
This is what I have so far for the DotDisplay class. Everything in this class was given by the professor except for the read() method which I attemped to create using the instructions.
package dotlab;
import java.lang.reflect.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/*
* Don't touch this until Part 3. Leave all the code alone. Just add a read() method
* as described in the instructions. When you do that correctly, the compiler
* errors will all be corrected.
*
* Run this as an application to see the dots on the screen.
*/
public class DotDisplay extends JFrame implements ActionListener
{
private ArrayList dots;
private DotPanel dotPanel;
private JButton readBtn;
private JButton quitBtn;
public DotDisplay()
{
dots = new ArrayList();
// Init GUI: DotPanel at center, control panel at north.
dotPanel = new DotPanel();
add(dotPanel, BorderLayout.CENTER);
JPanel controls = new JPanel();
readBtn = new JButton("Read...");
readBtn.addActionListener(this);
controls.add(readBtn);
quitBtn = new JButton("Quit");
quitBtn.addActionListener(this);
controls.add(quitBtn);
add(controls, BorderLayout.NORTH);
pack();
}
// Add a dot to the display list. Don't repaint here. Caller should
// repaint when list is fully populated.
public void addDot(Dot dot)
{
dots.add(dot);
}
// Process a button click in the control panel.
public void actionPerformed(ActionEvent e)
{
// Quit.
if (e.getSource() == quitBtn)
System.exit(0);
// Read. Prompt for input file, then parse using read().
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
File f = chooser.getSelectedFile();
if (f == null)
return;
try
{
dots.clear();
read(f);
dotPanel.repaint();
}
catch (IOException x)
{
System.out.println("Couldn't read file " + f);
}
catch (DotException x)
{
// Caused by invalid input line in file.
System.out.println(x.getMessage());
}
}
// Inner class
private class DotPanel extends JPanel
{
public Dimension getPreferredSize()
{
return new Dimension(800, 600);
}
public void paintComponent(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, 3000, 3000);
for (Dot dot: dots)
{
g.setColor(Color.LIGHT_GRAY);
try
{
// Reflect color name. Dot ctor shouldn't accept an invalid name, so
// this block shouldn't fail. If it does fail for some reason, the dot
// color will failsafe to light gray.
Field field = Color.class.getField(dot.getColorName());
Color fill = (Color)field.get(null);
g.setColor(fill);
}
catch (NoSuchFieldException|IllegalAccessException x) { }
// Compute bounding box for the dot, and paint.
int ulx = dot.getX() - dot.getRadius();
int uly = dot.getY() - dot.getRadius();
g.fillOval(ulx, uly, 2*dot.getRadius(), 2*dot.getRadius());
}
}
} // End of inner class DotPanel.
public static void main(String[] args)
{
new DotDisplay().setVisible(true);
}
//takes a File as an argument,
//returns nothing,
//and throws IOException and DotException
public void read(File f) throws IOException, DotException
{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
DotReader dr = new DotReader(br);
Dot d1;
boolean done = false;
while (!done)
{
d1 = dr.readDot();
if (d1 == null)
{
done = true;
}
else
{
addDot(null);
}
}
br.close();
fr.close();
}
}
Any help is appreciated! Thank you so much!!
Part 3: Fixing the DotDisplay class STEP 14: Now that we have a functional Dot and DotReader class, let's put those to work in making our application complete. Notice that DotDisplay doesn't compile because there's a cal to a read) method that doesn't exist yet. You probably won't understand much about this source. That's ok, all you need to do is give it a read() method. In DotDisplay.java create a private read) method that takes a File as an argument, returns nothing, and throws IOException and DotException. Within the method body, create a new FileReader that takes the argument file as a parameter, a new BufferedReader that takes the FileReader as a parameter, and a new DotReader that takes the BufferedReader as a parameter STEP 15: We're going to read the data from our DotReader and keep constructing new Dots as long as we have information to read. Use a while loop to continuously read from the DotReader until there is nothing else to read. Each pass through the loop should call addDot() with the newly read Dot as an argument. When the loop is done, close the Buffered Reader and then the FileReader STEP 16: Let's test out our program. Run DotDisplay.java and click 'Read... Use the navigator to find and open the '3dots.txt' that you downloaded with the rest of this assignment. You should have a window that looks like this: STEP 17: Great! Now let's make sure our IllegalArgumentException and DotException are working properly. Edit the 3dot.txt so that one of the lines causes an IlegalArgumentException. Driver: Paste the contents of your 3dot.txt. Undo those changesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started