hi, i have taken pieces from another program and put them in my program (very very badly), i cant figure this out at all, can anybody help me, PLEASE!! <br> my program allows users to move images from one square to another, im trying to constrict the images to one square at a time, i cant do it, any help please???<br> <br>
<java> import java.applet.*;<br> import java.awt.*;<br> import java.awt.event.*;<br> import java.awt.image.*;<br> <br> public class program extends Applet implements MouseListener, MouseMotionListener {<br> private Point counter1, counter2, counter3, counter4, counter5, mouse;<br> private int select;<br> private Image cabinet, bookcase, wardrobe, piano, chest;<br> <br>
public void mouseDragged (MouseEvent e) {<br> mouse = e.getPoint(); //continuously change the coordinates of the selected counter<br> if (select ==1) counter1 = mouse;<br> if (select ==2) counter2 = mouse;<br> if (select ==3) counter3 = mouse;<br> if (select ==4) counter4 = mouse;<br> if (select ==5) counter5 = mouse;<br> repaint();<br> }<br> <br> public void mouseMoved (MouseEvent e) {} //required for the interface<br> public void mousePressed (MouseEvent e) { //select a counter using the mouse<br> mouse = e.getPoint();<br> if (mouse.x > counter1.x - 60 && mouse.x < counter1.x +60 && mouse.y >counter1.y - 60 && mouse.y < counter1.y +60) select = 1;<br> if (mouse.x > counter2.x - 60 && mouse.x < counter2.x +60 && mouse.y >counter2.y - 60 && mouse.y < counter2.y +60) select = 2;<br> if (mouse.x > counter3.x - 60 && mouse.x < counter3.x +60 && mouse.y >counter3.y - 60 && mouse.y < counter3.y +60) select = 3;<br> if (mouse.x > counter4.x - 60 && mouse.x < counter4.x +60 && mouse.y >counter4.y - 60 && mouse.y < counter4.y +60) select = 4;<br> if (mouse.x > counter5.x - 60 && mouse.x < counter5.x +60 && mouse.y >counter5.y - 60 && mouse.y < counter5.y +60) select = 5;<br> }//required for the interface
public void mouseClicked (MouseEvent e){}<br> public void mouseReleased (MouseEvent e) {}<br> public void mouseEntered (MouseEvent e) {}<br> public void mouseExited (MouseEvent e){}<br> public void drawBox (Graphics g) {<br> for (int i = 10; i <= 220; i += 105) {<br> g.drawLine (10, i, 220, i);<br> }<br> <br> for (int l = 10; l <= 220; l += 70) {<br> g.drawLine (l, 10, l, 220);<br>} }<br> } </java>
Throw it away and start again. If you can't figure out what it does, then chances are neither is anyone else.
1) Figure out what you want your code to do. 2) Design it using named methods (preferably with JavaDoc). That will help you figure out what each part is doing. 3) Consider using named constants for fixed values. Numbers really don't mean anything.
Lastly, no-one is going to be of much help if you've just randomly copied and pasted code, particularly if it doesn't work. You've got two 'paint' methods, and in one of them you refer to a non-existant off-screen graphic object.