I'm trying to code where an image can be dragged using the mouseDragged method, but I can't seem to successfully do it with my current code. My aim is to ensure that when I place drag on every image in the applet, it follows the cursor.
/* Using drag mouseEvent, all objects in the app can be dragged...
*/
package finals_two;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* 10/16/2015
* @author michael.pabilona
*/
public class Finals_Two extends Applet implements MouseMotionListener
{
int hei, wid, xCoordinate, yCoordinate;
Image leftReceiver1, leftReceiver2, leftReceiver3;
Image rightReceiver1, rightReceiver2, rightReceiver3;
Image leftSpiker, rightSpiker, center, setter;
public void init()
{
hei = getSize().height;
wid = getSize().width;
xCoordinate = hei;
yCoordinate = wid;
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e)
{
xCoordinate = e.getX();
yCoordinate = e.getY();
repaint();
}
public void paint(Graphics gr)
{
setBackground(Color.white);
gr.setColor(Color.black);
//court bound top line
gr.drawLine(400, 130, 1200, 130);
//court bound bottom line
gr.drawLine(200, 530, 1000, 530);
//court bound left line
gr.drawLine(400, 130, 200, 530);
//court bound right line
gr.drawLine(1000, 530, 1200, 130);
//net
gr.drawLine(800, 130, 600, 530);
gr.drawLine(800, 130, 800, 0);
gr.drawLine(600, 530, 600, 400);
//net horizontal lines
gr.drawLine(800, 0, 600, 400);
gr.drawLine(800, 10, 600, 410);
gr.drawLine(800, 20, 600, 420);
gr.drawLine(800, 30, 600, 430);
gr.drawLine(800, 40, 600, 440);
gr.drawLine(800, 50, 600, 450);
gr.drawLine(800, 60, 600, 460);
gr.drawLine(800, 70, 600, 470);
gr.drawLine(800, 80, 600, 480);
//net vertical lines
gr.drawLine(795, 5, 795, 90);
gr.drawLine(790, 20, 790, 100);
gr.drawLine(785, 30, 785, 110);
gr.drawLine(780, 40, 780, 120);
gr.drawLine(775, 50, 775, 130);
gr.drawLine(770, 60, 770, 140);
gr.drawLine(765, 70, 765, 150);
gr.drawLine(760, 80, 760, 160);
gr.drawLine(755, 90, 755, 170);
gr.drawLine(750, 100, 750, 180);
gr.drawLine(745, 110, 745, 190);
gr.drawLine(740, 120, 740, 200);
gr.drawLine(735, 130, 735, 210);
gr.drawLine(730, 140, 730, 220);
gr.drawLine(725, 150, 725, 230);
gr.drawLine(720, 160, 720, 240);
gr.drawLine(715, 170, 715, 250);
gr.drawLine(710, 180, 710, 260);
gr.drawLine(705, 190, 705, 270);
gr.drawLine(700, 200, 700, 280);
gr.drawLine(695, 210, 695, 290);
gr.drawLine(690, 220, 690, 300);
gr.drawLine(685, 230, 685, 310);
gr.drawLine(680, 240, 680, 320);
gr.drawLine(675, 250, 675, 330);
gr.drawLine(670, 260, 670, 340);
gr.drawLine(665, 270, 665, 350);
gr.drawLine(660, 280, 660, 360);
gr.drawLine(655, 290, 655, 370);
gr.drawLine(650, 300, 650, 380);
gr.drawLine(645, 310, 645, 390);
gr.drawLine(640, 320, 640, 400);
gr.drawLine(635, 330, 635, 410);
gr.drawLine(630, 340, 630, 420);
gr.drawLine(625, 350, 625, 430);
gr.drawLine(620, 360, 620, 440);
gr.drawLine(615, 370, 615, 450);
gr.drawLine(610, 380, 610, 460);
gr.drawLine(605, 390, 605, 470);
gr.drawLine(600, 390, 600, 480);
//left mid line
gr.drawLine(625, 130, 425, 530);
//right mid line
gr.drawLine(975, 130, 775, 530);
//left backend receivers
leftReceiver1 = getImage(getDocumentBase(),"receiver.jpg");
gr.drawImage(leftReceiver1, 400, 100, 90, 90, this);
leftReceiver2 = getImage(getDocumentBase(),"receiver.jpg");
gr.drawImage(leftReceiver2, 325, 250, 90, 90, this);
leftReceiver3 = getImage(getDocumentBase(),"server1.jpg");
gr.drawImage(leftReceiver3, 75, 375, 150, 150, this);
//right backend receivers
rightReceiver1 = getImage(getDocumentBase(),"receiver1.jpg");
gr.drawImage(rightReceiver1, 1000, 100, 100, 100, this);
rightReceiver2 = getImage(getDocumentBase(),"receiver1.jpg");
gr.drawImage(rightReceiver2, 950, 250, 100, 100, this);
rightReceiver3 = getImage(getDocumentBase(),"receiver1.jpg");
gr.drawImage(rightReceiver3, 900, 400, 100, 100, this);
//left front liners
leftSpiker = getImage(getDocumentBase(),"spiker.jpg");
gr.drawImage(leftSpiker, 650, 30, 100, 100, this);
center = getImage(getDocumentBase(),"spiker.jpg");
gr.drawImage(center,600,150,100,100,this);
setter = getImage(getDocumentBase(),"setter.jpg");
gr.drawImage(setter, 500, 300, 150, 150, this);
//right front liners
rightSpiker = getImage(getDocumentBase(),"spiker1.jpg");
gr.drawImage(rightSpiker,650,350,130,130,this);
center = getImage(getDocumentBase(),"server.jpg");
gr.drawImage(center,750,150,130,130,this);
setter = getImage(getDocumentBase(),"defense1.jpg");
gr.drawImage(setter,850,90,110,110,this);
}
@Override
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
This is my current code. Can someone direct me what I am missing or what I did wrong here?