0

Im somewhat new in java, been programming for about a year now and im currently working on a project that lets the user choose a map (worldmap for example) and add cities to that map by clicking the map.

When the user clicks on the map he/she inputs a name and the city is drawn on those coordinates, and that work's fine. My problem is that I also want the name of the city to be drawn above the city, but I can't get it to work for some reason. It should be an easy task, but been trying for several hours now and it's starting to get very annoying so I hope someone else can help me with this simple enquiry.

The code:

public class Rita extends JComponent{
    private boolean klickad=false;
    protected int xx=0;
    private int yy=0;
    public Rita(int x, int y){
        xx=x;
        yy=y;
        setBounds(x, y, 20, 20);
        setPreferredSize(new Dimension(20,20));
        setMaximumSize(new Dimension(20,20));
        setMinimumSize(new Dimension(20,20));
    }

protected void paintComponent(Graphics g){
    super.paintComponent(g);
        drawString(g, xx+5, yy);
        if(klickad==false)
            klickadVal(g, xx, yy);

        else if(klickad==true)
            oKlickadVal(g);
    }
public void drawString(Graphics g, int x, int y){
    setFont(new Font("Courier New", Font.PLAIN, 16));
    g.setColor(Color.BLACK);
    g.drawString("Test test test test test", x, y);
}

public void klickadVal(Graphics g, int x, int y){
    g.setColor(Color.RED);
    g.fillRect(0,0,getWidth(),getHeight());
}

public void oKlickadVal(Graphics g){
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, getWidth(),getHeight());

Thanks in advance /Jimmy

John Snow
  • 5,214
  • 4
  • 37
  • 44
  • When you say that you "can't get it to work", are you getting any errors or anything like that? –  Jul 01 '11 at 09:20
  • How is it not working? Is the method being called? Is the string displaying at all? – Patrick Jul 01 '11 at 09:22
  • 1
    Just a hint: `if(klickad==false) ... else if(klickad==true) ...` would better be `if(klickad==false) ... else ...` or yet better `if(!klickad) ... else ...` - `klickad` can only either be true or false :) – Thomas Jul 01 '11 at 09:22
  • Not getting any error messages, and my rectangle is drawn on the map, but the string won't show. The drawString method is called (checked that with a println) but the string isn't drawn on the map – John Snow Jul 01 '11 at 09:31

2 Answers2

0

It's because of your drawing coordinates should be define relative to the component. You are setting the bounds of the component to x,y,w,h and drawing your text to the same x and y.
If x > w or y > h, then it won't be visible.

Change your code to this, using relative coordinates for the drawing commands:

protected void paintComponent(Graphics g){
    super.paintComponent(g);
        drawString(g, 5, 10);
        if(klickad==false)
            klickadVal(g, 0, 0);

        else if(klickad==true)
            oKlickadVal(g);
}

And be aware of that your drawing area is only 20px*20px, because of your bounds width and height.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • That worked. Thanks man! Wasn't realizing that when using this method, my string will be drawn inside the rectangle. Suppose I will need another object only containing the drawString method with my x and y coordinates + a few pixles for it to be drawn ontop of the rectangle. Thanks – John Snow Jul 01 '11 at 09:49
0

You are calling klickadVal or oKlickadVal after you've painted the string. These two methods fill the entire component with a single color overwriting the string you've displayed.

spot35
  • 888
  • 4
  • 9
  • Thought about that earlier, so tried to remove the drawRect and only called drawString but that didn't work so that wasn't the problem. Thanks anyway for your input – John Snow Jul 01 '11 at 09:46