Im coding my first Java applet for a University project and im having issues.
Im in the process of coding a search and information function.

When the user clicks anywhere in the applet a black circle appears under the cursor, when they then press the information button, information about that location is displayed.

My problem is that the oval doesnt appear when clicking in the applet until I press the search of information buttons first, so the mouselistener doesnt work until and actionlistener is triggered first.

Here is a cut down version of my applets code.

import java.util.*;
import java.applet.Applet ;
import java.awt.*; // import the java . awt package
import java.awt.event.*; // import the java . awt . e v ent package
import java.awt.*;

public class applet extends Applet implements ActionListener, MouseListener 
{
	int ovalxcord,ovalycord,ovalsize=20;
	int x, y, width, height;
	String resultMessage;
	
    public void paint(Graphics g)
    //pre: g is valid graphics object
    //post: draws to Applet window
    {        
    	setSize(800,300);
    	
        // draw search
        Font standard = new Font("Arial", Font.PLAIN, 12);
        g.setFont(standard);
        g.drawString ("Search:", 200,20);
        g.drawString(resultMessage,250,50);
        
        // draw black circle
        g.setColor(Color.black);
	g.fillOval(ovalxcord-10,ovalycord-10,ovalsize,ovalsize);
        
    }
    
    public void mouseClicked( MouseEvent e)
    {
 	   ovalxcord=e.getX();
 	   ovalycord=e.getY();
 	   repaint();
    }
   
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}

    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
   	
   	public void start( )
   	{
   		addMouseListener(this);
   	}

	Button searchButton;
	Button infoButton;
	TextField searchField;
   	
   	public void init()
	{
	   	searchButton = new Button("Search");
	   	infoButton = new Button("Information");
		searchField = new TextField(20);
		add(searchField);
		add(searchButton);
		add(infoButton);
	
		searchButton.addActionListener(this);
		infoButton.addActionListener(this);
	}
      
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == searchButton)
            {resultMessage = searchField.getText();}
        else
        if(e.getSource() == infoButton)
        	{resultMessage = "Information " + ovalxcord + "," + ovalycord;}
        repaint();
	}
       
       
}

Please let me know if you have any ideas.

Thank you

Solved
All because a string had a null value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.