Tom
Posts: 3
Nickname: snidely
Registered: Apr, 2002
|
|
Re: Array woes
|
Posted: Apr 13, 2002 5:11 AM
|
|
I'm still having a few troubles. This is going to take up a lot of space, so please be patient :(
If you want to get to my problem with the accursed thing, skip right down to the end.
I've added Diamond, House, and Face classes. They're all connected properly with extends and whatnot.
The file where the beef of the code is, is ShapesB. It seems to use an abstract class MyShapes to call up the individual shapes such as Circle, Square, Face etc.
[quote] // This applet allows the user to manipulate simple shapes by // dragging and resizing them.
import java.awt.*; import java.applet.Applet; import java.awt.event.*;
public class ShapesB extends Applet implements MouseListener, MouseMotionListener {
// MouseMotionListener specifies mouseDragged and mouseMoved, // MouseListener specifies mousePressed and the other methods // defined below. You have to define them even if they do // nothing!
private MyShape chosenShape; // The shape selected by the user
// Declare two Circles and two Squares private Circle circle = new Circle(40, 40, 30, Color.red); private Diamond diamond = new Diamond(80, 80, 30, Color.green); private Square square = new Square(120, 120, 30, Color.blue); private House house = new House(160, 160, 30, Color.yellow); private Face pinkface = new Face(190, 190, 30, Color.pink); private Face blackface = new Face(220, 220, 30, Color.black);
private int lastX, lastY; // For handling mouse drag events, These will remember the previous // x, y mouse position.
private String shapeName = "Nothing"; // To display current shape name or "Nothing" if no shape is selected
public void init() { addMouseListener(this); addMouseMotionListener(this); }
public void mouseDragged(MouseEvent e) { // This event happens several times whilst mouse is being dragged. // If one of the four shapes is selected, we move it by the // difference in position of the mouse pointer between events. if (chosenShape != null) { int x = e.getX(), y = e.getY(); chosenShape.move(x - lastX, y - lastY); lastX = x; lastY = y; repaint(); } }
public void mousePressed(MouseEvent e) { int x = e.getX(), y = e.getY(); lastX = x; lastY = y; // Did user press mouse over one of our four shapes? // Note that this code assumes that circle is on top, // diamond next, square next and house underneath. if (circle.contains(x, y)) { shapeName = "Circle"; chosenShape = circle; } else if (diamond.contains(x, y)) { shapeName = "Diamond"; chosenShape = diamond; } else if (square.contains(x, y)) { shapeName = "Square"; chosenShape = square; } else if (house.contains(x, y)) { shapeName = "House"; chosenShape = house; } else if (pinkface.contains(x, y)) { shapeName = "Pink face"; chosenShape = pinkface; } else if (blackface.contains(x, y)) { shapeName = "Black face"; chosenShape = blackface; } else { // no shape selected shapeName = "Nothing"; chosenShape = null; repaint(); return; }
// Test to see if Ctrl or Shift key is being pressed. If so,
if (e.isControlDown()) chosenShape.changeSize(10); else if (e.isShiftDown()) chosenShape.changeSize(-10); repaint(); }
// We need the following empty declarations of mouse // event handlers to satisfy the MouseListener and // MouseMotionListener interfaces public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {}
public void paint(Graphics g) { // draw the shapes with the black face on top house.draw(g); square.draw(g); diamond.draw(g); circle.draw(g); pinkface.draw(g); blackface.draw(g); g.drawString(shapeName, 10, 20); } } [/quote]
This is MyShape coming up now:
[quote] import java.awt.*; abstract class MyShape {
protected int x, y, size; protected Color color;
MyShape(int x, int y, int size, Color color) { this.x = x; this.y = y; this.size = size; this.color = color; } public int getX() { return x; } public int getY() { return y; } public void changeSize(int change) { size += change; if (size < 0) size = 0; // a negative size doesn't make sense } public void move(int x, int y) { this.x += x; this.y += y; } public boolean contains(int x, int y) { return (this.x <= x) && (this.y <= y) && (x <= this.x + size) && (y <= this.y + size); }
// We don't know how to draw each type of shape here so the // following method is defined abstract: abstract void draw(Graphics g); // Note that each subclass MUST implement draw if it is not // itself to be abstract }
So far, this program works. However, my assignment paper says:
[quote] Use an array shape of MyShapes rather than individual variables circle, square, face1 etc:
private Shape shape[] = new Shape[6];
This should be populated with shapes in the init() method: shape[0] = new Circle(40, 40, 30, Color.red); ... ... shape[5] = new House(240, 240, 30, Color.yellow); [/quote]
I'm not sure exactly what this means, and it doesn't refer back to this part. Where do I put private Shape shape[] = new Shape[6]? Where do I put the code telling it what to store in the array? How do I get it to recognise the symbols? (Circle, Square etc.)
I apologise for the length of this post, and to Matt - I'm too new at this to adapt the info from the other thread to my program. Feedback will be greatly appreciated.
|
|