tooltip text erases panel drawing in java

痞子三分冷 提交于 2019-12-24 00:59:54

问题


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) 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) 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.

Here is the code :

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

thanx in advance....


回答1:


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.

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.



来源:https://stackoverflow.com/questions/7190645/tooltip-text-erases-panel-drawing-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!