Beginners drawing

This is a small (working) java application that draws sprites - multi colored ovals which all move.

you can add about 50 sprites before any noticable performance drop on my machine (Dell Inspirion, 1 ghz)

you can add about 500 before it starts throwing errors.

This could be extended to draw tilemaps with sprites on top. It’s basically a model of the graphics management i am using in my game (though the sprites and backgrounds use images and dont float around in space.)

I thought this could ba a basic system which could be extended for many diferent purposes, but i know it’s not perfect.

If anyone has any alteration suggestions, they’d be appreciated.

In particular, the swing timer. i’ve been finding the sleeptimer posted here confusing, but that’s what i’d like to implement in my game.

it’s available here:
http://members.austarmetro.com.au/~juddman/files/java/drawingBasics.zip

OHHHH kay…

after implementing this in a game i’m making i discovered it’s a disaster. Don’t use this for anything other than ammusing little applets. you get better performance from Swing.

Hi juddman,

Nice code examples - thank you!!!

I hacked on your DrawingBasics source to include a start/stop animation capability, as per Sun’s timer examples:

/*

  • @(#)DrawingLessons.java 1.0 03/01/11
  • You can modify the template of this file in the
  • directory …\JCreator\Templates\Template_1\Project_Name.java
  • You can also create your own project template by making a new
  • folder in the directory …\JCreator\Template. Use the other
  • templates as examples.

/
import java.awt.
;
import java.awt.event.;
import javax.swing.
;
import javax.swing.event.*;

class DrawingBasics extends JFrame
{
BubbleSprite[] sprites = new BubbleSprite[1000];

  JmGraphicsPanel gpane = new JmGraphicsPanel();
  int curFill = 0;
  JPanel cp;
  JButton addSpr = new JButton("Add sprite");
  JButton startButton = new JButton("Start");
  JButton stopButton  = new JButton("Stop");

  Timer timer;
  boolean frozen = true;      // Animation frozen?

  ActionListener timedAction = new ActionListener()
  {
        public void actionPerformed(ActionEvent e)
        {
              go();
        }
  };

//--------------------------------------------------------------------//

  // Constructor
  public DrawingBasics(int fps, String windowTitle)
  {
        super(windowTitle);

        // Compute frame delay
        int delay = (fps > 0) ? (1000 / fps) : 100;      // Milliseconds

        // Set up a timer that calls this object's action handler.
        timer = new Timer(delay, timedAction);
        timer.setInitialDelay(0);      // Start up immediately
        timer.setCoalesce(true);      // Prevent event flooding

        for (int i = 0; i < sprites.length; i++)
              sprites[i] = null;

        // Sprite #1
        BubbleSprite tmp = new BubbleSprite( 80, 100, 30, 70);
        addSprite(tmp);
        
        // Sprite #2
         tmp = new BubbleSprite( 40, 40, 80, 50);
        tmp.setFillColor (Color.green);
        tmp.setLineColor(Color.yellow);
        addSprite(tmp);

        // Sprite #3
         tmp = new BubbleSprite( 45, 45, Math.random()*100, Math.random()*100);
        tmp.setFillColor (Color.orange);
        tmp.setLineColor(Color.magenta);
        addSprite(tmp);
        
        // Create user interface
        cp = (JPanel)getContentPane();
        cp.setLayout(new BorderLayout());
        
        cp.add(gpane, BorderLayout.CENTER);

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout( new GridLayout(1,3) ); // GridLayout (rows, cols, dx, dy)
        controlPanel.add(addSpr);
        controlPanel.add(startButton);
        controlPanel.add(stopButton);

        cp.add(controlPanel, BorderLayout.SOUTH);
        
        gpane.setGraphicArray(sprites);
        
        addSpr.addActionListener
        (
              new ActionListener()
              {
                    public void actionPerformed(ActionEvent e)
                    {
                          addRandomSprite();
                    }
              }
        );

        startButton.addActionListener
        (
              new ActionListener()
              {
                    public void actionPerformed(ActionEvent e)
                    {
                          if (frozen)
                          {
                                frozen = false;
                                startButton.setEnabled(false);
                                stopButton.setEnabled(true);
                                startAnimation();
                          }
                    }
              }
        );

        stopButton.addActionListener
        (
              new ActionListener()
              {
                    public void actionPerformed(ActionEvent e)
                    {
                          if (! frozen)
                          {
                                stopAnimation();
                                frozen = true;
                                startButton.setEnabled(true);
                                stopButton.setEnabled(false);
                          }
                    }
              }
        );

// timer.start(); // Autostart (disabled)

        // Window closing listener
        addWindowListener
        (
              new WindowAdapter()
              {
                    public void windowDeiconified(WindowEvent e)
                    {
                          if (frozen)
                          {
                                frozen = false;
                                startButton.setEnabled(false);
                                stopButton.setEnabled(true);
                                startAnimation();
                          }
                    }
                    public void windowIconified(WindowEvent e)
                    {
                          if (! frozen)
                          {
                                stopAnimation();
                                frozen = true;
                                startButton.setEnabled(true);
                                stopButton.setEnabled(false);
                          }
                    }
                    public void windowClosing(WindowEvent e)
                    {
                          stopAnimation();
                          dispose();
                          System.exit(0);
                    }  
              }
        );

  }

//--------------------------------------------------------------------//

  public void go()
  {
        for (int i=0; i<sprites.length; i++)
        {
              if (sprites[i] != null)
              sprites[i].randomMotion();
              else break;
        }
        //System.out.print(".");
        gpane.repaint();
  }

//--------------------------------------------------------------------//

  // Can be invoked by any thread (since timer is thread-safe).
  public synchronized void startAnimation()
  {
        if (frozen)
        {
              // Do nothing.  The user has requested
              // that we stop changing the image.
        }
        else
        {
              // Start animating!
              if ( !timer.isRunning() )
              {
                    timer.start();
              }
        }
  }

//--------------------------------------------------------------------//

  // Can be invoked by any thread (since timer is thread-safe).
  public synchronized void stopAnimation()
  {
        // Stop the animating thread.
        if ( timer.isRunning() )
        {
              timer.stop();
        }
  }

//--------------------------------------------------------------------//

  public void addSprite(BubbleSprite in)
  {
        sprites[curFill] = in;
        curFill ++;
  }

//--------------------------------------------------------------------//

  public void addRandomSprite()
  {
         BubbleSprite tmp = new BubbleSprite( 45, 45, Math.random()*100, Math.random()*100);
         int col1 = (int)(Math.random()*255);
         int col2 = (int)(Math.random()*255);
         int col3 = (int)(Math.random()*255);
         System.out.print ("" + col1 + col2 + col3);
        tmp.setFillColor (new Color(col1, col2, col3));
        tmp.setLineColor(Color.magenta);
        addSprite(tmp);
  }

//--------------------------------------------------------------------//

  public static void main(String args[])
  {
        System.out.println("Starting DrawingBasics...");

        int fps = 10;      // 1000 ms = 1 sec so we expect 10 fps.

        // Get frames per second from the command line argument.
        if (args.length > 0)
        {
              try
              {
                    fps = Integer.parseInt(args[0]);
              }
              catch (Exception e) {}
        }

        DrawingBasics mainFrame = new DrawingBasics(fps, "Sprite Animator");
        mainFrame.setSize(400, 400);
        mainFrame.setTitle("DrawingBasics");
        mainFrame.setVisible(true);
  }

}

  • Craig