Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You failed to call <code>super.paintComponent(g)</code> inside the overridden <code>paintComponent(...)</code> method. Moreover, while overriding a method of the <code>Base</code> class, always try to keep the access specifier of the methods, the same, as much as possible. In this case it's <code>protected</code> and not <code>public</code> :-) Now function should be like this : </p> <pre><code>@Override protected void paintComponent (Graphics g) { System.err.println("paintComponent()"); super.paintComponent(g); g.fillPolygon(shape); } </code></pre> <h2><strong>EDIT 1 :</strong></h2> <p>Moreover, since you are using a custom shape to be drawn, hence you again failed to specify the <code>ContentAreaFilled</code> property for this <code>JButton</code> in question, hence inside your constructor, you should write <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setContentAreaFilled%28boolean%29" rel="nofollow">setContentAreaFilled(false)</a>, for it to work nicely. Though if this doesn't works (for reasons specified in the Docs), then you have to use the plain old <code>Opaque</code> property and set it to <code>false</code> for this <code>JButton</code> using <code>setOpaque(false)</code> :-)</p> <p>Here is your code with modified changes :</p> <pre><code>import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.Polygon; public class ShiftingButton extends JButton implements ActionListener { private Polygon shape; public ShiftingButton () { setContentAreaFilled(false); initialize(); addActionListener(this); } protected void initialize() { shape = new Polygon(); setSize(120, 120); shape.addPoint(0, 0); shape.addPoint(0, 60); shape.addPoint(90, 0); } @Override public Dimension getPreferredSize() { return (new Dimension(120, 120)); } // Hit detection public boolean contains(int x, int y) { return shape.contains(x, y); } @Override protected void paintComponent(Graphics g) { System.err.println("paintComponent()"); super.paintComponent(g); g.fillPolygon(shape); } protected void paintBorder(Graphics g) { } @Override public void actionPerformed (ActionEvent ev) { System.out.println("ShiftingButton ActionEvent!"); } public static void main (String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); ShiftingButton button = new ShiftingButton(); panel.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload