Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic