meow00
asked on
a moving object in a layeredPane
Hi experts,
I tried to put a moving object in a layeredPane which is in a Frame. It works fine if i add it to the code directly, but it doesn't work if i put it in a layeredPane first. Does anyone know what I did wrong ???
please help ! many thanks.
-------------------------- ------
// MovingFigs.java
import java.lang.* ;
import javax.swing.*;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.BufferedIma ge;
public class MovingFigs extends JFrame implements ActionListener{
private JLayeredPane layeredPane;
private JPanel myPanel ;
/** Creates a new instance of Main */
public MovingFigs() {
layeredPane = new JLayeredPane();
layeredPane.setPreferredSi ze(new Dimension(300, 310));
MovingCircle myCircle = new MovingCircle() ;
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
layeredPane.add( myCircle, new Integer(0)) ;
getContentPane().setLayout (null) ;
getContentPane().add(layer edPane) ;
setSize(800, 600);
show();
}
//-----------
public void actionPerformed(ActionEven t e){}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MovingFigs meow = new MovingFigs() ;
// meow.setDefaultCloseOperat ion(JFrame .EXIT_ON_C LOSE);
meow.setVisible(true);
}
}
//------------------------ ---
// MovingCircle.java
import java.lang.* ;
import javax.swing.*;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.BufferedIma ge;
public class MovingCircle extends JPanel implements ActionListener{
private int x = 30, y = 30 ;
private final int DELTA_X = 10 ;
/** Creates a new instance of Class */
public MovingCircle() {
javax.swing.Timer circleTimer = new Timer(250, this);
circleTimer.start();}
public void actionPerformed(ActionEven t e){ repaint() ;}
//--------------------
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setPaint(Color.red);
x += DELTA_X;
g2d.fillOval(x, y, y, y);
g2d.drawOval(x, y, y, y);
g2d.dispose();
} // paintComponent
} // MovingCircle
I tried to put a moving object in a layeredPane which is in a Frame. It works fine if i add it to the code directly, but it doesn't work if i put it in a layeredPane first. Does anyone know what I did wrong ???
please help ! many thanks.
--------------------------
// MovingFigs.java
import java.lang.* ;
import javax.swing.*;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.BufferedIma
public class MovingFigs extends JFrame implements ActionListener{
private JLayeredPane layeredPane;
private JPanel myPanel ;
/** Creates a new instance of Main */
public MovingFigs() {
layeredPane = new JLayeredPane();
layeredPane.setPreferredSi
MovingCircle myCircle = new MovingCircle() ;
setDefaultCloseOperation(J
layeredPane.add( myCircle, new Integer(0)) ;
getContentPane().setLayout
getContentPane().add(layer
setSize(800, 600);
show();
}
//-----------
public void actionPerformed(ActionEven
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MovingFigs meow = new MovingFigs() ;
// meow.setDefaultCloseOperat
meow.setVisible(true);
}
}
//------------------------
// MovingCircle.java
import java.lang.* ;
import javax.swing.*;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.BufferedIma
public class MovingCircle extends JPanel implements ActionListener{
private int x = 30, y = 30 ;
private final int DELTA_X = 10 ;
/** Creates a new instance of Class */
public MovingCircle() {
javax.swing.Timer circleTimer = new Timer(250, this);
circleTimer.start();}
public void actionPerformed(ActionEven
//--------------------
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setPaint(Color.red);
x += DELTA_X;
g2d.fillOval(x, y, y, y);
g2d.drawOval(x, y, y, y);
g2d.dispose();
} // paintComponent
} // MovingCircle
you aren't using any layout managers so you need to set the size and position of all components expicitly.
ASKER
Hi objects & experts,
Now I set the size and position for all components, but it still doesn't work ..... did i miss anything ??? please help ! many many thanks !!!
layeredPane.add( myCircle, new Integer(0)) ;
layeredPane.setSize(20,20) ;
layeredPane.setLocation(30 ,30) ;
getContentPane().setSize(1 00,100) ;
getContentPane().setLocati on(50,50) ;
getContentPane().add(layer edPane) ;
Now I set the size and position for all components, but it still doesn't work ..... did i miss anything ??? please help ! many many thanks !!!
layeredPane.add( myCircle, new Integer(0)) ;
layeredPane.setSize(20,20)
layeredPane.setLocation(30
getContentPane().setSize(1
getContentPane().setLocati
getContentPane().add(layer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.