|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-18-2008, 04:53 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 7
|
|
|
Ping pong game
I have already got
if(sliderx_pos == x_pos && y_pos ==148)
{
x_speed *= -1;
y_speed *= -1;
}
The problem is that if the ball lands an any part the the slider it needs to bounce. At the moment its only bouncing off the far left hand side which represents the sliderx_pos. So i need away to say if the ball hits any part of the slider then bounce.
The slider width is 60 therefore i need away to say
if ball position is between 145 amd 205 then bounce.
Hope this makes sense.
Last edited by adam405 : 03-18-2008 at 05:22 PM.
|
|

03-18-2008, 05:55 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,104
|
|
Declare "box" as a member variable and use setLocation to move/position it in the paint method.
int appletsize_x = 300;
int appletsize_y = 300;
Rectangle box = new Rectangle(60, 25);
Graphics2D g2 = (Graphics2D) g;
box.setLocation(x_pos,y_pos);
g2.draw(box);
Then when it is time for collision–checking in your ball Runnable you can get access to the box as an obstacle with
int minX = box.x;
int maxX = box.x + box.width;
int maxY = box.y + box.height;
|
|

03-18-2008, 09:36 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 7
|
|
this is what i have, any ideas
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.Graphics;
public class FullBall extends Applet implements Runnable {
//position of the ball
int x_pos = 12;
int y_pos = 12;
//position of the ball
int sliderx_pos = 1;
int slidery_pos = 150;
//ball size
int radius = 5;
//size of slider
int swidth = 60;
int sheight = 1;
int realwidth = sliderx_pos + swidth;
//balldirection
int x_speed = 6;
int y_speed = 8;
//slider direcion
int sliderx_speed = 0;
//applet size
int appletsize_top = 10;
int appletsize_bottom = 160;
int appletsize_left = 10;
int appletsize_right = 340;
//random movement
Random rnd = new Random ();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
// start - method is called after init
// and every time the applet is maximized
public void start ()
{
// define a new thread
running = true;
thread = new Thread (this);
// start this thread
thread.start ();
System.out.println("start");
}
// A well–behaved Applet stops
// its animation when minimized.
public void stop()
{
running = false;
if(thread != null)
thread.interrupt();
thread = null;
System.out.println("stop");
}
//run- start the applet game
// Implementation of the Runnable interface.
public void run ()
{
while (running)
{
// lower ThreadPriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// change direction if ball hits any side
if (x_pos < appletsize_left || x_pos > appletsize_right )
{
x_speed *= -1;
}
if (y_pos < appletsize_top)
{
y_speed *= -1;
}
if(y_pos > appletsize_bottom )
{
y_speed *= -1;
}
//change direction of slider
if (sliderx_pos > appletsize_right - swidth)
{
sliderx_speed = appletsize_right - swidth;
}
else if (sliderx_pos < appletsize_left)
{
sliderx_speed = appletsize_left;
}
//logic for bouncing off slider
else if(x_pos >= sliderx_pos && x_pos <=realwidth && y_pos ==145)
{
x_speed *= -1;
y_speed *= -1;
}
sliderx_pos = sliderx_speed;
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint();
try
{
// Stop thread for the speed
Thread.sleep (50);
}
catch (InterruptedException ex)
{
running = false;
break;
}
// set ThreadPriority to maximum value
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
// set color
g.setColor (Color.red);
// paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle(sliderx_pos,slidery_pos,swidth,sheight);
g2.setColor (Color.blue);
g2.draw(box);
}
//slider key event
public boolean keyDown (Event e, int key)
{
if (key == Event.LEFT)
{
sliderx_speed = sliderx_pos -20 ;
}
else if (key == Event.RIGHT)
{
sliderx_speed = sliderx_pos +20;
}
sliderx_pos = sliderx_speed;
return true;
}
}
|
|

03-19-2008, 01:52 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,104
|
|
// <applet code="FullBallRx" width="400" height="400"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FullBallRx extends Applet implements Runnable {
//position of the ball
int x_pos = 12;
int y_pos = 12;
//position of the ball ** slider **
int sliderx_pos = 1;
int slidery_pos = 150;
//ball size
int radius = 5;
//size of slider
int swidth = 60;
int sheight = 1;
Rectangle box = new Rectangle(swidth, sheight);
// int realwidth = sliderx_pos + swidth;
//balldirection
int x_speed = 6;
int y_speed = 8;
//slider direcion
int sliderx_speed = 0;
//applet size
int appletsize_top = 10;
int appletsize_bottom = 160;
int appletsize_left = 10;
int appletsize_right = 340;
//random movement
Random rnd = new Random ();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
public void init()
{
enableEvents(AWTEvent.KEY_EVENT_MASK);
setFocusable(true);
}
public void start ()
{
// define a new thread
running = true;
thread = new Thread (this);
// start this thread
thread.start ();
System.out.println("start");
}
public void stop()
{
running = false;
if(thread != null)
thread.interrupt();
thread = null;
System.out.println("stop");
}
public void run ()
{
while (running)
{
// lower ThreadPriority
// Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// change direction if ball will hit any side on the
// next advance/stop. The ball position is at its center.
if (x_pos - radius + x_speed < appletsize_left ||
x_pos + radius + x_speed > appletsize_right )
{
x_speed *= -1;
}
if (y_pos - radius + y_speed < appletsize_top)
{
y_speed *= -1;
}
if(y_pos + radius + y_speed > appletsize_bottom )
{
y_speed *= -1;
}
//change direction of slider
// Moved to processKeyEvent method.
//logic for bouncing off slider
// We know where the ball and slider are so we
// should be able to calculate a possible collision.
// Look ahead to the next position of the ball for
// the possible collision.
// Assumption: consider collisions only on the top
// of the slider.
if(x_pos + radius + x_speed > sliderx_pos &&
x_pos - radius + x_speed < sliderx_pos + swidth) {
// Ball is in vertical plane of slider.
// Check for possible vertical contact.
if(y_pos + radius + y_speed > slidery_pos) {
// We have an imminent collision -> bounce!
y_speed *= -1;
}
}
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint();
try
{
// Stop thread for the speed
Thread.sleep (50);
}
catch (InterruptedException ex)
{
running = false;
break;
}
// set ThreadPriority to maximum value
// Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// set color
g.setColor (Color.red);
// paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
box.setLocation(sliderx_pos, slidery_pos);
g2.setColor (Color.blue);
g2.draw(box);
}
protected void processKeyEvent(KeyEvent e)
{
int keyCode = e.getKeyCode();
// linke Cursortaste
if (keyCode == KeyEvent.VK_LEFT)
{
// Ball bewegt sich dann nach links
if (sliderx_pos -10 > appletsize_left)
sliderx_pos = sliderx_pos -10;
}
// rechte Cursortaste
else if (keyCode == KeyEvent.VK_RIGHT)
{
if (sliderx_pos + swidth +10 < appletsize_right)
sliderx_pos = sliderx_pos +10;
}
}
}
|
|

03-19-2008, 03:04 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 7
|
|
thanks, works well. I am trying to add in a brick to hit. I am again struggling with the logic. The problem is that the brick sometime disappears even though the ball i no where near brick. This happens if i just let the game run with no keyboard interaction at the start
// <applet code="FullBallRx" width="400" height="400"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FullBall extends Applet implements Runnable {
//position of the ball
int x_pos = 12;
int y_pos = 12;
//position of the ball ** slider **
int sliderx_pos = 50;
int slidery_pos = 150;
//ball size
int radius = 5;
//size of slider
int swidth = 60;
int sheight = 1;
Rectangle box = new Rectangle(swidth, sheight);
//balldirection
int x_speed = 6;
int y_speed = 8;
//slider direcion
int sliderx_speed = 0;
//applet size
int appletsize_top = getHeight();
int appletsize_bottom = appletsize_top + 20 * 10;
int appletsize_left = getWidth();
int appletsize_right = appletsize_left + 35 * 10;
//random movement
Random rnd = new Random ();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
//level stuff
int lx_pos = 100;
int ly_pos = appletsize_top +20;
int lwidth = 50;
int lheight =1;
Rectangle brick = new Rectangle(lwidth, lheight);
public void init()
{
enableEvents(AWTEvent.KEY_EVENT_MASK);
setFocusable(true);
}
public void start ()
{
// define a new thread
running = true;
thread = new Thread (this);
// start this thread
thread.start ();
System.out.println("start");
}
public void stop()
{
running = false;
if(thread != null)
thread.interrupt();
thread = null;
System.out.println("stop");
}
public void run ()
{
while (running)
{
//change direct if the ball hits the left or right wall
// stop ball if out
if (x_pos - radius + x_speed < appletsize_left ||
x_pos + radius + x_speed > appletsize_right )
{
x_speed *= -1;
}
if (y_pos - radius + y_speed < appletsize_top)
{
y_speed *= -1;
}
if(y_pos + radius + y_speed > appletsize_bottom )
{
stop();
}
//what to do if we hit the slider
if(x_pos + radius > sliderx_pos &&
x_pos - radius < sliderx_pos + swidth) {
// Ball is in vertical plane of slider.
// Check for possible horizontal contact.
if(y_pos + radius + y_speed > slidery_pos) {
// We have an imminent collision -> bounce!
y_speed *= -1;
}
}
//what to do if we hit a brick
else if(x_pos - radius > lx_pos &&
x_pos + radius < lx_pos + lwidth) {
// Ball is in vertical plane of slider.
// Check for possible horizontal contact.
if(y_pos - radius - y_speed > ly_pos) {
// We have an imminent collision -> bounce!
y_speed *= -1;
lx_pos = 10000;
ly_pos = 10000;
}
}
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint();
try
{
// Stop thread for the speed
Thread.sleep (90);
}
catch (InterruptedException ex)
{
running = false;
break;
}
}
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
//DRAWING THE BALL
Graphics2D g2 = (Graphics2D) g;
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
//DRAWING THE SLIDER
box.setLocation(sliderx_pos, slidery_pos);
g2.setColor (Color.blue);
g2.draw(box);
//DRAWING THE BRICKS
brick.setLocation(lx_pos, ly_pos);
g2.setColor (Color.pink);
g2.draw(brick);
}
protected void processKeyEvent(KeyEvent e)
{
int keyCode = e.getKeyCode();
//move if left key is pressed
if (keyCode == KeyEvent.VK_LEFT)
{
if (sliderx_pos -10 > appletsize_left)
sliderx_pos = sliderx_pos -20;
}
//move if right key is pressed
else if (keyCode == KeyEvent.VK_RIGHT)
{
if (sliderx_pos + swidth +10 < appletsize_right)
sliderx_pos = sliderx_pos +20;
}
}
}
Last edited by adam405 : 03-19-2008 at 03:39 PM.
|
|

03-19-2008, 08:06 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,104
|
|
// <applet code="FullBall2" width="400" height="400"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FullBall2 extends Applet implements Runnable
{
//position of the ball
int x_pos = 12;
int y_pos = 12;
//ball size
int radius = 5;
//ball direction
int x_speed = 6;
int y_speed = 8;
//position of the slider
int sliderx_pos = 50;
int slidery_pos = 150;
//size of slider
int swidth = 60;
int sheight = 1;
Rectangle box = new Rectangle(swidth, sheight);
//slider direcion
int sliderx_speed = 0;
//applet size
// These spatial definitions are not correct:
// Component origin (0,0) is at upper left.
// getHeight = bottom of component.
// getHeight returns zero at this point in
// program execution.
int appletsize_top = //getHeight();
0;
// bottom would be 200 px below bottom of component.
int appletsize_bottom = //appletsize_top + 20 * 10;
200;
// getWidth = width/far right of component.
// Since the applet has not been realized, ie, made
// visible, this call to getWidth returns zero.
int appletsize_left = //getWidth();
0;
// right would be 350 past right edge of component.
int appletsize_right = //appletsize_left + 35 * 10;
350;
//level stuff
int lx_pos = 100;
int ly_pos = appletsize_top +20;
int lwidth = 50;
int lheight = 1;
// If you move this only once you can specify the location here.
Rectangle brick = new Rectangle(lx_pos, ly_pos, lwidth, lheight);
//random movement
Random rnd = new Random ();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
public void init()
{
enableEvents(AWTEvent.KEY_EVENT_MASK);
setFocusable(true);
}
public void start ()
{
// define a new thread
running = true;
thread = new Thread (this);
// start this thread
thread.start();
}
public void stop()
{
running = false;
if(thread != null)
thread.interrupt();
thread = null;
}
public void run ()
{
while (running)
{
//change direct if the ball hits the left or right wall
// stop ball if out
if (x_pos - radius + x_speed < appletsize_left ||
x_pos + radius + x_speed > appletsize_right )
{
x_speed *= -1;
}
if (y_pos - radius + y_speed < appletsize_top)
{
y_speed *= -1;
}
if(y_pos + radius + y_speed > appletsize_bottom )
{
// Reposition ball to avoid overshooting boundry
// as a consequence of high speed value.
y_pos = appletsize_bottom - radius;
y_speed *= -1;
// stop();
}
// What to do if we will hit the top of the slider on the
// next frame, ie, after the next trip through this method.
if(x_pos + radius + x_speed > sliderx_pos &&
x_pos - radius + x_speed < sliderx_pos + swidth)
{
// Ball is in vertical plane of slider.
// Check for possible contact.
if(y_pos + radius + y_speed > slidery_pos)
{
// We have an imminent collision -> bounce!
y_speed *= -1;
}
}
// What to do if we will hit the bottom of the
// brick the next time through.
if(x_pos + radius + x_speed > lx_pos &&
x_pos - radius + x_speed < lx_pos + lwidth)
{
// Ball is in vertical plane of brick.
// Check for contact on underside of brick.
if(y_pos - radius /*+ y_speed*/ < ly_pos + brick.height)
{
// We have an imminent collision -> bounce!
y_speed *= -1;
// The ball disappeared because some of the
// signs above were reversed which caused a
// false/erroneous positive for collision.
System.out.println("brick being removed");
lx_pos = 10000;
ly_pos = 10000;
brick.setLocation(lx_pos, lx_pos);
}
}
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint();
try
{
// Stop thread for the speed
Thread.sleep (90);
}
catch (InterruptedException ex)
{
running = false;
break;
}
}
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
// Show right and lower boundries to help in
// adjusting animation variables.
g.drawLine(appletsize_right, appletsize_top,
appletsize_right, appletsize_bottom);
g.drawLine(appletsize_left, appletsize_bottom,
appletsize_right, appletsize_bottom);
//DRAWING THE BALL
Graphics2D g2 = (Graphics2D) g;
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
//DRAWING THE SLIDER
box.setLocation(sliderx_pos, slidery_pos);
g2.setColor (Color.blue);
g2.draw(box);
//DRAWING THE BRICKS
g2.setColor (Color.pink);
g2.draw(brick);
}
protected void processKeyEvent(KeyEvent e)
{
int keyCode = e.getKeyCode();
//move if left key is pressed
if (keyCode == KeyEvent.VK_LEFT)
{
if (sliderx_pos -10 > appletsize_left)
sliderx_pos = sliderx_pos -20;
}
//move if right key is pressed
else if (keyCode == KeyEvent.VK_RIGHT)
{
if (sliderx_pos + swidth +10 < appletsize_right)
sliderx_pos = sliderx_pos +20;
}
}
}
|
|

03-23-2008, 10:52 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 7
|
|
|
thanks so far with all the help, i am now trying to get the thread to pause if all the bricks have been hit and then set a random new positions for the brick and repaint the thread and then carry on the game. I have ben trying to solve this for days now, any help would be greatfully recieved.
Thanks adam
|
|

03-24-2008, 05:29 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,104
|
|
// <applet code="FullBall3" width="400" height="400"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FullBall3 extends Applet implements Runnable
{
//position of the ball
int x_pos = 12;
int y_pos = 12;
//ball size
int radius = 5;
//ball direction
int x_speed = 6;
int y_speed = 8;
//position of the slider
int sliderx_pos = 50;
int slidery_pos = 150;
//size of slider
int swidth = 60;
int sheight = 1;
Rectangle box = new Rectangle(swidth, sheight);
//slider direcion
int sliderx_speed = 0;
int appletsize_top = 0;
int appletsize_bottom = 200;
int appletsize_left = 0;
int appletsize_right = 350;
//level stuff
int lx_pos = 100;
int ly_pos = appletsize_top +20;
int lwidth = 50;
int lheight = 1;
Rectangle brick = new Rectangle(lx_pos, ly_pos, lwidth, lheight);
//random movement
Random rnd = new Random();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
boolean allBricksRemoved = false;
public void init()
{
enableEvents(AWTEvent.KEY_EVENT_MASK);
setFocusable(true);
}
public void start ()
{
running = true;
thread = new Thread (this);
thread.start();
}
public void stop()
{
running = false;
if(thread != null)
thread.interrupt();
thread = null;
}
public void run ()
{
while (running)
{
//change direct if the ball is about to hit a wall
if (x_pos - radius + x_speed < appletsize_left ||
x_pos + radius + x_speed > appletsize_right )
{
x_speed *= -1;
}
if (y_pos - radius + y_speed < appletsize_top)
{
y_speed *= -1;
}
if(y_pos + radius + y_speed > appletsize_bottom )
{
// Reposition ball to avoid overshooting boundry
// as a consequence of high speed value.
y_pos = appletsize_bottom - radius;
y_speed *= -1;
}
// What to do if we will hit the top of the slider on the
// next frame, ie, after the next trip through this method.
if(x_pos + radius + x_speed > sliderx_pos &&
x_pos - radius + x_speed < sliderx_pos + swidth)
{
// Ball is in vertical plane of slider.
// Check for possible contact.
if(y_pos + radius + y_speed > slidery_pos)
{
y_speed *= -1;
}
}
// What to do if we will hit the bottom of the
// brick the next time through.
if(x_pos + radius + x_speed > lx_pos &&
x_pos - radius + x_speed < lx_pos + lwidth)
{
// Ball is in vertical plane of brick.
// Check for contact on underside of brick.
if(y_pos - radius /*+ y_speed*/ < ly_pos + brick.height)
{
y_speed *= -1;
System.out.println("brick being removed");
lx_pos = 10000;
ly_pos = 10000;
brick.setLocation(lx_pos, ly_pos);
// When the last brick is gone:
allBricksRemoved = true;
}
}
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint();
if(allBricksRemoved)
{
resetBricks();
}
try
{
Thread.sleep (90);
}
catch (InterruptedException ex)
{
running = false;
break;
}
}
}
private void resetBricks()
{
stop();
new Thread(new Runnable()
{
public void run()
{
allBricksRemoved = false;
// Reset the brick(s) before we start up again.
int w = appletsize_right - appletsize_left - brick.width;
lx_pos = rnd.nextInt(w);
ly_pos = appletsize_top +20;
brick.setLocation(lx_pos, ly_pos);
repaint();
System.out.printf("brick location = [%d, %d]%n",
lx_pos, ly_pos);
start();
}
}).start();
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
// Show right and lower boundries to help in
// adjusting animation variables.
g.drawLine(appletsize_right, appletsize_top,
appletsize_right, appletsize_bottom);
g.drawLine(appletsize_left, appletsize_bottom,
appletsize_right, appletsize_bottom);
//DRAWING THE BALL
Graphics2D g2 = (Graphics2D) g;
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
//DRAWING THE SLIDER
box.setLocation(sliderx_pos, slidery_pos);
g2.setColor (Color.blue);
g2.draw(box);
//DRAWING THE BRICKS
g2.setColor (Color.pink);
g2.draw(brick);
}
protected void processKeyEvent(KeyEvent e)
{
int keyCode = e.getKeyCode();
//move if left key is pressed
if (keyCode == KeyEvent.VK_LEFT)
{
if (sliderx_pos -10 > appletsize_left)
sliderx_pos = sliderx_pos -20;
}
//move if right key is pressed
else if (keyCode == KeyEvent.VK_RIGHT)
{
if (sliderx_pos + swidth +10 < appletsize_right)
sliderx_pos = sliderx_pos +20;
}
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|