CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010
    Posts
    6

    Threads, balls..

    Hi, i'm new with threads in java and really need some help right now.

    I want the program to add some balls, one after another with 1s interval...

    I know that I have to implement Runnable interface in NyPanel.java and the method run ().
    In main, do a while loop, create new threads and use Thread.sleep (1000); for the interval.

    But how is the code going to look like in method run (); ?

    My code for 1 ball:


    Main
    Code:
    public class Main
    {
        public static void main(String[] args)
        {
            NyPanel panelen = new NyPanel ();
            NyRam ramen = new NyRam ("Bollar");
    
            ramen.add(panelen, "Center");
            ramen.add(panelen.getPanelReferens(), "South");
            ramen.setVisible(true);
        }
    }



    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.Random;
    import java.util.ArrayList;
    
    public class NyPanel extends JPanel implements ActionListener
    {
        private Timer timern = new Timer (5, this);
        private double x = 0, y = 0, velX = 3, velY = 3;
        private Random farg = new Random ();
        private Color farger;
        private Ellipse2D cirkel;
        private ArrayList <Ellipse2D> bollar = new ArrayList <Ellipse2D> ();
        private JPanel panelen = new JPanel ();
        private JSlider linjalen = new JSlider (0, 80, 40);
    
        public NyPanel ()
        {
        }
    
        @Override
        public void paintComponent (Graphics gr)
        {
            super.paintComponent (gr);
            this.setBackground(Color.BLACK);
    
            Graphics2D g = (Graphics2D) gr;
    
            linjalen.setBackground(Color.BLACK);
            panelen.add(linjalen);
            panelen.setBackground(Color.BLACK);
            int linjalVarde = linjalen.getValue();
    
    
            cirkel = new Ellipse2D.Double (x, y, linjalVarde, linjalVarde);
            g.setColor (farger);
            g.fill (cirkel);
            timern.start ();
        }
    
        public void actionPerformed (ActionEvent e)
        {
            if (x < 0 || x > 755)
            {
                velX = -velX;
                skiftaFarger ();
            }
            if (y < 0 || y > 450)
            {
                velY = -velY;
                skiftaFarger ();
            }
            x += velX;
            y += velY;
    
            repaint ();
        }
    
        public void skiftaFarger ()
        {
            int rod = farg.nextInt (256);
            int gron = farg.nextInt (256);
            int bla = farg.nextInt (256);
            farger = new Color (rod, gron, bla);
        }
    
        public JPanel getPanelReferens ()
        {
            return panelen;
        }
    }

    NyRam.java, not showing that class. it is not necessary.

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Threads, balls..

    I would propose a different design.

    Use designated Objects, create eg. a Ball object, that knows what you want it to know, like location (Point), radius (int) and color. It knows how to manipulate these variables. Your timer in nyPanel instance, simply adds these objects to a different panel -object and calls repaint on that panel. That panel maintains a list of the balls and handles the painting of them using not adding components, but the methods in Graphics2D (such as fillOval(...)). All you need to do there is handling of the list, and fairly straight forward override of the paintComponent(...) method. Just remember to add the panel into your nyPanel instance.

    This model is substancially simpler, while you do not loose any future extendability, over using a ton of actual components. The drawn objects still contain necessary data for anything from mouse -based selection, dragging and scaling etc.

    Not that I know, where this experiment will lead you, but consider what simple changes you would need to do to allow adding of lines, non-filled shapes, boxes etc. What would you need to do to save and load the contents of the panel? How would that be compared to the approach using Components?

    Lastly, it is most often than not, beneficial to code in english. Most of the world do not understand swedish.

  3. #3
    Join Date
    Jul 2010
    Posts
    6

    Re: Threads, balls..

    Ok, thanks. Have to take a look at my files now

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Threads, balls..

    That panel maintains a list of the balls and handles the painting of them using not adding components, but the methods in Graphics2D (such as fillOval(...)).
    If you want to add flexibility to this approach then rather than having the panel paint the shape directly add an object that just knows how to draw a ball (or whatever shape you want to use) and use that object like a rubber stamp to draw each of the balls on the screen (ie set the objects x, y co-ordinates and call it's paint method). That way you can easily change the shape to draw just by supplying a different 'rubber stamp' object.

    Or to go a step further you could allow the registering of different 'rubber stamp' objects with different data object types so you can easily have bouncing balls, heads, fish etc all at the same time without having to recode your NyPanel class.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured