JPanel only paints on second press of JButton

Why does the GrphScreen JPanel only paint the images when I press the JButton1 a second time? The method called is jButton1MouseReleased

import java.awt.Image;

import java.awt.Toolkit;

import java.util.Random;

/*

* Main.java

*

* Created on August 16, 2006, 3:50 PM

*/

/**

*

* @author rockie12

*/

public class Main extends javax.swing.JFrame {

/** Creates new form Main */

public Main() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

jScrollPane1 = new javax.swing.JScrollPane();

MainTextArea = new javax.swing.JTextArea();

GrphScreen = new javax.swing.JPanel();

jPanel2 = new javax.swing.JPanel();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("PK Test"));

MainTextArea.setColumns(20);

MainTextArea.setRows(5);

jScrollPane1.setViewportView(MainTextArea);

GrphScreen.setBorder(new javax.swing.border.MatteBorder(null));

org.jdesktop.layout.GroupLayout GrphScreenLayout = new org.jdesktop.layout.GroupLayout(GrphScreen);

GrphScreen.setLayout(GrphScreenLayout);

GrphScreenLayout.setHorizontalGroup(

GrphScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(0, 364, Short.MAX_VALUE)

);

GrphScreenLayout.setVerticalGroup(

GrphScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(0, 100, Short.MAX_VALUE)

);

org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)

.add(GrphScreen, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()

.add(GrphScreen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 7, Short.MAX_VALUE)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))

);

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Controls"));

jButton1.setText("Fight");

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseReleased(java.awt.event.MouseEvent evt) {

jButton1MouseReleased(evt);

}

});

jButton2.setText("Walk Around");

jButton2.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseReleased(java.awt.event.MouseEvent evt) {

jButton2MouseReleased(evt);

}

});

org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);

jPanel2.setLayout(jPanel2Layout);

jPanel2Layout.setHorizontalGroup(

jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jPanel2Layout.createSequentialGroup()

.addContainerGap()

.add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jButton1)

.add(jButton2))

.addContainerGap(240, Short.MAX_VALUE))

);

jPanel2Layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

jPanel2Layout.setVerticalGroup(

jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()

.add(jButton2)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 36, Short.MAX_VALUE)

.add(jButton1)

.addContainerGap())

);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.addContainerGap()

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)

.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap(14, Short.MAX_VALUE))

);

pack();

}// </editor-fold>//GEN-END:initComponents

private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseReleased

// TODO add your handling code here:

Random randNum = new Random();

int checkNum = randNum.nextInt(12);

if(checkNum%4 == 0)

{

MainTextArea.setText("Trainer Found!!! Prepare to Battle!!!");

}

else if(checkNum < 5)

{

MainTextArea.setText("Pokemon Found!!! Prepare to Battle!!!");

}

else

{

MainTextArea.setText("No Pokemon Found!!! Try Again!!!");

// determine what pokemon you have to fight against

// next 2 lines create a variable to store the image in

Image image;

Toolkit toolkit = Toolkit.getDefaultToolkit();

// generate random number to use for which pokemon image to load

checkNum = randNum.nextInt(250) - 100;

// load the image file into the image variable

image = toolkit.getImage(checkNum + ".gif");

// draw image on the screen in the GrphScreen JPanel

GrphScreen.getGraphics().drawImage(image,10,10,null);

// generate random number to use for which pokemon image to load

checkNum = randNum.nextInt(250) - 100;

// get image for next pokemon and put it in the image variable

image = toolkit.getImage(checkNum + ".gif");

// draw image on the screen in the GrphScreen JPanel

GrphScreen.getGraphics().drawImage(image,100,10,null);

}

}//GEN-LAST:event_jButton2MouseReleased

private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased

// TODO add your handling code here:

Random randNum = new Random();

int checkNum = randNum.nextInt(255) - 100;

// next 2 lines create a variable to store the image in

Image image;

Toolkit toolkit = Toolkit.getDefaultToolkit();

// load the image file into the image variable

image = toolkit.getImage("250.gif");

// draw image on the screen in the GrphScreen JPanel

GrphScreen.getGraphics().drawImage(image,10,10,null);

// get image for next pokemon and put it in the image variable

image = toolkit.getImage("175.gif");

// draw image on the screen in the GrphScreen JPanel

GrphScreen.getGraphics().drawImage(image,100,10,null);

}//GEN-LAST:event_jButton1MouseReleased

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Main().setVisible(true);

}

});

}

// Variables declaration - do not modify//GEN-BEGIN:variables

private javax.swing.JPanel GrphScreen;

private javax.swing.JTextArea MainTextArea;

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JPanel jPanel1;

private javax.swing.JPanel jPanel2;

private javax.swing.JScrollPane jScrollPane1;

// End of variables declaration//GEN-END:variables

}

[9270 byte] By [rockie12_usa] at [2007-10-3 2:53:28]
# 1

1) Swing related questions should be posted in the Swing forum.

2) Use the "Code" formatting tags when posting code so the code is readable.

3) Use the "Preview" button when you ask a question to make sure your question is readable.

> Why does the GrphScreen JPanel only paint the images when I press the JButton1 a second time?

Don't know and your code isn't execuable so I can't see the described behavaiour. Don't use an IDE to build your GUI. The code isn't portable since I don't have access to the LayoutManager being used. Learn you build GUI using your own code. It will be easier to understand and maintain. You can still use the IDE to compile and debug the program.

> GrphScreen.getGraphics().drawImage(image,100,10,null);

That is not how you do custom painting. You should extend JComponent or JPanel. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/14painting/practice.html]Performing Custom Painting[/url]. Of course the easiest way to add an image is to use a JLabel. Read the tutorial on "How to Use Icons".

camickra at 2007-7-14 20:42:29 > top of Java-index,Java Essentials,Java Programming...