The issue appears to be with how you're drawing to the window. Your draw function doesn't account for the fact that you need to draw the pop up menu too. The way the Swing system works is that the frame has its repaint method called by the operating system. This message trickles down to each component, painting it along the way using the frame's Graphics object. You're bypassing that system and painting to the screen manually.
The solution is fairly simple. You need to keep a reference to the PopUpDemo class you create and use that to paint it.
For the PopClickListener:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class PopClickListener extends MouseAdapter {
PopUpDemo menu;
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
doPop(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
doPop(e);
}
}
private void doPop(MouseEvent e) {
menu = new PopUpDemo();
menu.setIgnoreRepaint(true);
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
Note the addition of the menu variable.
For the Driver class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Driver extends JFrame {
boolean isRunning = true;
int fps = 50;
int windowWidth = 400;
int windowHeight = 400;
BufferedImage backBuffer;
Insets insets;
InputHandler input;
PopClickListener popupClick;
...
void draw() {
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.WHITE);
bbg.fillRect(0, 0, windowWidth, windowHeight);
bbg.setColor(Color.BLACK);
bbg.drawOval(x, y, 20, 20);
if(popupClick.menu != null && popupClick.menu.isVisible()){
if(popupClick.menu.getParent() != null){
bbg.translate(popupClick.menu.getParent().getX()
, popupClick.menu.getParent().getY());
popupClick.menu.paint(bbg);
}
}
g.drawImage(backBuffer, insets.left, insets.top, this);
}
}
Two things to note here. First is the addition of the popupClick variable, which keeps track of the PopClickListener you're using. This way we can access the menu variable I mentioned earlier. Second is the change to the draw() method. You'll note that I added two lines, one to check if the menu exists and the other to call the paint method of the popup menu, passing it the Graphics object you're using for your buffering. The other important thing to note is that I had to call getParent().getX() and getParent().getY() to get the menu to draw in the right place. This is because the menu normally draws itself at 0,0 (in its parent's component space). That parent component is normally a JPanel located inside the JFrame's LayeredPane.
An alternative is to call repaint() on the menu component instead of translating the origin and then calling paint, but that tends to generate flickering because repaint() doesn't paint immediately, but instead paints when the JVM has a chance to get around to it.