Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6103909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:46:56+00:00 2026-05-23T13:46:56+00:00

Im somewhat new in java, been programming for about a year now and im

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:46:57+00:00Added an answer on May 23, 2026 at 1:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been thinking about starting a new graphics project and I want to use
I've been somewhat spoiled using Eclipse and java. I started using vim to do
We are looking at reworking a java business management web service that has been
At a new job I started, we have both a Java application that handles
I am somewhat new to using JPA -- I'll put that out there right
I'm somewhat new to the ASP.NET MVC architecture and I'm trying to sort out
Does VS2008 have somewhat C++0x standard support? DUPLICATE Visual Studio support for new C
Somewhat related to my question about integers instead of decimals; my vendor provides a
Working on a somewhat complex page for configuring customers at work. The setup is
I'm new to Java/Eclipse/Android, so this is probably an easy (if not stupid) question:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.