Вы находитесь на странице: 1из 16

import

import
import
import
import
import
import
import
import

javax.swing.*;
javax.imageio.*;
java.awt.image.BufferedImage;
java.awt.*;
java.awt.geom.*;
java.awt.event.*;
java.util.*;
java.io.*;
java.awt.print.*;

// Create enum types that will be useful in the program


enum Figures {TREE,SNOWFLAKE,GREETING,CLOUD,CABIN,SNOWMAN};
enum Mode {NONE,DRAW,SELECTED,MOVING};
//Code to print out the greeting card once the user is done
class thePrintPanel implements Printable
{
JPanel panelToPrint;
public int print(Graphics g, PageFormat pf, int page) throws PrinterExce
ption
{
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
AffineTransform t = new AffineTransform();
t.scale(0.9, 0.9);
g2d.transform(t);
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now print the window and its visible contents */
panelToPrint.printAll(g);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
public thePrintPanel(JPanel p)
{
panelToPrint = p;
}
}
public class Assig5
{
//variables that define the program that the user can see
private ShapePanel drawPanel;
private JPanel buttonPanel;
private JButton makeShape;
private JRadioButton makeTree, makeFlake, makeGreet, makeCloud, makeCabi
n,makeSnowman;
private ButtonGroup shapeGroup;

private Figures currShape;


private JLabel msg;
private JMenuBar theBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem endProgram, printScene,newFile,openFile,saveFile,saveA
sFile,saveAsJPG,cut,copy,paste;
private JPopupMenu popper;
private JMenuItem delete;
private JMenuItem resize;
private JFrame theWindow;
private int unsaved; //0 means unsaved, 1 means saved
private int samefile; //0 means same file, 1 means new file
private Scanner drawingFile;
private String infilename;
// This ArrayList is used to store the shapes in the program.
// It is specified to be of type MyShape, so objects of any class
// that implements the MyShape interface can be stored in here.
private String filename=null; //used for saving file
private ArrayList<MyShape> shapeList;
//variables used for comparing or adding shapes to arrays
private Greeting Greeting;
private Tree Tree;
private Cloud Cloud;
private Snowman Snowman;
private Snowflake Snowflake;
private Cabin Cabin;
private MyShape newShape;
private MyShape newShape2;
//private BufferedImage img;
private int ccpMode; //1=cut, 2=copy
public Assig5()
{
//laying out the visable part of the program
drawPanel = new ShapePanel(640, 480);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 2));
makeShape = new JButton("Click to Draw");
ButtonHandler bhandler = new ButtonHandler();
makeShape.addActionListener(bhandler);
buttonPanel.add(makeShape);
msg = new JLabel("");
buttonPanel.add(msg);
makeTree = new JRadioButton("Tree", false);
makeFlake = new JRadioButton("Snowflake", true);
makeGreet = new JRadioButton("Greeting", false);
makeCloud=new JRadioButton("Cloud",false);
makeCabin=new JRadioButton("Cabin",false);
makeSnowman=new JRadioButton("Snowman",false);
RadioHandler rhandler = new RadioHandler();
makeTree.addItemListener(rhandler);
makeFlake.addItemListener(rhandler);
makeGreet.addItemListener(rhandler);

makeCloud.addItemListener(rhandler);
makeCabin.addItemListener(rhandler);
makeSnowman.addItemListener(rhandler);
buttonPanel.add(makeFlake);
buttonPanel.add(makeTree);
buttonPanel.add(makeGreet);
buttonPanel.add(makeCloud);
buttonPanel.add(makeCabin);
buttonPanel.add(makeSnowman);
// A ButtonGroup allows a set of JRadioButtons to be associated
// together such that only one can be selected at a time
shapeGroup = new ButtonGroup();
shapeGroup.add(makeFlake);
shapeGroup.add(makeTree);
shapeGroup.add(makeGreet);
shapeGroup.add(makeCloud);
shapeGroup.add(makeCabin);
shapeGroup.add(makeSnowman);
currShape = Figures.SNOWFLAKE;
drawPanel.setMode(Mode.NONE);
theWindow = new JFrame("Assig5 Program Card Creator");
Container c = theWindow.getContentPane();
drawPanel.setBackground(Color.lightGray);
c.add(drawPanel, BorderLayout.NORTH);
c.add(buttonPanel, BorderLayout.SOUTH);
//JMenus are created
theBar = new JMenuBar();
theWindow.setJMenuBar(theBar);
fileMenu = new JMenu("File");
editMenu=new JMenu("Edit");
newFile= new JMenuItem("New");
openFile= new JMenuItem("Open");
saveFile= new JMenuItem("Save");
saveAsFile= new JMenuItem("Save As");
saveAsJPG= new JMenuItem("Save As JPG");
printScene = new JMenuItem("Print");
endProgram = new JMenuItem("Exit");
fileMenu.add(newFile);
fileMenu.add(openFile);
fileMenu.add(saveFile);
fileMenu.add(saveAsFile);
fileMenu.add(saveAsJPG);
fileMenu.add(printScene);
fileMenu.add(endProgram);
theBar.add(fileMenu);
cut=new JMenuItem("Cut");
copy=new JMenuItem("Copy");
paste=new JMenuItem("Paste");
cut.setEnabled(false);
copy.setEnabled(false);
paste.setEnabled(false);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
theBar.add(editMenu);

printScene.addActionListener(bhandler);
endProgram.addActionListener(bhandler);
newFile.addActionListener(bhandler);
openFile.addActionListener(bhandler);
saveFile.addActionListener(bhandler);
saveAsFile.addActionListener(bhandler);
saveAsJPG.addActionListener(bhandler);
cut.addActionListener(bhandler);
copy.addActionListener(bhandler);
paste.addActionListener(bhandler);
//JPopup menu is created
popper = new JPopupMenu();
delete = new JMenuItem("Delete");
resize= new JMenuItem("Resize");
resize.addActionListener(bhandler);
delete.addActionListener(bhandler);
popper.add(delete);
popper.add(resize);
theWindow.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON
_CLOSE);
theWindow.pack();
theWindow.setVisible(true);
//flags indicate when the file should be saved and renamed
unsaved=1;
samefile=0;
}
public static void main(String [] args)
{
new Assig5();
}
//switches between what figures are being made
private class RadioHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == makeTree){
currShape = Figures.TREE;
}else if (e.getSource() == makeFlake){
currShape = Figures.SNOWFLAKE;
}else if (e.getSource() == makeGreet){
currShape = Figures.GREETING;
}else if(e.getSource()==makeCloud){
currShape=Figures.CLOUD;
}else if(e.getSource()==makeCabin){
currShape=Figures.CABIN;
}else if(e.getSource()==makeSnowman){
currShape=Figures.SNOWMAN;
}
}
}
//
//
//
//

Note how the makeShape button and moveIt menu item are handled
-- we again simply set the state in the panel so that the mouse will
actually do the work. The state needs to be set back in the mouse
listener.

private class ButtonHandler implements ActionListener


{
//this class handles all button items
public void actionPerformed(ActionEvent e)
{
//makes the shape and switches modes
if (e.getSource() == makeShape){
if (makeShape.getText().equals("Click to Draw"))
{
drawPanel.setMode(Mode.DRAW);
msg.setText("Position new shapes with mo
use");
makeShape.setText("Click to Edit");
}
else{
drawPanel.setMode(Mode.NONE);
msg.setText("Edit shapes with mouse");
makeShape.setText("Click to Draw");
}
}else if (e.getSource() == delete){
//delets selected item
boolean ans = drawPanel.deleteSelected();
if (ans){
msg.setText("Shape deleted");
drawPanel.repaint();
unsaved=0;
}
}else if (e.getSource() == printScene){
//prints the card using the Print class above
Printable thePPanel = new thePrintPanel(drawPane
l);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(thePPanel);
boolean ok = job.printDialog();
if (ok){
try {
job.print();
}
catch (PrinterException ex) {
JOptionPane.showMessageDialog(th
eWindow,"The Print Job was not successful.");
}
}
}else if (e.getSource() == endProgram){
//quits the program and saves the file
int option=JOptionPane.showConfirmDialog(theWind
ow,"Are you sure you want to quit the program?");
if(option==0){
if(unsaved==0){
saveFile();
System.exit(0);
}else{
System.exit(0);
}
}
}else if(e.getSource()==newFile){
//opens a new file and clears the screen
int option=JOptionPane.showConfirmDialog(theWind
ow,"Are you sure you want a clean slate?");
if(option==0){

if (unsaved==0){
saveFile();
}
drawPanel.deleteAll();
unsaved=0;
samefile=0;
}
}else if(e.getSource()==openFile){
//opens a file and saves the current one
if(unsaved==0){
saveFile();
unsaved=1;
}
try{
infilename=JOptionPane.showInputDialog(t
heWindow,"Please enter the name of a file to open: ");
if(infilename!=null){
drawPanel.deleteAll();
filename=infilename;
drawingFile = new Scanner(new Fi
leInputStream(infilename));
readFile(shapeList,drawingFile);
drawPanel.repaint();
}
}catch(IOException f){
JOptionPane.showMessageDialog(theWindow,
"Invalid File Name.");
}
}else if(e.getSource()==saveFile){
//saves the file under the same name
if(unsaved==0){
saveFile();
unsaved=1;
}else if(unsaved==1&&filename==null){
unsaved=0;
saveFile();
unsaved=1;
}else if(unsaved==0&&filename==null){
saveFile();
unsaved=1;
}
}else if(e.getSource()==saveAsFile){
//saves the file under a new name
samefile=0;
saveFile();
samefile=1;
}else if(e.getSource()==saveAsJPG){
//saves the screen as a JPG but does not set the screen
as being saved because this program cannot
//open up and read in an image
savePicture();
}else if(e.getSource()==cut){
//cuts the selected shape
drawPanel.cutShape();
msg.setText("Shape cut. Click on a location to p
aste.");
ccpMode=1;
paste.setEnabled(true);
unsaved=0;

}else if(e.getSource()==copy){
//copies the selected shape
drawPanel.copyShape();
msg.setText("Shape copied. Click on a location t
o paste.");
ccpMode=2;
paste.setEnabled(true);
unsaved=0;
}else if(e.getSource()==paste){
//pasts the shape that was copied or pasted
msg.setText("Shape pasted.");
drawPanel.pasteShape();
unsaved=0;
}else if(e.getSource()==resize){
//resizes the shape that was selected
String size1=JOptionPane.showInputDialog(theWind
ow,"Please enter the new size: ");
if(size1!=null){
int size=Integer.parseInt(size1);
drawPanel.resizeShape(size);
drawPanel.repaint();
unsaved=0;
}
}
}
}
// Here we are extending JPanel. This way we can use all of the
// properties of JPanel (including generating MouseEvents) and also
// add new instance data and methods, as shown below. Since this is
// an inner class, it can access instance variables from the A5Help
// class if necessary.
private class ShapePanel extends JPanel
{
// These instance variables are used to store the desired size
// of the panel. See method getPreferredSize() below.
private int prefwid, prefht;
// Store index of the selected MyShape. This allows the Shape
// to be moved and updated.
private int selindex;
private int startindex=0;
private int endindex;
// Keep track of positions where mouse is moved on the display.
// This is used by mouse event handlers when moving the shapes.
private int x1, y1, x2, y2;
private boolean popped; // has popup menu been activated?
private Mode mode;

// Keep track of the current Mode

//private int ccpMode; /*1=cut, 2=copied */


private int xccp,yccp; /*stores x and y coordinates for pasting
*/
//private MyShape newShape2 is declared up at top
public ShapePanel (int pwid, int pht)
{

shapeList = new ArrayList<MyShape>(); // create empty Ar


rayList
selindex = -1;
prefwid = pwid; // values used by getPreferredSize metho
d below
prefht = pht;

// (which is called implicitly). This e

nables
// the JPanel to request the room that it needs.
// However, the JFrame is not required to honor
// that request.
setOpaque(true);// Paint all pixels here (See API)
setBackground(Color.lightGray);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMover());
popped = false;
} // end of constructor
// This class is extending MouseAdapter. MouseAdapter is a pred
efined
// class that implements MouseListener in a trivial way (i.e. no
ne of
// the methods actually do anything). Extending MouseAdapter al
lows
// a programmer to implement only the MouseListener methods that
// he/she needs but still satisfy the interface (recall that to
// implement an interface one must implement ALL of the methods
in the
// interface -- in this case I do not need 3 of the 5 MouseListe
ner
// methods)
// Note that there is a lot of logic in this class to test for t
he
// different state conditions of the program. The idea is that
clicking
// on and releasing the mouse will do different things at differ
ent
// times in the program execution. As an alternative, you could
in fact
// have MouseListeners for different circumstances (ex: for bein
g
// in DRAW mode vs. being in NONE mode). In this case, you coul
d
// actually swap the listeners in and out as appropriate using t
he
// removeMouseListener method in addition to the addMouseListene
r method
// for the JPanel.
private class MyMouseListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
x1 = e.getX(); // store where mouse is when cli
cked

y1 = e.getY();
xccp=e.getX(); //stores coordinates for cut, cop
y paste
yccp=e.getY();
if (!e.isPopupTrigger() && (mode == Mode.NONE ||
mode == Mode.SELECTED)){
// either NONE or
if (selindex >= 0)
// SELECTED mode
{
unSelect();
// unselect previous shape
cut.setEnabled(false); //disable
s copy and paste if nothing is selected
copy.setEnabled(false);
mode = Mode.NONE;
}
selindex = getSelected(x1, y1); // find
shape mouse is
// clicked on
if (selindex >= 0)
{
mode = Mode.SELECTED;

// Now i

n SELECTED mode for shape


cut.setEnabled(true); //Copy an
d cut are enabled because a shape is slected
copy.setEnabled(true);
// Check for double-click. If so, show
dialog to update text of
// the current text shape (will do nothi
ng if shape is not a MyText)
MyShape curr = shapeList.get(sel
index);
if (curr instanceof MyText && e.
getClickCount() == 2)
{
String newText = JOption
Pane.showInputDialog(theWindow,
"Enter
new text [Cancel for no change]");
if (newText != null)
((MyText) curr).
setText(newText);
}
}
repaint();

// Make sure updates ar

e redrawn
}
else if (e.isPopupTrigger() && selindex >= 0) /
/ if button is
{
// the popup menu
popper.show(ShapePanel.this, x1, y1);
// trigger, show
popped = true;
// popup menu
}

}
public void mouseReleased(MouseEvent e)
{
if (mode == Mode.DRAW) // in DRAW mode, create t
he new Shape
{
d add it to the list of Shapes. In this

// an
// ca

se we need to distinguish between the


// sh
apes since we are calling constructors
if (currShape == Figures.TREE)
{
newShape = new Tree(x1,y1,50);
}
else if (currShape == Figures.SNOWFLAKE)
{
newShape = new Snowflake(x1,y1,1
0);
}
else if (currShape == Figures.GREETING)
{
newShape = new Greeting(x1,y1,30
);
}
else if(currShape==Figures.CLOUD){
newShape= new Cloud(x1,y1,25);
}else if(currShape==Figures.CABIN){
newShape= new Cabin(x1,y1,50);
}else if(currShape==Figures.SNOWMAN){
newShape= new Snowman(x1,y1,50);
}
addShape(newShape);
unsaved=0;
}
// In MOVING mode, set mode back to NONE and uns
elect shape (since
// the move is finished when we release the mous
e).
else if (mode == Mode.MOVING)
{
mode = Mode.NONE;
unSelect();
makeShape.setEnabled(true);
unsaved=0;
repaint();
}
else if (e.isPopupTrigger() && selindex >= 0) //
if button is
{
// the popup menu trigger, show the
popper.show(ShapePanel.this, x1, y1); //
popup menu
}
popped = false; // unset popped since mouse is
being released
}
}

// the MouseMotionAdapter has the same idea as the MouseAdapter


// above, but with only 2 methods. The method not implemented
// here is mouseMoved. The difference between the two is whethe
r or
// not the mouse is pressed at the time. Since we require a "cl
ick and
// hold" to move our objects, we are using mouseDragged and not
// mouseMoved.
private class MyMover extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
x2 = e.getX(); // store where mouse is now
y2 = e.getY();
// Note how easy moving the shapes is, since the
"work"
// is done within the various shape classes. Al
l we do
// here is call the appropriate method. However
, we don't
// want to accidentally move the selected shape
with a right click
// so we make sure the popup menu has not been a
ctivated.
if ((mode == Mode.SELECTED || mode == Mode.MOVIN
G) && !popped)
{
try{
MyShape s = shapeList.get(selind
ex);
mode = Mode.MOVING;
s.move(x2, y2);
unsaved=0;
}catch(ArrayIndexOutOfBoundsException h)
{
}
}
repaint();

// Repaint screen to show update

s
}
}
// Check to see if point (x,y) is within any of the shapes. If
// so, select that shape and highlight it so user can see. Agai
n,
// note that we do not care which shape we are selecting here -// it only matters that it has the MyShape interface methods.
// This version of getSelected() always considers the shapes fro
m
// beginning to end of the ArrayList. Thus, if a shape is "unde
r"
// or "within" a shape that was previously created, it will not
// be possible to select the "inner" shape. In your assignment
you
// must redo this method so that it allows all shapes to be sele
cted.
// Think about how you would do this.

//instance variable that stores the start of the for loop


//start point changes each time and then loops back
//using mod function of a.size
//This loop allows for continuous selection of overlapping objec
ts
private int getSelected(double x, double y){
for (int i=startindex ; i < shapeList.size(); i++){
startindex=(i+1)%(shapeList.size());
if (shapeList.get(startindex).contains(x, y)){
shapeList.get(startindex).highlight(true
);
return startindex;
}
}
return -1;
}
//resizes selected shape
public void resizeShape(int size){
if(selindex>=0){
shapeList.get(selindex).resize(size);
selindex=-1;
}
}
//cuts the shape
public void cutShape(){
if(selindex>=0){
newShape2=shapeList.get(selindex);
shapeList.remove(selindex);
newShape2.highlight(false);
}
}
//copies the shape
public void copyShape(){
String [] shapeinfo;
String sinfo=null;
String textn=null;
int xn;
int yn;
int sizen;
//uses a similar method for reading in the file to accur
ately make a new shape
if(selindex>=0){
sinfo=shapeList.get(selindex).saveData();
shapeinfo=sinfo.split(":");
if (shapeinfo[0].equals("Tree")){
xn=Integer.parseInt(shapeinfo[1]);
yn=Integer.parseInt(shapeinfo[2]);
sizen=Integer.parseInt(shapeinfo[3]);
newShape2 = new Tree(xn,yn,sizen);
}else if (shapeinfo[0].equals("Snowflake")){
xn=Integer.parseInt(shapeinfo[1]);
yn=Integer.parseInt(shapeinfo[2]);
sizen=Integer.parseInt(shapeinfo[3]);
newShape2 = new Snowflake(xn,yn,sizen);
}else if (shapeinfo[0].equals("Greeting")){
xn=Integer.parseInt(shapeinfo[1]);
yn=Integer.parseInt(shapeinfo[2]);
sizen=Integer.parseInt(shapeinfo[3]);
textn=shapeinfo[4];

newShape2 = new Greeting(xn,yn,sizen,tex


tn);
}else if(shapeinfo[0].equals("Cloud")){
xn=Integer.parseInt(shapeinfo[1]);
yn=Integer.parseInt(shapeinfo[2]);
sizen=Integer.parseInt(shapeinfo[3]);
newShape2= new Cloud(xn,yn,sizen);
}else if(shapeinfo[0].equals("Cabin")){
xn=Integer.parseInt(shapeinfo[1]);
yn=Integer.parseInt(shapeinfo[2]);
sizen=Integer.parseInt(shapeinfo[3]);
newShape2= new Cabin(xn,yn,sizen);
}else if(shapeinfo[0].equals("Snowman")){
xn=Integer.parseInt(shapeinfo[1]);
yn=Integer.parseInt(shapeinfo[2]);
sizen=Integer.parseInt(shapeinfo[3]);
newShape2= new Snowman(xn,yn,sizen);
}
}
}
//pastes the shape based on whether it was copied or cut
public void pasteShape(){
if(ccpMode==1){
newShape2.move(xccp,yccp);
addShape(newShape2);
}else if(ccpMode==2){
newShape2.move(xccp,yccp);
addShape(newShape2);
unSelect();
}
selindex=-1;
repaint();
}
//unselects the shape
public void unSelect(){
try{
if (selindex >= 0){
shapeList.get(selindex).highlight(false)
;
selindex = -1;
}
}catch(IndexOutOfBoundsException e){
}
}
//deletes the selected shape
public boolean deleteSelected()
{
if (selindex >= 0)
{
shapeList.remove(selindex);
selindex = -1;
return true;
}
else return false;
}
//deletes all shapes in the ArrayList and clears the screen
public void deleteAll(){
for(int iii=shapeList.size()-1; iii>=0; iii--){
shapeList.remove(iii);

repaint();
}
}
public void setMode(Mode newMode)
{
mode = newMode;
}

// set Mode

private void addShape(MyShape newshape)


// Add shape
{
shapeList.add(newshape);
repaint();
// repaint so we can see new shape
}
// Method called implicitly by the JFrame to determine how much
// space this JPanel wants. Be sure to include this method in
// your program so that your panel will be sized correctly.
public Dimension getPreferredSize()
{
return new Dimension(prefwid, prefht);
}
// This method enables the shapes to be seen. Note the paramete
r,
// which is implicitly passed. To draw the shapes, we in turn
// call the draw() method for each shape. The real work is in t
he draw()
// method for each MyShape
public void paintComponent (Graphics g)
{
super.paintComponent(g);
// don't forget this li
ne!
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < shapeList.size(); i++)
{
shapeList.get(i).draw(g2d);
}
}
} // end of ShapePanel
//saves the file
private void saveFile(){
try{
//makes sure the name is not null
if(samefile==0||filename==null){
filename=JOptionPane.showInputDialog(theWindow,"
Please enter the name of a file: ");
}
try{
//displayes what the file name is the that program is sa
ving to
if(filename!=null){
JOptionPane.showMessageDialog(theWindow,
"Saving file: "+filename);
}
FileWriter outputFile=new FileWriter(filename);
PrintWriter newDrawing=new PrintWriter(outputFil
e);
for(int ii=0;ii<shapeList.size();ii++){
newDrawing.println(shapeList.get(ii).sav

eData());
}
newDrawing.close();
samefile=1;
}catch(NullPointerException e){
}
}catch(IOException e)
{
JOptionPane.showMessageDialog(theWindow,"The file was no
t saved.");
}
}
//reads in a card from a saved file
private void readFile(ArrayList<MyShape> shapeList, Scanner drawingFile)
{
String shapeS=null;
String text=null;
String [] A;
int xr;
int yr;
int sizer;
//while the file has a line to read it is read into a string
while(drawingFile.hasNextLine()==true){
shapeS=drawingFile.nextLine();
//the string is then split up into an array
A=shapeS.split(":");
//a series of if statements create a new shape based on
what was read in from the file
if (A[0].equals("Snowman")){
xr=Integer.parseInt(A[1]);
yr=Integer.parseInt(A[2]);
sizer=Integer.parseInt(A[3]);
newShape=new Snowman(xr,yr,sizer);
}else if(A[0].equals("Cloud")){
xr=Integer.parseInt(A[1]);
yr=Integer.parseInt(A[2]);
sizer=Integer.parseInt(A[3]);
newShape=new Cloud(xr,yr,sizer);
}else if(A[0].equals("Snowflake")){
xr=Integer.parseInt(A[1]);
yr=Integer.parseInt(A[2]);
sizer=Integer.parseInt(A[3]);
newShape=new Snowflake(xr,yr,sizer);
}else if(A[0].equals("Cabin")){
xr=Integer.parseInt(A[1]);
yr=Integer.parseInt(A[2]);
sizer=Integer.parseInt(A[3]);
newShape=new Cabin(xr,yr,sizer);
}else if(A[0].equals("Tree")){
xr=Integer.parseInt(A[1]);
yr=Integer.parseInt(A[2]);
sizer=Integer.parseInt(A[3]);
newShape=new Tree(xr,yr,sizer);
}else if(A[0].equals("Greeting")){
xr=Integer.parseInt(A[1]);
yr=Integer.parseInt(A[2]);
sizer=Integer.parseInt(A[3]);
text=A[4];
newShape=new Greeting(xr,yr,sizer,text);

}
//the new shape is added to the ArrayList
shapeList.add(newShape);
}
}
//saves the pane as an image using a BufferedImage
private void savePicture(){
String imagefile=JOptionPane.showInputDialog(theWindow,"Please e
nter the name of the image file: ");
if(imagefile != null){
BufferedImage img = new BufferedImage(drawPanel.getWidth
(),drawPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
drawPanel.printAll(img.getGraphics());
try{
ImageIO.write(img,"jpg",new File(imagefile));
}catch(IOException g){
JOptionPane.showMessageDialog(theWindow,"The ima
ge was not saved.");
}
}
}
}

Вам также может понравиться