Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is nothing painted when I call repaint()?
    primarykey
    data
    text
    <p>First of all, I am a beginner. I'm trying to make a puzzle game using an array of <code>Piece</code>s. Each <code>Piece</code> represents a number from 1 to 9. I am trying to paint using <code>paintComponent(Graphics g)</code>, but when I call the <code>repaint()</code> method, nothing happens. There is no error, so there must be some point I am not aware of.</p> <p>I'm using NetBeans. I created a new desktop application and then added a <code>JPanel</code> and a <code>JButton</code>.</p> <p>This is my code:</p> <pre><code>public class PuzzleGame2View extends FrameView { public Piece pieces[][]; Drawing outer = new Drawing(); public PuzzleGame2View(SingleFrameApplication app) { super(app); initComponents(); //more code that netbeans automatically wrote...... public class Drawing extends JFrame implements MouseListener{ public void paintComponent(Graphics g ){ g = jPanel1.getGraphics(); super.paintComponents(g); for (int i = 0; i &lt; pieces.length; i++) { for (int j = 0; j &lt; pieces.length; j++) { if (pieces[i][j].getText()!=null) { g.setColor(Color.red); g.fillRect(i*100, j*100, 100, 100); g.setColor(Color.BLACK); g.drawString(pieces[i][j].getText(), i*100 + 50, j*100 + 20); } } } } public void makePieces(){ pieces = new Piece[3][3]; for (int i = 0; i &lt; pieces.length; i++) { for (int j = 0; j &lt; pieces.length; j++) { if (i == 2 &amp;&amp; j == 2){ pieces[i][j] = new Piece(j, j, null); } else pieces[i][j] = new Piece(j, j, "" + (i*3+j+1) ); } } } </code></pre> <p>I am trying to call the <code>repaint()</code> method when I click the button.</p> <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: makePieces(); outer.repaint(); } </code></pre> <p>Here's the class <code>Piece</code>:</p> <pre><code>package puzzlegame2; public class Piece { private int row,count; private String text; public Piece(int row, int count, String text) { this.row = row; this.count = count; this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } } </code></pre> <p>It's just the first step; there are many things to do. But I can't go on until I totally understand how <code>public void paintComponent(Graphics g)</code> and <code>repaint()</code> work.</p> <p>So, please, any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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