Help !! Moving Blobs
Moving Blobs/Blob.java
Moving Blobs/Blob.java
/*
* Purpose: to define a single Blob object.
*
* Author: CompSci 101
* Date Written: May, 2013
*/
import
java
.
awt
.
*
;
public
class
Blob
{
public
static
final
int
BLOB_SIZE
=
Ex5JPanel
.
BLOB_SIZE
;
public
static
final
Point
CENTRE_POINT
=
Ex5JPanel
.
CENTRE_POINT
;
public
static
final
Rectangle
GAME_AREA
=
Ex5JPanel
.
GAME_AREA
;
public
static
final
Rectangle
LEFT_OF_GAME_AREA
=
Ex5JPanel
.
LEFT_OF_GAME_AREA
;
public
static
final
Rectangle
RIGHT_OF_GAME_AREA
=
Ex5JPanel
.
RIGHT_OF_GAME_AREA
;
private
int
moveX
;
private
Rectangle
area
;
private
boolean
isVisible
;
public
Blob
()
{
int
range
=
BLOB_SIZE
/
2
;
moveX
=
(
int
)
(
Math
.
random
()
*
range
+
4
);
if
(
Math
.
random
()
<
0.5
)
{
moveX
=
-
moveX
;
}
int
y
=
(
int
)
(
Math
.
random
()
*
(
GAME_AREA
.
height
-
BLOB_SIZE
)
+
GAME_AREA
.
y
);
area
=
new
Rectangle
(
CENTRE_POINT
.
x
-
BLOB_SIZE
/
2
,
y
,
BLOB_SIZE
,
BLOB_SIZE
);
isVisible
=
true
;
}
// ---------------------------------------------------------
// Complete these 3 methods as per Part 1 of the hand out
// ---------------------------------------------------------
public
boolean
getIsVisible
()
{
}
public
void
draw
(
Graphics
g
)
{
if
(
!
isVisible
)
{
return
;
}
}
public
void
move
()
{
}
}
Moving Blobs/Ex5App.java
Moving Blobs/Ex5App.java
/*
* Purpose: application for the
* moving blobs.
*
* Author: CompSci 101
*/
public
class
Ex5App
{
public
static
void
main
(
String
[]
args
)
{
Ex5JFrame
gui
=
new
Ex5JFrame
(
"Blobs"
,
3
,
3
,
Ex5JPanel
.
JFRAME_AREA_WIDTH
,
Ex5JPanel
.
JFRAME_AREA_HEIGHT
);
}
}
Moving Blobs/Ex5JFrame.java
Moving Blobs/Ex5JFrame.java
/*
* Purpose: frame for the
* moving blobs.
*
* Author: CompSci 101
*
*/
import
java
.
awt
.
*
;
import
java
.
awt
.
event
.
*
;
import
javax
.
swing
.
*
;
public
class
Ex5JFrame
extends
JFrame
{
public
Ex5JFrame
(
String
title
,
int
x
,
int
y
,
int
width
,
int
height
)
{
// Set the title, top left location, and close operation for the frame
setTitle
(
title
);
setLocation
(
x
,
y
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
// Create an instance of the JPanel class, and set this to define the
// content of the window
JPanel
frameContent
=
new
Ex5JPanel
();
Container
visibleArea
=
getContentPane
();
visibleArea
.
add
(
frameContent
);
// Set the size of the content pane of the window, resize and validate the
// window to suit, obtain keyboard focus, and then make the window visible
frameContent
.
setPreferredSize
(
new
Dimension
(
width
,
height
));
pack
();
frameContent
.
requestFocusInWindow
();
setVisible
(
true
);
}
}
Moving Blobs/Ex5JPanel.java
Moving Blobs/Ex5JPanel.java
/*
* Purpose: creates moving Blob objects.
*
* Author:
* Date-written: May 2013
*/
import
java
.
awt
.
*
;
import
java
.
awt
.
event
.
*
;
import
javax
.
swing
.
*
;
public
class
Ex5JPanel
extends
JPanel
implements
ActionListener
,
MouseListener
{
public
static
final
int
BLOB_SIZE
=
40
;
public
static
final
int
MAX_NUMBER_OF_BLOBS
=
30
;
public
static
final
int
GAP_LEFT_RIGHT
=
BLOB_SIZE
;
public
static
final
int
GAP_ABOVE
=
100
;
public
static
final
int
GAP_BELOW
=
100
;
public
static
final
int
GAME_AREA_HEIGHT
=
400
;
public
static
final
int
GAME_AREA_WIDTH
=
600
;
public
static
final
Rectangle
GAME_AREA
=
new
Rectangle
(
GAP_LEFT_RIGHT
,
GAP_ABOVE
,
GAME_AREA_WIDTH
,
GAME_AREA_HEIGHT
);
public
static
final
Point
CENTRE_POINT
=
new
Point
((
GAME_AREA
.
x
+
GAME_AREA
.
width
/
2
),
((
GAME_AREA
.
y
+
GAME_AREA
.
height
/
2
)));
public
static
final
int
JFRAME_AREA_WIDTH
=
GAME_AREA_WIDTH
+
2
*
GAP_LEFT_RIGHT
;
public
static
final
int
JFRAME_AREA_HEIGHT
=
GAME_AREA_HEIGHT
+
GAP_ABOVE
+
GAP_BELOW
;
public
static
final
int
LAUNCH_PAD_SIZE
=
BLOB_SIZE
*
2
;
public
static
final
Rectangle
LAUNCH_PAD_AREA
=
new
Rectangle
(
CENTRE_POINT
.
x
-
LAUNCH_PAD_SIZE
/
2
,
GAME_AREA
.
y
,
LAUNCH_PAD_SIZE
+
1
,
GAME_AREA
.
height
);
public
static
final
Color
BACKGROUND_COLOUR
=
Color
.
GREEN
;
public
static
final
Color
LAUNCH_PAD_COLOUR
=
Color
.
GREEN
;
public
static
final
Color
GAME_AREA_COLOUR
=
Color
.
BLUE
;
public
static
final
Rectangle
LEFT_OF_GAME_AREA
=
new
Rectangle
(
-
BLOB_SIZE
*
2
,
0
,
GAME_AREA
.
x
+
BLOB_SIZE
*
2
,
JFRAME_AREA_HEIGHT
);
public
static
final
Rectangle
RIGHT_OF_GAME_AREA
=
new
Rectangle
(
GAME_AREA
.
x
+
GAME_AREA
.
width
+
1
,
0
,
JFRAME_AREA_WIDTH
-
GAME_AREA
.
x
-
GAME_AREA_WIDTH
+
BLOB_SIZE
*
2
,
JFRAME_AREA_HEIGHT
);
private
boolean
noMoreBlobs
;
private
int
upTo
;
private
Blob
[]
blobs
;
public
Ex5JPanel
()
{
setBackground
(
BACKGROUND_COLOUR
);
addMouseListener
(
this
);
initialise
();
}
private
void
initialise
()
{
blobs
=
new
Blob
[
MAX_NUMBER_OF_BLOBS
];
noMoreBlobs
=
false
;
upTo
=
0
;
}
//--------------------------------------------------------
// Complete these 4 methods as Part 2 and 3 of the handout
//--------------------------------------------------------
public
void
keyPressed
(
KeyEvent
e
)
{
}
private
void
drawBlobs
(
Graphics
g
)
{
}
private
void
addABlob
()
{
}
private
void
moveBlobs
()
{
}
// Handle ActionEvents (Comes here each time the Timer ticks)
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
Math
.
random
()
<
0.08
)
{
addABlob
();
}
moveBlobs
();
if
(
moreBlobsToGo
()
==
false
)
{
noMoreBlobs
=
true
;
t
.
stop
();
}
repaint
();
}
// Checks whether there are any blobs either moving or about to start moving
private
boolean
moreBlobsToGo
()
{
if
(
noMoreBlobs
)
{
return
false
;
}
if
(
upTo
<
MAX_NUMBER_OF_BLOBS
)
{
return
true
;
}
for
(
int
i
=
0
;
i
<
upTo
;
i
++
)
{
if
(
blobs
[
i
].
getIsVisible
()
==
true
)
{
return
true
;
}
}
return
false
;
}
// Draw everything
public
void
paintComponent
(
Graphics
g
){
super
.
paintComponent
(
g
);
drawInformation
(
g
);
drawGameArea
(
g
);
drawBlobs
(
g
);
drawBorderAreas
(
g
);
drawLaunchArea
(
g
);
if
(
noMoreBlobs
)
{
drawEndMessage
(
g
);
}
}
// Drawing helper methods
private
void
drawGameArea
(
Graphics
g
)
{
g
.
setColor
(
GAME_AREA_COLOUR
);
g
.
fillRect
(
GAME_AREA
.
x
,
GAME_AREA
.
y
,
GAME_AREA
.
width
,
GAME_AREA
.
height
);
}
private
void
drawLaunchArea
(
Graphics
g
)
{
g
.
setColor
(
BACKGROUND_COLOUR
);
g
.
fillRect
(
LAUNCH_PAD_AREA
.
x
,
LAUNCH_PAD_AREA
.
y
,
LAUNCH_PAD_AREA
.
width
,
LAUNCH_PAD_AREA
.
height
);
}
private
void
drawInformation
(
Graphics
g
)
{
final
int
HUGE_FONT_SIZE
=
60
;
final
Font
HUGE_FONT
=
new
Font
(
"SansSerif"
,
Font
.
BOLD
,
HUGE_FONT_SIZE
);
final
Color
NICE_GRAY_COLOUR
=
new
Color
(
157
,
161
,
158
);
int
numberOfBlobsToGo
=
getNumberOfBlobsToGo
();
int
xPos
=
GAME_AREA
.
x
;
int
yPos
=
GAME_AREA
.
y
-
30
;
g
.
setColor
(
NICE_GRAY_COLOUR
);
g
.
setFont
(
HUGE_FONT
);
g
.
drawString
(
"BLOBS TO GO: "
+
numberOfBlobsToGo
,
xPos
,
yPos
);
}
private
int
getNumberOfBlobsToGo
()
{
int
countActive
=
0
;
for
(
int
i
=
0
;
i
<
upTo
;
i
++
)
{
if
(
blobs
[
i
].
getIsVisible
())
{
countActive
++
;
}
}
return
countActive
+
(
MAX_NUMBER_OF_BLOBS
-
upTo
);
}
private
void
drawEndMessage
(
Graphics
g
)
{
final
int
HUGE_FONT_SIZE
=
60
;
final
Font
HUGE_FONT
=
new
Font
(
"SansSerif"
,
Font
.
BOLD
,
HUGE_FONT_SIZE
);
final
Color
NICE_GRAY_COLOUR
=
new
Color
(
157
,
161
,
158
);
int
xPos
=
GAME_AREA
.
x
+
15
;
int
yPos
=
CENTRE_POINT
.
y
-
HUGE_FONT_SIZE
;
g
.
setColor
(
NICE_GRAY_COLOUR
);
g
.
setFont
(
HUGE_FONT
);
g
.
drawString
(
"NO MORE BLOBS"
,
xPos
,
yPos
);
}
private
void
drawBorderAreas
(
Graphics
g
)
{
g
.
setColor
(
BACKGROUND_COLOUR
);
g
.
fillRect
(
LEFT_OF_GAME_AREA
.
x
,
LEFT_OF_GAME_AREA
.
y
,
LEFT_OF_GAME_AREA
.
width
,
LEFT_OF_GAME_AREA
.
height
);
g
.
fillRect
(
RIGHT_OF_GAME_AREA
.
x
,
RIGHT_OF_GAME_AREA
.
y
,
RIGHT_OF_GAME_AREA
.
width
,
RIGHT_OF_GAME_AREA
.
height
);
}
// Handle MouseEvents - comes here when mouse is pressed
public
void
mousePressed
(
MouseEvent
e
)
{
initialise
();
repaint
();
}
public
void
mouseClicked
(
MouseEvent
e
)
{}
public
void
mouseReleased
(
MouseEvent
e
)
{}
public
void
mouseEntered
(
MouseEvent
e
)
{}
public
void
mouseExited
(
MouseEvent
e
)
{}
// Additional KeyEvent methods (required if KeyEvents being handled in program)
public
void
keyTyped
(
KeyEvent
e
)
{}
public
void
keyReleased
(
KeyEvent
e
)
{}
}