我的drawString 不起作用

发布于 2024-11-18 04:20:25 字数 1295 浏览 3 评论 0原文

我对java有点陌生,现在已经编程大约一年了,我目前正在开发一个项目,该项目允许用户选择地图(例如世界地图)并通过单击地图将城市添加到该地图。

当用户单击地图时,他/她输入一个名称,城市就会绘制在这些坐标上,这样就可以了。我的问题是,我还希望将城市的名称绘制在城市上方,但由于某种原因我无法让它工作。 这应该是一项简单的任务,但现在已经尝试了几个小时,并且开始变得非常烦人,所以我希望其他人可以帮助我完成这个简单的询问。

代码:

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());

提前致谢 /吉米

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-11-25 04:20:25

这是因为您的绘图坐标应该相对于组件定义。
您将组件的边界设置为 x,y,w,h 并将文本绘制到相同的 xy
如果x > wy > h,那么它将不可见。

将您的代码更改为以下内容,使用绘图命令的相对坐标:

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);
}

并请注意,由于您的边界宽度和高度,您的绘图区域只有 20px*20px。

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.

故笙诉离歌 2024-11-25 04:20:25

绘制字符串后,您将调用 klickadVal 或 oKlickadVal。这两种方法用单一颜色填充整个组件,覆盖您显示的字符串。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文