devdon
devdon

Reputation: 31

java graphics2d instances of same class behave differently

New to Java, I don't understand why the following code works for the first button I add to the form and not the other two instances from the same class. Also, the mouseExit handler doesn't seem to get called at all, or only when exiting all buttons, not each one separately. Can you help me?

The setup is that I have a test case with a simple JFrame, a contentPane, and a JLabel with an icon showing a piano keyboard. That's it. I create instances of polygon buttons representing keys and all they are suppposed to do is hilight as the mouse passes over them, which the first one does and the others do in text but not in graphics - meaning that the proper handlers are called but no painting is done.

package com.mst.buttons;

import java.awt.BorderLayout;

public class buttonFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    buttonFrame frame = new buttonFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public buttonFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 578, 373);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JLabel piano = new JLabel("");
        piano.setLabelFor(piano);
        piano.setIcon(new ImageIcon("C:\\Users\\Donald\\workspace2\\Keyboard450x220.png"));
        GridBagConstraints gbc_piano = new GridBagConstraints();
        gbc_piano.insets = new Insets(0, 0, 5, 5);
        gbc_piano.fill = GridBagConstraints.BOTH;
        gbc_piano.gridx = 2;
        gbc_piano.gridy = 1;
        contentPane.add(piano, gbc_piano);

        ////////////////////////////////////////////////
        int[] xs1 = new int[] { 0,45,45,0,0 };
        int[] ys1 = new int[] { 0,0,220,220,0 };
        Polygon p1 = new Polygon(xs1, ys1, xs1.length);
        String n1 = "C3";

        int[] xs2 = new int[] { 45,90,90,45,45 };
        int[] ys2 = new int[] { 0,0,220,220,0 };
        Polygon p2 = new Polygon(xs2, ys2, xs2.length);
        String n2 = "D3";

        int[] xs3 = new int[] { 90,135,135,90,90 };
        int[] ys3 = new int[] { 0,0,220,220,0 };
        Polygon p3 = new Polygon(xs3, ys3, xs3.length);
        String n3 = "D3";

        PianoButton pb1 = new PianoButton(p1, n1);
        piano.add(pb1);

        PianoButton pb2 = new PianoButton(p2, n2);
        piano.add(pb2);

        PianoButton pb3 = new PianoButton(p3, n3);
        piano.add(pb3);
    }
}


class PianoButton extends JComponent implements MouseListener {

    Polygon key;
    String noteName;
    Color color;

    public PianoButton(Polygon p, String nn) {
        key = p;
        noteName = nn;
        color = Color.white;

        setBounds(key.getBounds());
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {}

    @Override
    public void mouseEntered(MouseEvent arg0) {
        hilite();
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        unhilite();
    }

    @Override
    public void mousePressed(MouseEvent arg0) {}

    @Override
    public void mouseReleased(MouseEvent arg0) {
        turnRed();
        repaint();
    }

    void hilite() {
        System.out.println("Turned yellow");
        color = Color.yellow;
        repaint();
    }

    void turnRed() {
        System.out.println("Turned Red");
        color = Color.red;
    }

    void unhilite() {
        System.out.println("Turned white");
        color = Color.white;
    }

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g2);
        g2.setColor(color);
        g2.fillPolygon(key);
        g2.drawPolygon(key);
    }
}

Upvotes: 0

Views: 159

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Don't use setBounds. The problem seems to be that the code is adding three components to a label. Add them to a panel instead.

Upvotes: 2

Related Questions