Академический Документы
Профессиональный Документы
Культура Документы
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.*;
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.
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;
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
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
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.
repaint();
}
}
public void setMode(Mode newMode)
{
mode = newMode;
}
// set Mode
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.");
}
}
}
}