• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help changing the colour of ball each time it hits the frame.

 
Harsh Parmar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.Color;
import java.awt.Graphics;


public class MainClass extends javax.swing.JFrame implements Runnable {
//integer and velocity
int width;
int height;
int posX=60;
int posY=100;
//velocity
int x=2;
int y=2;
int size=20;

Thread move = new Thread(this);

public void run() {
try {

while (true) {
posX = posX + x;
posY = posY + y;
if(posX>width-size){x=-2;}
if(posY>height-size){y=-2;}
if(posX<0){x=2;}
if(posY<0){y=2;}
repaint();
move.sleep(10);

}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void paint(Graphics g) {
//width = getWidth();
//height = getHeight();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.RED);
g.fillOval(posX , posY , size, size);
}



/**
* Creates new form MainClass
*/
public MainClass() {
initComponents();
this.setSize(300,300);
this.setResizable(false);
width = getWidth();
height = getHeight();
move.start();
}

/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainClass().setVisible(true);
}
});
}
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

LM is right; if you call Thread#sleep, you can simply put the event dispatch thread (EDT) to sleep. You should do anything with display objects on the EDT because GUIs are usually not thread safe. You can read about Swing threads here, and about the Swing Timer here in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!