工具提示文字会清除Java中的面板图 [英] tooltip text erases panel drawing in java

查看:103
本文介绍了工具提示文字会清除Java中的面板图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel,在其上使用paintComponent方法执行了一些绘图,此后,当用户单击JPanel时,在其上绘制了一个字符串(或任何图形),并且随着用户移动鼠标在JPanel上,它在JPanel的工具提示中显示坐标.

I have a JPanel on which some drawing is performed using paintComponent method and after that when ever a user clicks on the JPanel a string is drawn (or any drawing) on it and as the user moves mouse over the JPanel it shows the coordinates in the tooltip of the JPanel.

1)问题是,当工具提示移到绘制的字符串上方时,它会擦除​​它,但是此工具提示文本对我在paintComponent方法中执行的绘图部分没有擦除效果.我不明白为什么会这样.

1) The problem is that when the tooltip comes over the drawn string it erases it but this tooltiptext has no erasing effect on the drawing part which I performed in paintComponent method. I am not able to understand that why this is happening.

2)而且,当我单击单击绘制字符串,然后最小化并还原我的应用程序时,绘制的字符串也消失了.

2) And also when I draw string on click and then minimize and restore my application my drawn strings are gone.

希望大家都明白我的意思.

Hope u all understand what I mean to say.

这是代码:

@Override
public void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);
    Graphics2D graphics2D = (Graphics2D) graphics;
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    drawBorder(graphics2D);
    drawGrid(graphics2D);
}

private void drawBorder(Graphics2D graphics2D) {
    graphics2D.setColor(Color.ORANGE);
    graphics2D.setStroke(new BasicStroke(borderStroke));
    graphics2D.drawRoundRect(panelStartLoc.x, panelStartLoc.y, panelBorder.width,
            panelBorder.height, borderRoundness, borderRoundness);
}

private void drawGrid(Graphics2D graphics2D) {
    graphics2D.setColor(Color.ORANGE);
    graphics2D.setStroke(new BasicStroke(gridCellStroke));

    for (int row = gridStartLoc.x; row < panelBorder.getWidth(); row += cellWidth + cellHorGap) {
        for (int col = gridStartLoc.y; col < panelBorder.getHeight(); col += cellHeight + cellVerGap) {
            graphics2D.drawRoundRect(row, col, cellWidth, cellHeight, cellRoundness, cellRoundness);
        }
    }
}

public void drawSubjectAtClickLoc(int subjectCode) {
    Color color = getBackground();
    String drawString = null;
    int subjectDrawXLoc = cellClickLoc.x + 4;
    int subjectDrawYLoc = (cellClickLoc.y + cellHeight) - 3;
    Graphics2D graphics2D = (Graphics2D) getGraphics();

    if (subjectCode == SUBJECT_CLEAR) {
        graphics2D.setColor(getBackground());
        graphics2D.fillRoundRect(cellClickLoc.x + 2, cellClickLoc.y + 2, cellWidth - 4, 
                cellHeight - 4, cellRoundness, cellRoundness);
        return;
    }
    if (subjectCode == SUBJECT_HUMAN) {
        color = Color.WHITE;
        drawString = "H";
    }
    if (subjectCode == SUBJECT_RESOURCE) {
        color = Color.GREEN;
        drawString = "R";
    }

    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.setFont(new Font(null, Font.BOLD, 26));
    graphics2D.setColor(color);
    graphics2D.drawString(drawString, subjectDrawXLoc, subjectDrawYLoc);
}

先谢谢....

推荐答案

当屏幕被覆盖时,Java会调用paintComponent()来修复屏幕.如果您在paintComponent()方法之外进行绘制,则在固定屏幕后,多余的图形将被删除.

When your screen is covered, Java calls paintComponent() to fix the screen. If you draw outside of the paintComponent() method, then when the screen is fixed up, your extra drawings will be erased.

所以不要那样做:在paintComponent()中完成所有图形.当用户单击某处时,将要绘制的字符串和坐标添加到某种数据结构(即对象列表,每个对象包含一个String和一些坐标),然后调用repaint().在您的paintComponent()方法中,查看该数据结构并绘制字符串.

So don't do it that way: do all of your drawing in paintComponent(). When the user clicks somewhere, add the string you want to draw and the coordinates to a data structure of some kind (i.e., a list of objects, each object containing a String and some coordinates), then call repaint(). In your paintComponent() method, look in that data structure and draw the strings.

这篇关于工具提示文字会清除Java中的面板图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆