Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Beginners drawing  (Read 2407 times)
0 Members and 1 Guest are viewing this topic.
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Posted 2003-01-15 01:01:31 »

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

Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #1 - Posted 2003-01-17 00:28:30 »

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.
Offline mattocks

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-02-28 05:58:25 »

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 = 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 != null)
                 sprites.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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!