how would i go about removing objects?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: how would i go about removing objects?

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    3

    how would i go about removing objects?

    what im trying to do is after box1 and box 2 intersect i want to remove all boxes and remove Graphics2D so that i can add a contentpane to the program,calling certain buttons and labels,how would i go about doing or is there a simpler way then what im thinking about?
    ________
    public class BoxIntersection extends JApplet implements Runnable, KeyListener
    {

    public void init()
    {
    getContentPane().setLayout(null);
    setSize(DISPLAY_WIDTH,DISPLAY_HEIGHT);
    setIgnoreRepaint(false);
    addKeyListener(this);
    //setBackground(Color.CYAN);

    backBuffer = new BufferedImage(DISPLAY_WIDTH,DISPLAY_HEIGHT,BufferedImage.TYPE_INT_RGB);
    bbGraphics = (Graphics2D) backBuffer.getGraphics();

    //create six boxes...
    box1 = new Box(DISPLAY_WIDTH/2 - 50,DISPLAY_HEIGHT/2 - 25,100,50);
    box2 = new Box(100,100,50,50);
    box3 = new Box(0,0,400,5);
    box4 = new Box(395,0,5,400);
    box5 = new Box(0,0,5,400);
    box6 = new Box(0,395,400,5);
    }
    public void start()
    {
    loop = new Thread(this);
    loop.start();
    }

    public void stop()
    {
    loop = null;
    }

    public void run()
    {
    long startTime,waitTime,elapsedTime;
    //1000/25 FPS = 40ms delay
    int delayTime = 1000/25;

    Thread thisThread = Thread.currentThread();
    while(loop == thisThread)
    {
    startTime = System.currentTimeMillis();

    //render to back buffer now
    render(bbGraphics);
    //render back buffer image to screen
    Graphics g = getGraphics();
    g.drawImage(backBuffer, 0, 0,null);
    g.dispose();

    //handle frame rate
    elapsedTime = System.currentTimeMillis() - startTime;
    waitTime = Math.max(delayTime - elapsedTime, 5);

    try
    {
    Thread.sleep(waitTime);
    }
    catch(InterruptedException e)
    {
    }
    }
    }

    public void render(Graphics g)
    {
    g.clearRect(0, 0,DISPLAY_WIDTH,DISPLAY_HEIGHT);
    if(box1.intersects(box2)) // change the color to white...
    {
    g.setColor(Color.white);

    }
    else
    g.setColor(Color.green);
    box1.render(g);
    box2.render(g);
    box3.render(g);
    box4.render(g);
    box5.render(g);
    box6.render(g);

    ______
    and below i have key movements that i didnt include in this post

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you want to remove components from a container you use the methods defined in the Container class (the component returned by getContentPane() is a Container descendant). Just remember to issue a validate() for a container after you have made changes to it.

    For the rendering you might want to check out the JComponent method:

    paintComponent(Graphics g).

    This allows you to do custom painting on panels/frames that also contains standard components.
    eschew obfuscation

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2005, 03:03 AM
  2. Bug with Creation of Objects in SQL *Plus Worksheet
    By Michael Cole in forum Database
    Replies: 2
    Last Post: 04-02-2003, 04:06 PM
  3. Bug with Creation of Objects in SQL *Plus Worksheet
    By Michael Cole in forum oracle.general
    Replies: 0
    Last Post: 02-20-2003, 11:25 PM
  4. Thin Objects for Better Performance
    By Dave Fleischman in forum Enterprise
    Replies: 3
    Last Post: 08-18-2000, 02:10 PM
  5. Components vs Objects
    By Oliver Lennon in forum Enterprise
    Replies: 3
    Last Post: 04-18-2000, 01:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center