0

I am trying to make a (very) simple applet using Java. I am new to programming and its concepts.

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JApplet;

public class EuroCapitalsApplet extends JApplet{

    private Image image = ("Mountain.jpg"));
    String[] mountainArray = new String [14];
    ArrayList<String> arrayList = new ArrayList<String>();
    String mountain;
    Map<String, Object> map = new HashMap<String, Object>();

    public void init()
    {
        mountain=getParameter("citiesToMark");
        mountainArray = cities.split("\\,");
        for(int i = 0; i < mountainArray.length; i++)
        {
           arrayList.add(mountainArray[i]); 
            }
           map.put("Tall", 140);
    }

    public void paint(Graphics g){
           super.paint(g);
           g.drawImage(image, 0, 0, this);
           if(mountainList.get(0).equals("Tall")){
               System.out.println("Hello");
           }
    }
}

I have put the "Hello" statement in there just to see how many times it prints out - its currently prints out 23 times where as I would like to print out once only I do not know what I am doing wrong, it took me a while to get this far.

I do not know much about loops only what I have read on here and through videos on Youtube. Could someone please push me in the right direction? I have little experience bar some HTML and CSS.

5
  • What are you trying to do ? What is your problem? Commented Feb 28, 2014 at 19:11
  • I don't even know how you were able to run this program. Commented Feb 28, 2014 at 19:15
  • I am trying to paint a small line on the image. I do not have the code for this in the example but i know how to do it. I was hoping to place an if statement in the paint method that checks to see if a certain string is in the array, and if it is the line is drawn. Commented Feb 28, 2014 at 19:16
  • Also stop with the YouTube tutorials. Go to the official tutorials here and look at the resources in the tag wiki Commented Feb 28, 2014 at 19:17
  • Thanks for the replies. I obviously don't know what i am doing, i gave it ago anyway. Thanks again. Commented Feb 28, 2014 at 20:53

2 Answers 2

1

The paint method is called every time something changes, this can include things like window resizing, minimizing, etc. The paint method should have little to no logic, as it will slow down your applet a lot.

Sign up to request clarification or add additional context in comments.

Comments

0

Each time the applet refreshes the paint method is invoked ... consider that it can be invoked hundred of times in couple of seconds.

You should put there only the methods responsible for rendering something graphic and possibly with low execution time.

Look at this: http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html

P.S you cannot know exactly how many times the paint method is inked so don't put in there application logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.