-
counter problems
hi, been trying to do a program a specific way, ive gave up on that way, cant do it
im sticking to the program i have, only problem is my "countMoves" counter, whenever i drag an image then drop it it doesnt add 1 to the counter, not until i drag another image and drop it, then the counter changes to 1 when it should really be 2, any help plz?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class coursework extends Applet implements MouseListener, MouseMotionListener {
private Point counter1, counter2, counter3, counter4, counter5, mouse;
private int select, countMoves=0;
private Image cabinet, bookcase, wardrobe, piano, chest;
public void init() {
this.addMouseMotionListener (this);
this.addMouseListener (this);
select = 0;
cabinet = getImage (getDocumentBase(), "cabinet.jpg") ;
bookcase = getImage (getDocumentBase(), "bookcase.jpg");
wardrobe = getImage (getDocumentBase(), "wardrobe.jpg");
piano = getImage (getDocumentBase(), "piano.jpg") ;
chest = getImage (getDocumentBase(), "chest.jpg");
counter1 = new Point(17, 24);
counter2 = new Point(155, 130);
counter3 = new Point(85, 130);
counter4 = new Point(155, 24);
counter5 = new Point(17, 130);
mouse = new Point();
}
public void paint (Graphics g) {
drawBox (g);
g.drawImage (cabinet, counter1.x, counter1.y, 60, 60, this);
g.drawImage (bookcase, counter2.x, counter2.y, 60, 60, this);
g.drawImage (wardrobe, counter3.x, counter3.y, 60, 60, this);
g.drawImage (piano, counter4.x, counter4.y, 60, 60, this);
g.drawImage (chest, counter5.x, counter5.y, 60, 60, this);
g.drawString ("1", 42, 113);
g.drawString ("2", 113, 113);
g.drawString ("3", 180, 113);
g.drawString ("4", 42, 216);
g.drawString ("5", 113, 216);
g.drawString ("6", 180, 216);
g.drawString ("number of moves are " +countMoves, 300, 300);
}
public void mouseDragged (MouseEvent e) {
mouse = e.getPoint();
//continuously change the coordinates of the selected counter
if (select ==1) counter1 = mouse;
if (select ==2) counter2 = mouse;
if (select ==3) counter3 = mouse;
if (select ==4) counter4 = mouse;
if (select ==5) counter5 = mouse;
repaint();
}
public void mouseMoved (MouseEvent e) {} //required for the interface
public void mousePressed (MouseEvent e) { //select a counter using the mouse
mouse = e.getPoint();
if (mouse.x > counter1.x - 60 && mouse.x < counter1.x + 60 && mouse.y >counter1.y - 60 && mouse.y < counter1.y +60) select = 1;
if (mouse.x > counter2.x - 60 && mouse.x < counter2.x + 60 && mouse.y >counter2.y - 60 && mouse.y < counter2.y +60) select = 2;
if (mouse.x > counter3.x - 60 && mouse.x < counter3.x + 60 && mouse.y >counter3.y - 60 && mouse.y < counter3.y +60) select = 3;
if (mouse.x > counter4.x - 60 && mouse.x < counter4.x + 60 && mouse.y >counter4.y - 60 && mouse.y < counter4.y +60) select = 4;
if (mouse.x > counter5.x - 60 && mouse.x < counter5.x + 60 && mouse.y >counter5.y - 60 && mouse.y < counter5.y +60) select = 5;
}//required for the interface
public void mouseClicked (MouseEvent e){}
public void mouseReleased (MouseEvent e) {countMoves++;}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e){}
public void drawBox (Graphics g) {
for (int i = 10; i <= 220; i += 105) {
g.drawLine (10, i, 220, i);
}
for (int l = 10; l <= 220; l += 70) {
g.drawLine (l, 10, l, 220);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks