I am currently trying to display a world map, on to which I want to draw dots in the centroids of each country. I have already found all the necessary coordinates, but my main issue is actually drawing those coordinates based on user input. Instead of using a scanner and taking system input, I am receiving a string in JLabel, that I then want to find the associated coordinate for in a hash map that I created. I am able to receive the input using an ActionListener, but don’t know how to approach then using that input to draw something.
I attempted to make a String instance variable; public static String in; which is meant to store the inputted value in the ActionListener method:
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
((DefaultListModel) answers.getModel()).addElement(textField.getText());
in = (String)textField.getText();
}
}
By printing within the method, along with a display box that I put on to the JFrame printing user input, I know that I’m able to receive the string they enter. If, then, in a paint() method, I want to take that input and use it to draw something, why does the following code do nothing?
protected void paintComponent(Graphics g) {
super.paintComponent(g);
background.paint(g);
g.setColor(Color.red);
if(in != null) {
g.drawOval(500, 500, 100, 100);
}
}
Currently, the logic isn’t implemented to actually draw the right location, but I just want to be able to know that I am indeed able to recognize the non-empty string in the paint method. My understanding is that the paint method constantly runs while the program is running, so what exactly do I need to do to actually be able to store the inputted text and display it?
Thanks!
Adarsh Venkateswaran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.