java JDialog draw called double

78 views Asked by At

I've got a problem which makes me go crazy. I call a Method via this line of code:

private void btnGetFolderMouseClicked(MouseEvent e) {
    MessageController.showAlert(AlertBox.AlertType.OK);
}

and this method is called:

public class MessageController {
public static void showAlert(AlertBox.AlertType alertType) {
    AlertBox alertBox = new AlertBox("Test12345678910dfkjsdgmdgbu<xdfg<bdxgj ghfhftz rgdx", alertType);
    alertBox.getRootPane().setOpaque(false);
    //alertBox.getContentPane ().setBackground (new Color(0, 0, 0, 0));
    //alertBox.setBackground (new Color(0, 0, 0, 0));
    alertBox.setVisible(true);

//      BDialog dialog = new BDialog();
//      dialog.setVisible(true);

    System.out.println("Nach Box");
    //alertBox.invalidate();
}
}

And here is my JDialog class:

public class AlertBox extends JDialog {
private ImageIcon downSide = new ImageIcon(getClass().getResource("/AlertBox/Down_Side.png"));
private ImageIcon leftDownCorner = new ImageIcon(getClass().getResource("/AlertBox/Left_Down_Corner.png"));
private ImageIcon leftSide = new ImageIcon(getClass().getResource("/AlertBox/Left_Side.png"));
private ImageIcon leftUpCorner = new ImageIcon(getClass().getResource("/AlertBox/Left_Up_Corner.png"));
private ImageIcon rightDownCorner = new ImageIcon(getClass().getResource("/AlertBox/Right_Down_Corner.png"));
private ImageIcon rightSide = new ImageIcon(getClass().getResource("/AlertBox/Right_Side.png"));
private ImageIcon rightUpCorner = new ImageIcon(getClass().getResource("/AlertBox/Right_Up_Corner.png"));
private ImageIcon upSide = new ImageIcon(getClass().getResource("/AlertBox/Up_Side.png"));

private String text;

private Rectangle2D fontSize;

private Font font = new Font("Arial", Font.BOLD, 13);

private ImagedButton btnOK;

public enum AlertType{
    OK,
    Warning,
    Error
}

private AlertType alertType;

public AlertBox(String text, AlertType alertType){

    // Set JDialog properties
    super(new JFrame(), true);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    setUndecorated(true);
    setLocationRelativeTo(null);

    this.text = text;
    this.alertType = alertType;

    btnOK = new ImagedButton("OK");
    btnOK.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button Clicked");
            exitAlertBox();
        }
    });
    //this.add(btnOK);
}

@Override
public void paint(Graphics g) {
    // Create Graphics2D Object
    Graphics2D g2d = (Graphics2D) g.create();

    // Get the Size
    fontSize = StringMetrics.getBounds(g2d, font, text);

    // set the fontSize
    int width = (int) fontSize.getWidth() + 5 + leftSide.getIconWidth() + 5 + rightSide.getIconWidth();
    int height = leftUpCorner.getIconHeight() + 5 + (int)fontSize.getHeight() + 5 + btnOK.getHeight() + 5 + leftDownCorner.getIconHeight();
    this.setSize(width, height);

    //this.replaceButton(width, height);

    // Draw Left Upper Corner
    System.out.println(leftDownCorner.getIconWidth() + " ; " + leftDownCorner.getIconHeight());
    g.drawImage(leftUpCorner.getImage(), 0, 0, this);
    g.drawImage(new ImageIcon(getClass().getResource("/Buttons/Button_Down.png")).getImage(), 21, 20, this);

    // Draw Upper Side
    //g.drawImage(upSide.getImage(), leftUpCorner.getIconWidth(), 0, 5 + (int) fontSize.getWidth() + 5, upSide.getIconHeight(), this);

    // Draw Right Upper Corner
    //g.drawImage(rightUpCorner.getImage(), this.getWidth() - rightUpCorner.getIconWidth(), 0, this);

    // Draw Left Side
    //g.drawImage(leftSide.getImage(), 0, leftUpCorner.getIconHeight(), leftSide.getIconWidth(), 5 + (int)fontSize.getHeight() + 5, this);

    // Draw Right Side
    //g.drawImage(rightSide.getImage(), width - rightSide.getIconWidth(), rightUpCorner.getIconHeight(), rightSide.getIconWidth(), 5 + upSide.getIconHeight() + 5, this);

    // Draw Left Downer Corner
    //g.drawImage(leftDownCorner.getImage(), 0, height - leftDownCorner.getIconHeight(), this);

    // Draw Right Downer Corner
    //g.drawImage(rightDownCorner.getImage(), this.getWidth() - rightUpCorner.getIconWidth(), height - rightDownCorner.getIconHeight(), this);

    // Draw Downer Side
    //g.drawImage(downSide.getImage(), leftDownCorner.getIconWidth(), height - downSide.getIconHeight(), 5 + (int) fontSize.getWidth() + 5, downSide.getIconHeight(), this);



    // Draw the text
    //g2d.drawString(text, 10, 10);
    //g2d.drawString(text, leftUpCorner.getIconWidth() + 5, (int)fontSize.getHeight() + 8);


    if (this.alertType == AlertType.OK){

    }
}

private void replaceButton(int width, int height){
    // paint OK-Button
    int testWidth = width - (btnOK.getWidth() / 2);
    int testHeight = height - btnOK.getHeight() - 5;

    btnOK.setBounds(testWidth, testHeight, btnOK.getWidth(), btnOK.getHeight());
}

private void exitAlertBox(){
    this.dispose();
}
}

The problem is, that the window should (at this version of code) display 2 Images. Well it does.. sometimes.. But most of the runs it doesn't.

not showing

showing

When I debug my code, it seems like the 'paint' Method is called twice, but i don't find the line where this happens.

Did i do anything wrong?

Thanks for your help :)

0

There are 0 answers