我的drawString 不起作用
我对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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的绘图坐标应该相对于组件定义。
您将组件的边界设置为
x,y,w,h
并将文本绘制到相同的x
和y
。如果
x > w
或y > h
,那么它将不可见。将您的代码更改为以下内容,使用绘图命令的相对坐标:
并请注意,由于您的边界宽度和高度,您的绘图区域只有 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 samex
andy
.If
x > w
ory > h
, then it won't be visible.Change your code to this, using relative coordinates for the drawing commands:
And be aware of that your drawing area is only 20px*20px, because of your bounds width and height.
绘制字符串后,您将调用 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.