Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
/** Drawing Panel displays the circles the user has created via mouse clicks
*
* @author (Greg Johnson)
* @version (November 1, 2017)
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
import jdk.internal.dynalink.beans.StaticClass;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class DrawingPanel extends JPanel
{
SmartEllipse _circle = null;
ArrayList<SuperEllipse> EllipseList = new ArrayList<SuperEllipse>();
JButton sButton = new JButton("Save to file");
JButton rButton = new JButton("Read from file");
JPanel panel = new JPanel();
private KeySListener _saveKey;
private KeyOListener _openKey;
Random rand = new Random();
/**
* Constructor for objects of class ObjectPanel
*/
public DrawingPanel()
{
set_saveKey(new KeySListener(this));
set_openKey(new KeyOListener(this));
this.add(sButton);
this.add(rButton);
MyMouseListener myMouseListener = new MyMouseListener(this);
// set up the display on the panel
this.setBackground(Color.WHITE);
// add the panel as a MouseListener
this.addMouseListener(myMouseListener);
sButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileDialog = new JFileChooser();
int returnVal = fileDialog.showSaveDialog(sButton);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream( new FileOutputStream(fileDialog.getSelectedFile()));)
{
//ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.data"));
objectOutputStream.writeObject(EllipseList);
objectOutputStream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}}
});
rButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileDialog = new JFileChooser();
int returnVal = fileDialog.showOpenDialog(rButton);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileDialog.getSelectedFile()));){
EllipseList= (ArrayList<SuperEllipse>) objectInputStream.readObject();
getParent().repaint();
}
catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}}}
});
}
/**
* The paintComponent method of class DrawingPanel is responsible for painting
* the panel any time there is a change in the panel.
*/
public void paintComponent(Graphics aBrush)
{
super.paintComponent(aBrush);
Graphics2D aBetterBrush = (Graphics2D)aBrush;
for(SuperEllipse smartEllipse : EllipseList) {
smartEllipse.fill(aBetterBrush);
}
}
/**
* The mouseClicked method will create a new SmartEllipse
* and (eventually) add it to the ArrayList of dots in the drawing area
*/
public void mouseClicked(java.awt.event.MouseEvent e)
{
Point currentPoint;
// get the current mouse location
currentPoint = e.getPoint();
//create a new circle and put draw it on the panel
_circle = new SuperEllipse(get_color());
_circle.setSize(20,20); //dot on screen is 20 by 20 pixels
_circle.setLocation(currentPoint.x-10, currentPoint.y-10); //subtracting 10 from x and y centers the circle on the mouse click
repaint();
EllipseList.add((SuperEllipse) _circle);
}
private Color get_color() {
int red = rand.nextInt(250);
int blue = rand.nextInt(250);
int green = rand.nextInt(250);
return new Color(red, blue, green);
}
public KeySListener get_saveKey() {
return _saveKey;
}
public void set_saveKey(KeySListener _saveKey) {
this._saveKey = _saveKey;
}
public KeyOListener get_openKey() {
return _openKey;
}
public void set_openKey(KeyOListener _openKey) {
this._openKey = _openKey;
}
private class KeySListener extends KeyInteractor{
public KeySListener(JPanel p) {
super(p, KeyEvent.VK_S);
}
public void actionPerformed(ActionEvent e) {
sButton.doClick();
}
}
private class KeyOListener extends KeyInteractor{
public KeyOListener(JPanel p) {
super(p, KeyEvent.VK_O);
}
public void actionPerformed(ActionEvent e) {
rButton.doClick();
}
}
}