//SwingDrawingMsc.java //Mike parr 1/12/04 import java.util.*; // for array list import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDrawingMsc extends JFrame implements ActionListener, MouseListener { private JMenu fileMenu; private JMenuItem saveItem, openItem, saveAsItem, quitItem; private JPanel controlPanel; private JButton newTextButton, newCircleButton,newVertTextButton, newSquareButton, moveButton; private JButton newLineButton, deleteButton,fillButton; private JButton widerButton, narrowButton,tallerButton, shorterButton; private JButton upButton, downButton,leftButton, rightButton; private JButton toButton, fromButton; private JPanel textPanel, editPanel, drawingPanel; private JTextArea messageArea;// e.g messageArea.append("\n value of x is "+x); private JTextField editField; private JTextField statusField; private JMenuBar menuBar; private ArrayList shapes = new ArrayList(); // holds all the shapes private boolean someShapeSelected = false; // has user clicked on a shape? private int selectedShapeNumber; // the currently-selected shape object // ------------------------------------------------------------- public static void main(String [] args) { SwingDrawingMsc frame = new SwingDrawingMsc();//new frame.setSize(700, 600); frame.initGUI(); frame.setVisible(true); } public void initGUI() { setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane(); window.setLayout(new BorderLayout() ); menuBar=new JMenuBar(); fileMenu = new JMenu("File"); saveItem = new JMenuItem("save"); fileMenu.add(saveItem); saveItem.addActionListener(this); openItem = new JMenuItem("open"); fileMenu.add(openItem); openItem.addActionListener(this); saveAsItem = new JMenuItem("save as"); fileMenu.add( saveAsItem); saveAsItem.addActionListener(this); quitItem = new JMenuItem("quit"); fileMenu.add(quitItem); quitItem.addActionListener(this); menuBar.add(fileMenu); // end of menu setup controlPanel=new JPanel(); controlPanel.setLayout(new GridLayout(13,2) ); // make a grid of buttons newTextButton = new JButton("text "); newTextButton.addActionListener(this); controlPanel.add(newTextButton); newCircleButton = new JButton("circle"); newCircleButton.addActionListener(this); controlPanel.add(newCircleButton); newSquareButton = new JButton("square"); controlPanel.add(newSquareButton); newSquareButton.addActionListener(this); newLineButton = new JButton("line"); newLineButton.addActionListener(this); controlPanel.add(newLineButton); upButton = new JButton("up"); upButton.addActionListener(this); controlPanel.add(upButton); leftButton = new JButton("left"); leftButton.addActionListener(this); controlPanel.add(leftButton); downButton = new JButton("down"); downButton.addActionListener(this); controlPanel.add(downButton); rightButton = new JButton("right"); rightButton.addActionListener(this); controlPanel.add(rightButton); deleteButton = new JButton("delete"); deleteButton.addActionListener(this); controlPanel.add(deleteButton); fillButton = new JButton("fill/unfill"); fillButton.addActionListener(this); controlPanel.add(fillButton); widerButton = new JButton("wider"); widerButton.addActionListener(this); controlPanel.add(widerButton); narrowButton = new JButton("narrow"); narrowButton.addActionListener(this); controlPanel.add(narrowButton); tallerButton = new JButton("taller"); tallerButton.addActionListener(this); controlPanel.add(tallerButton); shorterButton = new JButton("shorter"); shorterButton.addActionListener(this); controlPanel.add(shorterButton); newVertTextButton = new JButton("vert text"); newVertTextButton.addActionListener(this); controlPanel.add(newVertTextButton); toButton = new JButton("to shape"); toButton.addActionListener(this); controlPanel.add(toButton); fromButton = new JButton("from shape"); fromButton.addActionListener(this); controlPanel.add(fromButton); textPanel=new JPanel(); textPanel.setLayout(new FlowLayout() ); textPanel.add(new JLabel("status:")); statusField = new JTextField("draws square, circle, moves them up "+ "(select one first",50); textPanel.add(statusField); // for messages, prompts, debugging... messageArea = new JTextArea("messageArea - for debug info...."+ "\nClick in centre of a shape, the click on Up.", 6,50); messageArea.setLineWrap(true); JScrollPane areaScrollPane = new JScrollPane(messageArea); textPanel.add(areaScrollPane); editPanel = new JPanel(); editPanel.setLayout(new FlowLayout() ); editPanel.add(new JLabel("Edit text:")); editField = new JTextField(50); editPanel.add(editField); drawingPanel=new JPanel(); drawingPanel.setPreferredSize(new Dimension(666,777)); drawingPanel. setBackground(Color.white); window.add("South",textPanel); window.add("West",controlPanel); window.add("Center", drawingPanel); window.add("North",editPanel); setJMenuBar(menuBar); drawingPanel.addMouseListener(this); // detect clicks on drawing area window.setVisible(true); } // end of initGUI // ------------------------------------------------------ // --------------------------------------------------------------- //--------------------------------------------------------------------- //----------------------------------------------------------------- //MouseListener implementation - all 5 methods needed public void mouseClicked (MouseEvent event){ // here on a click on drawing findWhichShape(event); } // all these empty methods needed for mouse-clicks public void mouseReleased (MouseEvent e){ } public void mousePressed (MouseEvent e){ } public void mouseEntered (MouseEvent e){ } public void mouseExited (MouseEvent e){ } // --------------------------------------------------------- // handle buttons etc public void actionPerformed(ActionEvent event) { if(event.getSource()==newCircleButton) { //handle click on newCircleButton shapes.add(new Circle(10,10,200)); //create new circle, add it to shapes } if(event.getSource()==newTextButton) { //handle click on new TextButton - a square initially } if(event.getSource()==newSquareButton) { //handle click on newSquareButton } if(event.getSource()==upButton) { //handle click on upButton // get the currently-selected shape, and invoke its move() method // (but only if the user has selected a shape first) if(someShapeSelected) { Shape tempShape = (Shape)shapes.get(selectedShapeNumber); tempShape.move(0,-10); } } if(event.getSource()==downButton) { //handle click on downButton } if(event.getSource()==leftButton) { //handle click on leftButton } if(event.getSource()==rightButton) { //handle click on rightButton } if(event.getSource()==newLineButton) { //handle click on newLineButton } if(event.getSource()==deleteButton) { //handle click on deleteButton if(someShapeSelected) shapes.remove(selectedShapeNumber); } if(event.getSource()==fillButton) { //handle click on fillButton } if(event.getSource()==widerButton) { //handle click on Button } if(event.getSource()==narrowButton) { //handle click on Button } else if(event.getSource()==tallerButton) { //handle click on Button } if(event.getSource()==shorterButton) { //handle click on Button } if(event.getSource()==newVertTextButton) { //handle click on Button } if(event.getSource()==toButton) { //handle click on Button } if(event.getSource()==fromButton) { //handle click on Button } // end of handling buttons // handle menu: if(event.getSource()==saveItem) { //handle click on "save" } if(event.getSource()==openItem) { //handle click on "open" } if(event.getSource()==saveAsItem) { //handle click on save as } if(event.getSource()==quitItem) { //handle click on "quit" System.exit(0); } drawAllShapes(); // always } // end of actionListener //----------------------------------------------------- // finds distance between2 points. e.g d=distanceBetween(a1,b1,a2,b2) private int distanceBetween(int x1, int y1, int x2, int y2) { // pythag - (double is a long int - to avoid overflow) double temp = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1); return (int) (Math.sqrt(temp)); // return it as an int } //--------------------------------------------------------- // look through the shapes to find closest private void findWhichShape(MouseEvent event){ // get mouse coords int smallestLength=10000; int mouseX = event.getX(); // get x,y of mouse click position int mouseY = event.getY(); messageArea.append("\n number of shapes="+shapes.size()); // demo of debug if (shapes.size()!=0) { // there are some shapes to examine for(int n = 0; n