aspose file tools*
The moose likes Beginning Java and the fly likes I can't figure out why this is not working! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I can Watch "I can New topic
Author

I can't figure out why this is not working!

Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
Here are my requirements:
Create a wrapper HTML file that opens up a 300 x 200 window.
The applet window should have a black background. Initially, the window contains a String representing your name printed at position (50, 50) of the window. [For example, my program would print Amber Settle at position (50, 50)]. Your name should be displayed in cyan when the window first appears.
When the user clicks the mouse anywhere in the applet window, your name is redrawn at that position in the window.
The first time the name is redrawn it should be displayed in pink. The second time is is redrawn it should be displayed in cyan again. This alternation of colors should continue indefinitely. [In particular, every nth time it is redrawn, where n is odd, it should appear in pink. Every nth time it is redrawn, where n is even, it should appear in cyan].
To achieve this behavior, you will have to write a listener class that implements MouseListener. Within the listener's mouseClicked method, use the getX() and getY() methods of the event to determine where the click occurred. You should then reset the instance variables representing the current position in the window to those values. To alternate colors you should have an instance variable that indicates the number of times the name has been redrawn. Set it to 0 initially. Whenever the mouse is clicked, it should be incremented. Whenever the number of times it has been redrawn is even, display the name in cyan. If it is odd, display it in pink.
When I open the Applet it opens in pink and when i click it won't move. can you give me some advice?
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
First things first,
1. you are declaring and initializing variables x and y
2. you are trying to update variables x and y on a mouseClicked event
Where are you using these variables to paint?


"It is almost always answered in the API" - Murali Nanchala
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
I thought I needed the coordinates of where the mouse was clicked, so x and y would be set to the event. What should I use?
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
The way you are setting the x and y values in the mouseClicked() method is correct. Murlai's point was that your code does not use the x and y values in your paint() method.
Mark
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
I tried that but i got an error. This is what wrote:
g.drawstring("Yvette Hobbs" x-4, y-4, 50,50);
Is this the proper syntax?
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
This question could be easily answered by looking at the API documentation. The method signature is:

drawString(String str, int x, int y)

Mark
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Lisa,
Two adjustments needed
1. statements inside "if" clause of paint() method should be grouped.
2. i++ added inside MouseClicked method.

Originally posted by Lisa Smith:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class AlternateName extends Applet
{
// Keep track of the position of the last mouse click,
// and whether or not the mouse is in the window
int i = 0;
private int x = 10, y = 10;
private boolean mouseHere = false;
// Create a listener for mouse events and add to the applet
public void init()
{
i++;
ClickListener recorder = new ClickListener();
addMouseListener(recorder);
}
// Locate and color the dot appropriately
public void paint(Graphics g)
{

if (i %2 == 1){ // { added
setBackground(Color.black) ;
g.setColor(Color.cyan);
g.drawString("Yvette Hobbs",50,50);
} // } added
if (i %2 == 0){ // { added
setBackground(Color.black) ;
g.setColor(Color.pink);
g.drawString("Yvette Hobbs",50,50);
} // } added
}
// Definition of the mouse listener as an inner class
private class ClickListener implements MouseListener
{
// Record the location of the mouse click event
public void mouseClicked(MouseEvent event)
{
x = event.getX();
y = event.getY();
i++; // increment i with mouse click
repaint();
}
// Empty definitions for unimplemented methods
public void mouseExited(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
}
}


[ April 16, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I can't figure out why this is not working!
 
Similar Threads
doubt in AWTEventMulticaster
DragBallPanel
how to detect click on panel
how to keep drawn free hand painted image when resizing the window
Moving multiple ojects in Java2D