Status
Not open for further replies.

UnforgivenGamer

Estimable
Dec 15, 2014
8
0
4,520
Hey guys. I usually use this website for computer hardware research, however I was wondering if anyone out there could help me with a problem I'm having. I'm in a computer science class in college and I can't figure out how to write this program in Java. I've tried many things, however nothing seems to be working. If anyone could help me out that would be great. Thanks!
 
Solution

USAFRet

Illustrious
Moderator


Generally, homework requests require that you put up what you are trying to do, and where it is failing.

Some code, and what exactly does not work.
We won't do the whole thing for you.
 

UnforgivenGamer

Estimable
Dec 15, 2014
8
0
4,520

This is what I have so far

import wheelsunh.users.*;
import java.awt.Color;
import java.awt.event.MouseEvent;

/**.
* Creature.java:
* Implementing an Interface
* @author Joshua Rush
*/

public class Creature extends ShapeGroup
{
// Local variables that define the location of the creature's body
protected int bodyX = 0; protected int bodyY = 0;
// Local variables that reference the shapes being used
protected Rectangle rect;
protected Rectangle rect2;
protected Rectangle rect3;
protected Line lin;
protected Line lin2;
protected RoundedRectangle rdrc;
protected Ellipse circ;
protected Ellipse circ2;
protected int x = 0;
protected int y = 0;
protected Color color;

//------------Creature-------------------------
/**.
* Creates a default Creature
*/
public Creature ( )
{

}

//----------creates creature location----------
/**.
* creates creature at a certain location
* @param y int
* @param x int
*/
public Creature( int x, int y )
{

this.x = x;
this.y = y;
color = Color.RED;

// Creates the objects that appear under the creature's body
rect = new Rectangle ( Color.GREEN ); rect.setRotation ( 45 );
rect.setSize ( 20, 20 ); rect.setLocation ( x + 51, y + 16 );
rect2 = new Rectangle ( Color.GREEN ); rect2.setRotation ( 45 );
rect2.setSize ( 20, 20 ); rect2.setLocation ( x - 22, y + 16 );
lin = new Line ( ); lin.setSize ( 30, 20 ); lin.setRotation ( 45 );
lin.setLocation ( x + 5, y - 10 );
lin2 = new Line ( ); lin2.setSize ( 30, 20 ); lin2.setRotation ( 135 );
lin2.setLocation ( x + 15, y - 10 );
// Creates the creature's body and objects that appear above the body
rdrc = new RoundedRectangle ( x + bodyX, y + bodyY );
rdrc.setColor ( color ); rdrc.setRotation ( 45 );
rect3 = new Rectangle ( Color.BLACK ); rect3.setSize ( 25, 10 );
rect3.setLocation ( x + 13, y + 28 );
circ = new Ellipse ( Color.BLACK ); circ.setSize ( 10, 10 );
circ.setLocation ( x + 10, y + 10 );
circ2 = new Ellipse ( Color.BLACK ); circ2.setSize ( 10, 10 );
circ2.setLocation ( x + 25, y + 10 );
}

//-----------add shape-----------------
/**.
* adds shapes to the ShapeGroup
* @param s AbstractShape
*/
public void add ( AbstractShape s )
{
add ( rect );
add ( rect2 );
add ( rect3 );
add ( lin );
add ( lin2 );
add ( rdrc );
add ( circ );
add ( circ2 );
}

//----------MousePressed-------------------------
/**.
* changes color when mouse is pressed
* @param e MouseEvent
*/
public void mousePressed ( MouseEvent e )
{

}

//----------Creature color---------
/**.
*creates the creature with a certain color
*@param color Color
*/
public Creature ( Color color )
{
x = 0;
y = 0;
this.color = color;

// Creates the objects that appear under the creature's body
rect = new Rectangle ( Color.GREEN ); rect.setRotation ( 45 );
rect.setSize ( 20, 20 ); rect.setLocation ( x + 51, y + 16 );
rect2 = new Rectangle ( Color.GREEN ); rect2.setRotation ( 45 );
rect2.setSize ( 20, 20 ); rect2.setLocation ( x - 22, y + 16 );
lin = new Line ( ); lin.setSize ( 30, 20 ); lin.setRotation ( 45 );
lin.setLocation ( x + 5, y - 10 );
lin2 = new Line ( ); lin2.setSize ( 30, 20 ); lin2.setRotation ( 135 );
lin2.setLocation ( x + 15, y - 10 );
// Creates the creature's body and objects that appear above the body
rdrc = new RoundedRectangle ( x + bodyX, y + bodyY );
rdrc.setColor ( color ); rdrc.setRotation ( 45 );
rect3 = new Rectangle ( Color.BLACK ); rect3.setSize ( 25, 10 );
rect3.setLocation ( x + 13, y + 28 );
circ = new Ellipse ( Color.BLACK ); circ.setSize ( 10, 10 );
circ.setLocation ( x + 10, y + 10 );
circ2 = new Ellipse ( Color.BLACK ); circ2.setSize ( 10, 10 );
circ2.setLocation ( x + 25, y + 10 );
}

//------------color-----------
/**.
*sets the current color
*@param color Color
*/
public void setColor ( Color color )
{
this.color = color;
rdrc.setColor ( this.color );
}

//-------------get color-----------
/**.
*gets the current color
*@return ( )
*/
public Color getColor ( )
{
return color;
}

//--------------main--------
/**.
*creates tests all the methods
*@param args []String
*/
public static void main( String[] args )
{
new Frame ( );
Creature app = new Creature ( 69, 69 );
System.out.println ( "Color: " + app.getColor ( ) );
}

} //End of Class Creature

I'm trying to figure out how to write the mousePressed method, however I cannot figure out a solution. The way I thought it was done was to have something like this.

public void mousePressed ( MouseEvent e )
{
rdrc.setColor ( );
}

And I thought it would call that method upon the mousePress. rdrc is the shape that is the main body of the class I created and I need it to change color when the mouse is pressed. Any ideas?

 

TuGeorge

Commendable
Nov 23, 2016
1
0
1,520


Hi,

You might want to learn smthing about the java.awt package, what a Frame is and how it receives events from the JVM.
- the first line in main: "new Frame();" - you need to keep a reference to that frame in a variable.
- then you need to register a MouseListener ( one example here )
That MouseListener is an interface that you need to implement (might be an anonymous class or your Creature class) and that will receive mouse notifications and take the actions that you need to take.

One other piece of advice: for production you might consider learning Swing or JavaFX (awt is so outdated)
 
Solution
Status
Not open for further replies.