Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top