The Artima Developer Community
Sponsored Link

Java Answers Forum
Array woes

4 replies on 1 page. Most recent reply: Apr 13, 2002 5:11 AM by Tom

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Tom

Posts: 3
Nickname: snidely
Registered: Apr, 2002

Array woes Posted: Apr 12, 2002 8:19 AM
Reply to this message Reply
Advertisement
The program I'm making is structured like this:

ShapesB
...|
...|
MyShapes
...|....|
...|....|
Circle Square

What it does is this: In ShapesB, I can create a circle or a square like so:

<quote>
private MyShape chosenShape; // The shape selected by the user

// Declare a red circle and a blue Square
private Circle circle1 = new Circle(40, 40, 30, Color.red);
private Square square1 = new Square(120, 120, 30, Color.blue);
</quote>

(chosenShape is a variable used for when the user clicks on a shape.)

They get drawn like so:

<quote>public void paint(Graphics g) {
// draw the shapes with the black face on top
square1.draw(g);
circle1.draw(g);
}
</quote>

Now, I want to replace this rather clumsy method of creating shapes with an array , shape[]. Any ideas?

I am pretty much a newbie when it comes to java, so please bear with me. :(

If you want the whole code, just ask.

Thanks in advance.


Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: Array woes Posted: Apr 12, 2002 8:45 AM
Reply to this message Reply
I take it that Circle and Square extend MyShapes? Create a MyShapes array or a vector. If you know the max size you want and don't mind including the extra code to resize the array if (and when) you need to, use an array. Otherwise you a vector. A vector is like an array, but only holds objects of type Object, and you don't have to worry about the size or the vector. If you are using a vector, you have to use the <code>instanceof</code> keyword when you're getting an object from the vector. Here is some sample code (based on the assumptions that Circle and Square extend MyShapes):
<pre>
private Vector shapesVector = new Vector();

private MyShape chosenShape; // The shape selected by the user

// Declare a red circle and a blue Square
private Circle circle1 = new Circle(40, 40, 30, Color.red);

shapesVector.add(circle1);

private Square square1 = new Square(120, 120, 30, Color.blue);

shapesVector.add(square1);

//To get a specific shape.
chosenShape = shapesVector.elementAt(0); //for circle1

//to draw the shape
chosenShape.draw(g)
/* Each subclass of MyShapes would have to have a
* paint method for this to work. Then, when the above
* line is executed, the JVM first looks to see if
* there is a draw method in the class of the object
* that chosenShape is referencing. That is, in this
* case, instead of looking to the MyShapes class,
* because chosenShape is referencing a Circle object,
* the JVM will look to the Circle class for a draw
* method.
*/
</pre>
Hope this helps.

Hiran

> The program I'm making is structured like this:
>
> ShapesB
> ...|
> ...|
> MyShapes
> ...|....|
> ...|....|
> Circle Square
>
> What it does is this: In ShapesB, I can create a
> circle or a square like so:
>
> <quote>
> private MyShape chosenShape; // The shape selected by
> the user
>
> // Declare a red circle and a blue Square
> private Circle circle1 = new Circle(40, 40, 30,
> Color.red);
> private Square square1 = new Square(120, 120, 30,
> Color.blue);
> </quote>
>
> (chosenShape is a variable used for when the user
> clicks on a shape.)
>
> They get drawn like so:
>
> <quote>public void paint(Graphics g) {
> // draw the shapes with the black face on
> face on top
> square1.draw(g);
> circle1.draw(g);
> }
> </quote>
>
> Now, I want to replace this rather clumsy method of
> creating shapes with an array , shape[]. Any ideas?
>
> I am pretty much a newbie when it comes to java, so
> please bear with me. :(
>
> If you want the whole code, just ask.
>
> Thanks in advance.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Array woes Posted: Apr 12, 2002 9:45 AM
Reply to this message Reply
Sounds like you are in Patti's class. Have a look at the "Interface Question" thread, where the same problem is discussed: http://www.artima.com/forums/flat.jsp?forum=1&thread=774

Tom

Posts: 3
Nickname: snidely
Registered: Apr, 2002

Re: Array woes Posted: Apr 12, 2002 11:17 AM
Reply to this message Reply
In Reply To Hiran
Cheers, I'll see how well that works.

In Reply To Matt
Hmm, Patti's task is somewhat similar to mine. Creepy!

Thanks for the helpful answers, guys. I'll see if I cn get the blasted thing working. :)

Tom

Posts: 3
Nickname: snidely
Registered: Apr, 2002

Re: Array woes Posted: Apr 13, 2002 5:11 AM
Reply to this message Reply
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.

Flat View: This topic has 4 replies on 1 page
Topic: Dll to JSp Previous Topic   Next Topic Topic: is it possible to copy dynamic data from applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use