Hi everyone,
I have a lab assignment that I need to do, and currently I'm stuck at the step to make the "arrow" move every time the user clicks. Here is the description of my assignment:
So far here is my code :Start by drawing an arrow pointing at the upper-left corner of the screen (use the drawLine method)
Now create two instance variables to represent the x and y screen coordinates where the arrow should be drawn. Then adjust your paint method to draw the arrow wherever the x and y coordinates dictate, rather than the upper-left corner. To test that this works, set your instance variables to different values in the init method, and see if your arrow goes there when you run the applet.
Next, fill in the mousePressed method with code that updates the x and y coordinates of the arrow to be wherever the mouse was pressed down. To do this, you will need to use the getX() and getY() methods of the MouseEvent that is being passed in as a parameter (these methods return int values - see the MouseEvent Javadocs). You also need to make a call to repaint(); from mousePressed. This tells Java to make another call to your paint method as soon as it gets the chance. Note that repaint is a method inherited from the Applet class; we can call it because our class extends Applet.
Now you should be able to click on your applet and the arrow will go wherever you click.
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Target extends Applet implements MouseListener, KeyListener { int x, y; public void init() { //init is called when the applet starts (much like a constructor) addMouseListener(this); //registers this applet to receive mouse events addKeyListener(this); //registers this applet to receive key events } public void paint(Graphics g) { g.drawLine(x, y, x+20, y+20); g.drawLine(x, y, x, y + 7); g.drawLine(x, y, x + 7, y); } public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); e.translatePoint(x, y); repaint(); } }
I know that the code inside mousePressed is wrong, but I can't figure out what's wrong with it. I try to click on the screen, but of course, the arrow does not move
And I also want to ask about KeyPressed method too. The instructions were :
I took a look at the javadoc for this class, but to be honest, I don't understand what the instructors want me to do ...Next fill in the keyPressed method. Make it so the arrow can move in any of the four directions given by the arrow keys on the keyboard. You can do this by using the getKeyCode method of KeyEvent and comparing the result with the static variables made available in the KeyEvent class. For example, to test if the left arrow key was pressed, use the following:
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
Not surprisingly, the constants you want are KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, and KeyEvent.VK_DOWN.
Now you should be able to move the arrow by either clicking or using the arrow keys (you will have to click on the applet first to give the applet focus).
Please take a look at my problem and suggest me what I should change for it to work. Thanks everyone in advance.