Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The likely cause of the problem is probably down to the fact that you've failed to call <code>super.paint</code>, but since you've not provided the paint code, it's only an educated guess...</p> <p>Some other educated guesses...</p> <ul> <li>You're overriding <code>paint</code> (possibly of a top level container like <code>JFrame</code>), when you should be overriding <code>paintComponent</code> of something like <code>JComponent</code> or <code>JPanel</code> (and calling <code>super.paintComponent</code>)</li> </ul> <p>Take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/painting/" rel="nofollow">Performing Custom Painting</a> and <a href="http://www.oracle.com/technetwork/java/painting-140037.html" rel="nofollow">Painting in AWT and Swing</a> for more details</p> <p><strong>Updated based on updated code</strong></p> <p>You have two basic problems...</p> <pre><code>public class Board2 extends JFrame { //... public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(50,50,20,20); } </code></pre> <p>You're overriding <code>paint</code> of top level container and you're breaking the paint chain.</p> <p>Painting in Swing is done by a series of chained method calls which work together to produce the final result, reach doing a particular job, when you fail to call <code>super.paint</code>, you stop these methods from working.</p> <p>When you do this on a top level container you can prevent it from painting it's children as well...</p> <p>Also, top level containers aren't double buffered, which makes them flicker when painted.</p> <p>Instead, create yourself a custom component, override it's <code>paintComponent</code> and do your custom painting there</p> <p>For example...</p> <pre><code>public class Board2 extends JFrame { static JPanel p1 static JButton b1 public Board2(){ setSize(1000, 800); setResizable(false); b1 = new JButton("Button"); p1 = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(50,50,20,20); } } p1.add(b1); add(p1, BorderLayout.CENTER); setVisible(true); } public static void main(String args[]){ EventQueue.invokeLater(new Runnable() { public void run() { new Board2(); } }); } } </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