Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add files via upload
- Loading branch information
Showing
25 changed files
with
581 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#BlueJ class context | ||
comment0.target=Collection | ||
comment0.text=\r\n\ Write\ a\ description\ of\ class\ Collection\ here.\r\n\r\n\ @author\ (your\ name)\r\n\ @version\ (a\ version\ number\ or\ a\ date)\r\n | ||
comment1.params= | ||
comment1.target=Collection() | ||
comment1.text=\r\n\ Constructor\ for\ objects\ of\ class\ Collection\r\n | ||
comment2.params=y | ||
comment2.target=int\ sampleMethod(int) | ||
comment2.text=\r\n\ An\ example\ of\ a\ method\ -\ replace\ this\ comment\ with\ your\ own\r\n\r\n\ @param\ \ y\ \ a\ sample\ parameter\ for\ a\ method\r\n\ @return\ \ \ \ the\ sum\ of\ x\ and\ y\r\n | ||
numComments=3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
/** | ||
* Write a description of class Collection here. | ||
* | ||
* @author (your name) | ||
* @version (a version number or a date) | ||
*/ | ||
public class Collection | ||
{ | ||
// instance variables - replace the example below with your own | ||
private int x; | ||
|
||
/** | ||
* Constructor for objects of class Collection | ||
*/ | ||
public Collection() | ||
{ | ||
// initialise instance variables | ||
x = 0; | ||
} | ||
|
||
/** | ||
* An example of a method - replace this comment with your own | ||
* | ||
* @param y a sample parameter for a method | ||
* @return the sum of x and y | ||
*/ | ||
public int sampleMethod(int y) | ||
{ | ||
// put your code here | ||
return x + y; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#BlueJ class context | ||
comment0.target=DotArt | ||
comment1.params=title | ||
comment1.target=DotArt(java.lang.String) | ||
comment1.text=\r\n\ Constructor\ for\ objects\ of\ class\ MyApp\r\n | ||
comment2.params=args | ||
comment2.target=void\ main(java.lang.String[]) | ||
comment2.text=\r\n\ The\ mainline\ program.\r\n | ||
numComments=3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 2102: DotArt class is the main program. | ||
* | ||
* @author (Greg Johnson) | ||
* @version (November 1,2017) | ||
*/ | ||
import java.awt.*; | ||
import javax.swing.*; | ||
|
||
public class DotArt extends JFrame | ||
{ | ||
DrawingPanel _theDrawingPanel; | ||
|
||
/** | ||
* Constructor for objects of class MyApp | ||
*/ | ||
public DotArt(String title) | ||
{ | ||
super(title); | ||
this.setSize(1000, 800); | ||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
// create and add Panels to the Frame | ||
this.add(new DrawingPanel()); | ||
this.setVisible(true); | ||
} | ||
|
||
/** | ||
* The mainline program. | ||
*/ | ||
public static void main(String[] args) | ||
{ | ||
DotArt da = new DotArt("2102 Dot Art"); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#BlueJ class context | ||
comment0.target=DrawingPanel | ||
comment1.params= | ||
comment1.target=DrawingPanel() | ||
comment1.text=\r\n\ Constructor\ for\ objects\ of\ class\ ObjectPanel\r\n | ||
comment2.params=aBrush | ||
comment2.target=void\ paintComponent(java.awt.Graphics) | ||
comment2.text=\r\n\ The\ paintComponent\ method\ of\ class\ DrawingPanel\ is\ responsible\ for\ painting\r\n\ the\ panel\ any\ time\ there\ is\ a\ change\ in\ the\ panel.\r\n | ||
comment3.params=e | ||
comment3.target=void\ mouseClicked(java.awt.event.MouseEvent) | ||
comment3.text=\r\n\ The\ mouseClicked\ method\ will\ create\ a\ new\ SmartEllipse\r\n\ and\ (eventually)\ add\ it\ to\ the\ ArrayList\ of\ dots\ in\ the\ drawing\ area\r\n | ||
numComments=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** 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(); | ||
} | ||
} | ||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#BlueJ class context | ||
comment0.target=KeyInteractor | ||
comment0.text=\r\n\ A\ simplified\ class\ for\ keyboard\ interaction.\ Subclasses\ override\r\n\ the\ \ actionPerformed\ method\ to\ do\ something\ useful.\ While\ \ \ \ \ \ \ \ \ \r\nthe\ javax.swing.JComponent\ passed\ in\ can\ technically\ be\r\n\ anything,\ it\ should\ generally\ be\ the\ main\ panel\ in\ a\ GUI.\r\n\ The\ call\ to\ super()\ in\ the\ subclass\ will\ need\ parameters--the\ component\ \r\n\ and\ the\ keyCode\ at\ least.\r\n\r\n\ @author\ John\ Goodwin\ (simplified\ rdm)\r\n | ||
comment1.params=comp\ keyCode\ mod | ||
comment1.target=KeyInteractor(javax.swing.JComponent,\ int,\ int) | ||
comment1.text=\r\n\ Creates\ a\ key\ interactor\ for\ the\ specified\ component.\r\n\r\n\ @param\ comp\ the\ component\ that\ will\ receive\ the\ key\ input\r\n\ @param\ keyCode\ the\ (int)\ key\ code\ that\ the\ interactor\ will\ respond\ to\r\n\ @param\ mod\ the\ (int)\ modifier\ of\ that\ key\ code.\ \r\n\ \ see\ constants\ in\ KeyEvent\ for\ codes\r\n | ||
comment2.params=comp\ keyCode | ||
comment2.target=KeyInteractor(javax.swing.JComponent,\ int) | ||
comment3.params=e | ||
comment3.target=void\ actionPerformed(java.awt.event.ActionEvent) | ||
comment3.text=\r\n\r\n\ The\ method\ that\ gets\ called\ when\ the\ key\ is\ pressed.\r\n\r\n\ @param\ e\ the\ ActionEvent\ associated\ with\ the\ current\ call\r\n | ||
numComments=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//source code for KeyInteractor: | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
|
||
/** | ||
* A simplified class for keyboard interaction. Subclasses override | ||
* the actionPerformed method to do something useful. While | ||
the javax.swing.JComponent passed in can technically be | ||
* anything, it should generally be the main panel in a GUI. | ||
* The call to super() in the subclass will need parameters--the component | ||
* and the keyCode at least. | ||
* | ||
* @author John Goodwin (simplified rdm) | ||
*/ | ||
|
||
|
||
public abstract class KeyInteractor extends AbstractAction { | ||
|
||
public static final String COMMAND_PREFIX = "KeyInteractor"; | ||
|
||
/** | ||
* Creates a key interactor for the specified component. | ||
* | ||
* @param comp the component that will receive the key input | ||
* @param keyCode the (int) key code that the interactor will respond to | ||
* @param mod the (int) modifier of that key code. | ||
* see constants in KeyEvent for codes | ||
*/ | ||
public KeyInteractor (JComponent comp, int keyCode, int mod) { | ||
super(); | ||
// a name that identifies this action | ||
String actionID = COMMAND_PREFIX + keyCode + "." + mod; | ||
|
||
//map the keystroke to the actionID in the InputMap | ||
InputMap imap = comp.getInputMap(); | ||
imap.put(KeyStroke.getKeyStroke(keyCode,mod), actionID); | ||
|
||
//map the action key to this | ||
ActionMap amap = comp.getActionMap(); | ||
amap.put(actionID, this); | ||
} | ||
|
||
public KeyInteractor (JComponent comp, int keyCode) { | ||
this(comp,keyCode,0); // default--no CTRL-SHIFT-etc modifier | ||
} | ||
|
||
/** | ||
* | ||
* The method that gets called when the key is pressed. | ||
* | ||
* @param e the ActionEvent associated with the current call | ||
*/ | ||
public abstract void actionPerformed (ActionEvent e); | ||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#BlueJ class context | ||
comment0.target=MyMouseListener | ||
comment1.params=dp | ||
comment1.target=MyMouseListener(DrawingPanel) | ||
comment2.params=e | ||
comment2.target=void\ mouseClicked(java.awt.event.MouseEvent) | ||
numComments=3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import java.awt.event.*; | ||
import javax.swing.event.*; | ||
public class MyMouseListener extends MouseAdapter | ||
{ | ||
DrawingPanel _myPanel; | ||
// constructor | ||
public MyMouseListener(DrawingPanel dp) | ||
{ | ||
_myPanel = dp; | ||
} | ||
public void mouseClicked(MouseEvent e) | ||
{ | ||
_myPanel.mouseClicked(e); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#BlueJ class context | ||
comment0.target=SmartEllipse | ||
comment1.params=aColor | ||
comment1.target=SmartEllipse(java.awt.Color) | ||
comment1.text=\ \r\n\ Constructor\ for\ the\ SmartEllipse\ class\r\n | ||
comment10.params=aBetterBrush | ||
comment10.target=void\ fill(java.awt.Graphics2D) | ||
comment10.text=\ \r\n\ Method\ fill\ will\ fill\ in\ the\ elliptical\ object\ with\ the\ fill\ color\r\n | ||
comment11.params=aBrush | ||
comment11.target=void\ draw(java.awt.Graphics2D) | ||
comment11.text=\r\n\ Method\ draw\ will\ draw\ an\ outline\ of\ the\ elliptical\ object\ with\ \r\n\ the\ draw\ color\r\n | ||
comment2.params=aColor | ||
comment2.target=void\ setBorderColor(java.awt.Color) | ||
comment3.params= | ||
comment3.target=java.awt.Color\ getBorderColor() | ||
comment4.params=aColor | ||
comment4.target=void\ setFillColor(java.awt.Color) | ||
comment5.params= | ||
comment5.target=java.awt.Color\ getFillColor() | ||
comment6.params=aRotation | ||
comment6.target=void\ setRotation(int) | ||
comment7.params=x\ y | ||
comment7.target=void\ setLocation(double,\ double) | ||
comment8.params=aWidth\ aHeight | ||
comment8.target=void\ setSize(int,\ int) | ||
comment9.params=aChangeInX\ aChangeInY | ||
comment9.target=void\ move(int,\ int) | ||
numComments=12 |
Oops, something went wrong.