未正确绘制自定义Java组件

时间:2013-02-26 02:57:56

标签: java swing graphics2d paintcomponent jcomponent

我有一个未正确绘制的自定义按钮。它在左上角显示为一个点。我有三节课。一个用于按钮,一个用于菜单或绘制几个按钮,另一个用于调用菜单。

第1课 - 按钮

@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);
}

第2课 - 菜单

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);
    }       
}

第3类 - 主要

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

编辑:澄清我要做的是创建一个带有一些自定义按钮(组件)的弹出菜单。

编辑:我让他们画画,但鼠标监听器不起作用。

这是菜单类

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);
}
}

这是按钮类     公共类MenuButton扩展JComponent实现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);
}

}

我打电话给的儿童班     公共类PauseMenu扩展了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);
}
}

0 个答案:

没有答案
相关问题