I have a PopUp class that expands and contracts like a pop-up. It extends JPanel.
I have overridden the typical visibility methods for the JPanel to choose whether the PopUp object should be drawn. The JPanel should be visible only when the pop-up is fully expanded.
However, this is the part that does not work.
Here is the relevant PopUp class code. I added some comments that hopefully help:
public class PopUp extends JPanel {
/**
* Expanded x coordinate
*/
private double x;
/**
* Expanded y coordinate
*/
private double y;
/**
* Expanded width value
*/
private double width;
/**
* Expanded height value
*/
private double height;
/**
* Number of steps until fully expanded
*/
private int steps;
/**
* This divided by steps is the percentage the pop-up is expanded
*/
private int expansionStage = 0;
/**
* Whether or not the pop-up is expanding
*/
private boolean isExpanding = false;
/**
* Whether or not the pop-up is visible
*/
private boolean visible;
/**
* Color of the pop-up
*/
private Color color;
/**
* The rectangle that represents the bounds of the pop-up
*/
private Rectangle2D popUp;
/**
* The currently used transform for the pop-up
*/
private AffineTransform trans;
/**
* Initializes a newly created {@code PopUp} with a uniform color
* @param x The x coordinate of the expanded pop-up
* @param y The y coordinate of the expanded pop-up
* @param w The width of the expanded pop-up
* @param h The height of the expanded pop-up
* @param expansionSteps The number of steps until fully expanded
* @param popUpColor The color of the pop-up
*/
public PopUp(double x, double y, double w, double h, int expansionSteps, Color popUpColor) {
this.x = x;
this.y = y;
width = w;
height = h;
color = popUpColor;
steps = expansionSteps;
this.borderWidth = 0;
this.borderColor = null;
popUp = new Rectangle2D.Double(0, 0, width, height);
setBounds((int) Math.round(x), (int) Math.round(y), (int) Math.round(w), (int) Math.round(h));
trans = new AffineTransform();
//Centers the rectangle pop-up at the center of the given rectangle made by the given x, y, width, and height
trans.translate(x + width/2 * (1 - (double) expansionStage/steps), y + height/2 * (1 - (double) expansionStage/steps));
//Scales the rectangle based on the percentage it is expanded
trans.scale((double) expansionStage/steps, (double) expansionStage/steps);
}
/**
* Draws the pop-up
* @param g Graphics object from paintComponent
*/
public final void draw(Graphics g) {
//Expands pop-up one step
if(isExpanding && visible)
expansionStage = Math.min(expansionStage + 1, steps);
//Contracts pop-up one step
else if(visible)
expansionStage = Math.max(expansionStage - 1, 0);
//Resets pop-up size to 0
else
expansionStage = 0;
if(visible) {
//Sets the visibility of the JPanel to true if the pop-up is fully expanded (false otherwise)
super.setVisible(expansionStage/steps == 1);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform trans = new AffineTransform();
//Centers the rectangle pop-up at the center of the given rectangle made by the given x, y, width, and height
trans.translate(x + width/2 * (1 - (double) expansionStage/steps), y + height/2 * (1 - (double) expansionStage/steps));
//Scales the rectangle based on the percentage it is expanded
trans.scale((double) expansionStage/steps, (double) expansionStage/steps);
this.trans = trans;
g2d.setColor(color);
Shape transformed = trans.createTransformedShape(popUp);
g2d.fill(transformed);
}
else
super.setVisible(false);
}
/**
* Sets whether the pop-up is expanding or not
* @param expanding Whether or not the pop-up should expand
*/
public final void setExpanding(boolean expanding) {
isExpanding = expanding;
}
/**
* Returns whether or not the pop-up is expanding
* @return Whether or not the pop-up is expanding
*/
public final boolean getExpanding() {
return isExpanding;
}
/**
* Returns the percentage that the pop-up has expanded
* @return The percentage that the pop-up has expanded
*/
public final double percentageExpanded() {
return (double) expansionStage/steps;
}
/**
* Different than JPanel.setVisible(boolean visible) in that it
* only draws the PopUp if this is true, and the JPanel is visible
* only when this is true and the popUp is expanded
* @param visible Whether or not the pop-up should be visible
*/
@Override
public void setVisible(boolean visible) {
this.visible = visible;
}
/**
* Different than JPanel.isVisible() in that it
* only draws the PopUp if this is true, and the JPanel is visible
* only when this is true and the popUp is expanded
* @return Whether or not the pop-up should be visible
*/
@Override
public boolean isVisible() {
return visible;
}
public boolean jPanelIsVisible() {
return super.isVisible();
}
}
I set it up by creating one. Then, I then add it to the main JPanel and set its visibility to true.
In the paintComponent() method for the main JPanel, I put a call to PopUp.draw(g).
Finally, I have PopUp.setExpanding(true) when I want it to expand and PopUp.setExpanding(false) when I want it to contract.
Let me know if any other information is required.
Update:
I am planning to use a modified version of MadProgrammer's version of my PopUp class, but I thought I would let you all know what the real problem was.
MadProgrammer had the right idea when he was thinking the visibility was the problem. When I was using super.setVisible(), it referred to my isVisible() method for my PopUp, which was unfortunate.