0

I have a custom button that is not being drawn properly. It shows up as a dot in the upper left hand corner. I have three classes. One for the button, one for the menu or drawing of several buttons and a main class that calls the menu.

Class 1 - button

@Override
protected void paintComponent(Graphics g) {
    g.setColor(Color.white);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(img, this.x, this.y,this.getWidth(), this.getHeight(), null);
    g2d.setFont(this.font);
    g2d.drawString(this.label, x, y);
}

Class 2 - menu

public void draw(Graphics g, int width, int height) {
    int x, y;       
    for(int i = 0; i < buttons.length; i++) {
        x = (int) (width / 2 - buttons[i].getWidth() / 2);
        y = (int) (height / 2 - buttons[i].getHeight() / 2);

        buttons[i].setBounds(x, y + (i*60), buttons[i].getWidth(), buttons[i].getHeight());
        buttons[i].paintComponent(g);
    }       
}

Class 3 - main

if(gMenu != null) {
                gMenu.draw(g, gWindow.getWidth(), gWindow.getHeight());
            }

Edit: To clarify what I'm trying to do is create a popup menu with a few custom buttons (components).

Edit: I got them to draw, but the mouse listener doesn't work.

here is the menu class

public class Menu extends JComponent {

GameWindow window;

public Menu(GameWindow window) {
    this.window = window;
    this.setVisible(true);
}

public void addChildToPanel(JComponent child) {
    int width = (int) getWidth();
    if(child.getWidth() > width) {
        width = child.getWidth();
    }
    int height = (int) getHeight();
    height += child.getHeight();
    setSize(width, height);

    Dimension screen = new Dimension(window.getWidth(), window.getHeight());
    int x = (screen.width - width) / 2;
    int y = (screen.height - height) / 2;
    setBounds(x, y, width, height);
}
}

Here is the button class public class MenuButton extends JComponent implements MouseListener {

private BufferedImage img;
private BufferedImage img_h;
private int x, y;
private String label;
private Font font;

private int state = 0;

// state 0 = normal
// state 1 = highlight
// state 2 = pressed

public MenuButton(BufferedImage img, BufferedImage img_h, String label, Font font) {
    this.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
    this.img_h = img_h;
    this.img = img;
    this.label = label;
    this.font = font;
    setVisible(true);

    setBounds(0, 0, img.getWidth(), img.getHeight());
    this.addMouseListener(this);
}

@Override
protected void paintComponent(Graphics g) {
    g.setColor(Color.white);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    switch (state) {
    case 0:
        g2d.drawImage(img, this.getBounds().x, this.getBounds().y,this.getWidth(), this.getHeight(), null);
        break;
    case 1:
        g2d.drawImage(img_h, this.getBounds().x, this.getBounds().y,this.getWidth(), this.getHeight(), null);
        break;
    case 2:
        g2d.drawImage(img, this.x, this.y,this.getWidth(), this.getHeight(), null);
        break;
    }

    g2d.setFont(this.font);

    int size = g2d.getFontMetrics(font).stringWidth(this.label);
    int x = this.getBounds().x + this.getWidth() - 20 - size;
    int y = this.getBounds().y + 29;

    g2d.drawString(this.label,x, y);
}

public void setState(int state) {
    this.state = state;
}

@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
    setState(1);
}

@Override
public void mouseExited(MouseEvent e) {
    setState(0);
}

@Override
public void mousePressed(MouseEvent e) {
    setState(2);
}

@Override
public void mouseReleased(MouseEvent e) {
    setState(1);
}

}

Child class that I call draw on public class PauseMenu extends Menu {

private int width;
private int height;

private MenuButton[] buttons = new MenuButton[4];

public PauseMenu(GameWindow window) {
    super(window);
    Font font = null;
    BufferedImage img = null;
    BufferedImage img_h = null;
    try {
        InputStream is = getClass().getResourceAsStream("/fonts/ALDOPC.ttf");
        font = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.PLAIN, 24f);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);

        img = ImageIO.read(getClass().getResourceAsStream("/gui/bg_menu_button_round.png"));
        img_h = ImageIO.read(getClass().getResourceAsStream("/gui/bg_menu_button_round_highlight.png"));
    }
    catch(Exception e)
    {
        System.out.print("Failed to load resources");
        System.out.println();
    }

    MenuButton resume = new MenuButton(img, img_h, "CONTINUE", font);
    final MenuButton quit = new MenuButton(img, img_h, "QUIT", font);
    MenuButton vid = new MenuButton(img, img_h, "VIDEO", font);
    MenuButton sound = new MenuButton(img, img_h, "SOUND", font);

    buttons[0] = resume;
    buttons[1] = vid;
    buttons[2] = sound;
    buttons[3] = quit;

    for(int i = 0; i < buttons.length; i++) {
        int x = (window.getWidth() - img.getWidth()) / 2;
        int y = (window.getHeight() - img.getHeight()) / 2;
        buttons[i].setBounds(x, y + (i * img.getHeight()), img.getWidth(), img.getHeight());
        this.addChildToPanel(buttons[i]);
    }
}

public void draw(Graphics g) {
    for(int i = 0; i < buttons.length; i++)
        buttons[i].paintComponent(g);
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
lostdev
  • 736
  • 8
  • 23
  • 1
    I don't know about others, but I'm totally lost with this. Consider creating and posting an [sscce](http://sscce.org) if you don't get a decent answer soon. Why are you calling paintComponent directly? This is most unusual you know. – Hovercraft Full Of Eels Feb 26 '13 at 03:08
  • 1
    Where is the `Graphics` context coming from? You should rarely need to call `paintComponent` directly. You should also be calling `super.paintComponent`, but that's another issue. This is not enough information to go on, we really need a working example – MadProgrammer Feb 26 '13 at 03:08
  • 1
    @HovercraftFullOfEels Concern over `paintComponent` method, worried over `setBounds` and calling `paintComponent` directly, terrified over where `g` is coming from - so no, you're not the only one whose lost – MadProgrammer Feb 26 '13 at 03:09
  • So the main loop actually calls draw itself using setIgnoreRepaint(true) on the frame. So the main class's main loop calls Graphics2D g = (Graphics2D) gWindow.getBufferStrategy().getDrawGraphics(); where gWindow is the frame. – lostdev Feb 26 '13 at 03:18
  • 1
    Not sure why you keep invoking paintComponent() and draw()? If you are creating custom components then they should be able to paint themselves. Your Menu class should probably just be a panel with a proper layout manager (GridLayout?) so you don't have to calculate all the sizes. – camickr Feb 26 '13 at 05:43
  • I have tried using a LayeredPane with 2 overlapping panels. One that doesn't repaint overlayed by a normal panel. Adding my menu to this panel doesn't draw them. The other panel that I override the draw is for my gaming surface with sprites and such. – lostdev Feb 26 '13 at 07:07
  • @camickr I used your suggestion and redid the design to use a layout manager and it works perfectly. The custom components had to use 0,0 as their position for the layout to align them properly. Thanks! – lostdev Mar 02 '13 at 18:33

0 Answers0