Solved

a moving object in a layeredPane

Posted on 2004-09-28
3
278 Views
Last Modified: 2011-10-03
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.BufferedImage;


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.setPreferredSize(new Dimension(300, 310));
   
         MovingCircle myCircle = new MovingCircle() ;
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
          layeredPane.add( myCircle, new Integer(0)) ;
            getContentPane().setLayout(null) ;
            getContentPane().add(layeredPane) ;
          setSize(800, 600);
          show();

    }
   
   
    //-----------
    public void actionPerformed(ActionEvent e){}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         MovingFigs meow = new MovingFigs() ;
      //   meow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         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.BufferedImage;

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(ActionEvent 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

0
Comment
Question by:meow00
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12175085
you aren't using any layout managers so you need to set the size and position of all components expicitly.
0
 
LVL 1

Author Comment

by:meow00
ID: 12176369
  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(100,100) ;
            getContentPane().setLocation(50,50) ;
            getContentPane().add(layeredPane) ;
           
0
 
LVL 92

Accepted Solution

by:
objects earned 450 total points
ID: 12176407
have you set the size and position of myCircle?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 30
egit plugin on eclipse 8 63
expectj telnet failing 5 36
use lov values 2 50
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question