Cant get my balls to move
I am trying to write an applet based on one of the moscow puzzles, basically i have to make a chute long in which will be housed 4 white balls and 4 black, the object of the puzzle is to manouver the black balls passed the whites and out of the chute there is a recess in one side of the chute which functions as a passing place.
I have adapted the code i had used in an earlier project and managed to get all the components on screen but nowwhen i click on a ball and try to move it, it won't move...however it looks like the applet is trying to move the chute and everything else in it.
here is what i have
Thanks in advance for any help you guys can give me
:)
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class RollThemOut extends Applet implements MouseListener, MouseMotionListener
{
private Point blkball1, blkball2, blkball3, blkball4,whtball1, whtball2, whtball3, whtball4, mouse;
private int select;
public void init()
{
this.addMouseMotionListener(this);
this.addMouseListener(this);
select=0;
blkball1 = new Point(15,110);
blkball2 = new Point(60,110);
blkball3 = new Point(105,110);
blkball4 = new Point(150,110);
whtball1 = new Point(195,106);
whtball2 = new Point(250,106);
whtball3 = new Point(305,106);
whtball4 = new Point(360,106);
mouse = new Point();
}
public void paint(Graphics g)
{
drawBox(g);
g.setColor(Color.black);
g.fillOval(blkball1.x, blkball1.y, 45, 45);
g.fillOval(blkball2.x, blkball2.y, 45, 45);
g.fillOval(blkball3.x, blkball3.y, 45, 45);
g.fillOval(blkball4.x, blkball4.y, 45, 45);
g.drawOval(whtball1.x, whtball1.y, 55, 55);
g.drawOval(whtball2.x, whtball2.y, 55, 55);
g.drawOval(whtball3.x, whtball3.y, 55, 55);
g.drawOval(whtball4.x, whtball4.y, 55, 55);
}
public void mouseDragged(MouseEvent e)
{
if (select==1) blkball1 = mouse;
if (select==2) blkball2 = mouse;
if (select==3) blkball3 = mouse;
if (select==4) blkball4 = mouse;
if (select==5) whtball1 = mouse;
if (select==6) whtball2 = mouse;
if (select==7) whtball3 = mouse;
if (select==8) whtball4 = mouse;
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
mouse = e.getPoint();
if (mouse.x > blkball1.x - 10 && mouse.x < blkball1.x + 10 &&
mouse.y > blkball1.y - 10 && mouse.y < blkball1.y + 10) select = 1;
if (mouse.x > blkball2.x - 10 && mouse.x < blkball2.x + 10 &&
mouse.y > blkball2.y - 10 && mouse.y < blkball2.y + 10) select = 2;
if (mouse.x > blkball3.x - 10 && mouse.x < blkball3.x + 10 &&
mouse.y > blkball3.y - 10 && mouse.y < blkball3.y + 10) select = 3;
if (mouse.x > blkball4.x - 10 && mouse.x < blkball4.x + 10 &&
mouse.y > blkball4.y - 10 && mouse.y < blkball4.y + 10) select = 4;
if (mouse.x > whtball1.x - 10 && mouse.x < whtball1.x + 10 &&
mouse.y > whtball1.y - 10 && mouse.y < whtball1.y + 10) select = 5;
if (mouse.x > whtball2.x - 10 && mouse.x < whtball2.x + 10 &&
mouse.y > whtball2.y - 10 && mouse.y < whtball2.y + 10) select = 6;
if (mouse.x > whtball3.x - 10 && mouse.x < whtball3.x + 10 &&
mouse.y > whtball3.y - 10 && mouse.y < whtball3.y + 10) select = 7;
if (mouse.x > whtball4.x - 10 && mouse.x < whtball4.x + 10 &&
mouse.y > whtball4.y - 10 && mouse.y < whtball4.y + 10) select = 8;
}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void drawBox(Graphics g)
{
g.setColor(Color.black);
g.fillRect(10,100,685 ,68);
g.fillOval(410, 55, 65, 65);
g.fillRect(410, 85, 65, 25);
g.fillRect(690, 107, 20, 55);
g.setColor(Color.white);
g.fillRect(15,105,675,58);
g.fillOval(415, 60, 55, 55);
g.fillRect(415, 90, 55, 20);
g.fillRect(690, 112, 30, 45);
}
}
They are moving now, but where, and why ?
I have set up the applet as a panel here, I think you should consider
using a memory image to avoid flickering. You will have to add some more
code here, - vertical move of a ball should only be allowed where there is
room for it, - I think.
Code:
import java.awt.event.*;
import java.awt.*;
import java.util.*;
/**
* A ball w. a diameter, a color and a location
*/
class Ball {
private Rectangle rect=null;
private Color color=null;
public Ball (int x, int y, int diameter, Color color) {
rect=new Rectangle(x,y,diameter, diameter);
this.color=color;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(color);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
g.setColor(c);
}
public boolean isHit(Point p) {
return rect.contains(p);
}
public Rectangle getRectangle() {
return rect;
}
public void moveBall(Point p) {
rect.x += p.x;
/**
* include next statement to allow vertical dragging, possibly
* only allowed under the arc...some positional test perhaps ?
*/
//rect.y += p.y;
}
}
/**
* Your applet as a panel
*/
class RollThemOut extends Panel
implements MouseListener, MouseMotionListener, Comparator {
public static ArrayList bList=new ArrayList();
public static Ball [] balls = {
new Ball(15, 110, 45, Color.black),
new Ball(60, 110, 45, Color.black),
new Ball(105, 110, 45, Color.black),
new Ball(150, 110, 45, Color.black),
new Ball(195, 106, 55, Color.white),
new Ball(250, 106, 55, Color.white),
new Ball(305, 106, 55, Color.white),
new Ball(360, 106, 55, Color.white)
};
private Point lastPos; // last dragging position
private Point moveOffset=new Point(); // dragging distance
private int selectedIndex=-1; // index of currently selected ball
public void init() {
this.addMouseMotionListener(this);
this.addMouseListener(this);
selectedIndex = -1;
}
public void paint(Graphics g) {
update (g);
}
public void update(Graphics g) {
Color c = g.getColor();
g.setColor(Color.blue);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
drawBox(g);
drawBalls(g);
g.setColor(c);
}
private void drawBalls(Graphics g) {
for (int i = 0; i < balls.length; i++) {
balls[i].draw(g);
}
}
public void drawBox(Graphics g) {
g.setColor(Color.black);
g.fillRect(10, 100, 685, 68);
g.fillOval(410, 55, 65, 65);
g.fillRect(410, 85, 65, 25);
g.fillRect(690, 107, 20, 55);
g.setColor(Color.darkGray);
g.fillRect(15, 105, 675, 58);
g.fillOval(415, 60, 55, 55);
g.fillRect(415, 90, 55, 20);
g.fillRect(690, 112, 30, 45);
}
public void mouseDragged(MouseEvent e) {
if (selectedIndex == -1) {
return;
}
moveOffset.x = e.getPoint().x - lastPos.x;
moveOffset.y = e.getPoint().y - lastPos.y;
balls[selectedIndex].moveBall(moveOffset);
repaint();
lastPos=e.getPoint();
}
/**
* I dont know all the rules here but I've added a sort of the
* balls array to simplify checking of move-space
* @param e
*/
public void mouseReleased(MouseEvent e) {
if (selectedIndex >= 0) { // dragging may have occurred
sortBalls();
}
selectedIndex = -1;
}
/**
* Sort the ball array on x-position
*/
private void sortBalls() {
bList.clear();
for (int i = 0; i < balls.length; i++) {
bList.add(balls[i]);
}
Collections.sort(bList, this);
for (int i = 0; i < bList.size(); i++) {
balls[i] = (Ball) bList.get(i);
}
}
public int compare(Object o1, Object o2) {
Ball b1 = (Ball) o1;
Ball b2 = (Ball) o2;
return b1.getRectangle().x - b2.getRectangle().x;
}
public boolean equals(Object obj) {
return false;
}
/**
* Check for a hit
* @param e
*/
public void mousePressed(MouseEvent e) {
lastPos = e.getPoint();
selectedIndex = -1;
for (int i=0; i<balls.length; i++) {
if (balls[i].isHit(lastPos)) {
selectedIndex = i;
break;
}
}
System.out.println("Select:"+selectedIndex+"\r\n");
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public class RollDriver {
/**
* ************* MAIN ***************************
* Sets up a jframe and a textarea for running the applet code
* @param args
*/
public static void main (String [] args) {
Frame f=new Frame();
f.setLayout(new GridLayout());
RollThemOut p=new RollThemOut();
p.init();
f.add(p);
f.setSize(780, 600);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.validate();
f.setVisible(true);
}
}