Hey all,
1st let me say, this IS a school assignment, so I don't expect you to answer directly. However I would like a nudge or two in the right direction so I can actually complete the assignment.
Okay. We have to write a program which uses anonymous inner classes of Mouse Events to change the colour of a shape in a JPanel.
So...... this is what I have......
Circle class:
import java.awt.*; // Class definition public class Circle extends Point { protected int size; // Constructor method declaration public Circle() { super(); // set size and offset y co-ordinate to display below point size = 50; yPos = 100; } // Method to update new size of the circle public void reSize(int newSize) { size = size + newSize; } // An overiding method to display circle on screen as inherited method from class Point not suitable public void display(Graphics g) { // Call method fillOval from class Graphics to draw the circle of variable diameter of size g.fillOval(xPos, yPos, size, size); } }
This just creates a circle from class called point. Simple enough.
Point class:
import java.awt.*; // Class definition public class Point { protected int xPos, yPos; // Constructor method declaration public Point() { xPos = 100; yPos = 100; } // Method to update new position of point on screen public void moveXY(int newX, int newY) { xPos = xPos + newX; yPos = yPos + newY; } // Cannot resize a point but method required to support polymorphism public void reSize(int newSize) { } // Method to print X,Y co-ordinates of point on screen public void outputXYCoords(Graphics g) { g.drawString("X Coord = " + xPos + " Y Coord = " + yPos, 5, yPos-10); } // Method to draw point on screen at new X.Y position public void display(Graphics g) { g.fillOval(xPos, yPos, 5, 5); } }
Again, not very complicated.
Main code:
import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.awt.Color.*; import javax.swing.*; import javax.swing.event.*; // Class definition public class PolyMorphDemo extends JPanel implements ActionListener { private JLabel mouseStatus; private int startX, startY, endX, endY, diffX, diffY; private JButton makeBigger, makeSmaller; private JComboBox shapeChoice; private Point firstPoint; private Circle firstCircle; private Square firstSquare; private Doughnut firstDoughnut; private Point aShape; // Constructor method declaration public PolyMorphDemo() { makeBigger = new JButton("Make Bigger"); add(makeBigger); makeBigger.addActionListener(this); makeSmaller = new JButton("Make Smaller"); add(makeSmaller); makeSmaller.addActionListener(this); shapeChoice = new JComboBox(); shapeChoice.addItem("Point"); shapeChoice.addItem("Square"); shapeChoice.addItem("Circle"); shapeChoice.addItem("Doughnut"); add(shapeChoice); shapeChoice.addActionListener(this); // Instantiate shape classes firstPoint = new Point(); firstCircle = new Circle(); firstSquare = new Square(); firstDoughnut = new Doughnut(); // Initially assign aShape a Point object aShape = firstPoint; // Create a new window using the Swing class JFrame and add this panel makeFrame(); } public void actionPerformed(ActionEvent e) { // Manipulate respective object when buttons pressed mouseStatus = new JLabel(); add(mouseStatus); this.addMouseListener(new MouseAdapter() { // This is the bit I'm having trouble with - this simply doesn't implement Color color = Color.darkGray; public void paint(Graphics g) { g.setColor(this.color); } // The next bit works fine apart from the this.color line - it just doesn't recolour the shape to gray public void mousePressed(MouseEvent me){ mouseStatus.setText("Mouse pressed X: " + me.getX() + " Y: " + me.getY()); startX = me.getX(); startY = me.getY(); this.color = Color.darkGray; repaint(); } }); this.addMouseListener(new MouseAdapter() { // Same problem as above Color color = Color.black; public void paint(Graphics g) { g.setColor(this.color); } // Same as above - this moves the object fine public void mouseReleased(MouseEvent me) { mouseStatus.setText("Mouse released X: " + me.getX() + " Y: " + me.getY()); endX = me.getX(); endY = me.getY(); diffX = endX - startX; diffY = endY - startY; mouseStatus.setText("Mouse has moved X: " + diffX + " Y: " + diffY); aShape.moveXY(diffX,diffY); this.color = Color.black; repaint(); } }); if (e.getSource() == makeBigger) { aShape.reSize(20); } else if (e.getSource() == makeSmaller) { aShape.reSize(-20); } // checkbox assigns object to aShape else if (e.getSource() == shapeChoice) { if (shapeChoice.getSelectedItem().equals("Circle")) aShape = firstCircle; else if (shapeChoice.getSelectedItem().equals("Square")) aShape = firstSquare; else if (shapeChoice.getSelectedItem().equals("Point")) aShape = firstPoint; else if (shapeChoice.getSelectedItem().equals("Doughnut")) aShape = firstDoughnut; } // Ask Java to repaint the window to reflect the updates made repaint(); } // Provide this method to instruct Java what to do when repainting the window public void paintComponent(Graphics g) { super.paintComponent(g); // Call the respective methods from respective class to print the new co-ordinate values aShape.outputXYCoords(g); // Call the respective methods from respective class to redraw the shape aShape.display(g); } // Create a window frame public void makeFrame() { // Instantiate a window frame using Swing class JFrame JFrame frame = new JFrame("PolyMorphDemo"); // When the window is closed terminate the application frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // set initial size of window frame.setSize(800, 600); // add the current object to the centre of the frame and make visible frame.getContentPane().add(this, BorderLayout.CENTER); frame.setVisible(true); } }
As you can tell, there are also 2 other classes (square and doughnut) which create a square and a doughnut (who'd have thought it, eh? ) in place of a circle or point.
So basically, every piece of functionality (moving according to the mouse drag), making bigger, smaller etc. works.
The only problem is that I simply cannot get the object to change colour to gray when the mouse button is pressed, and turn back to black when it's released. I've tried several avenues to get this working, and none have worked, so if someone could provide a little aid, that'd be very much appreciated.
Thanks,
Tom.