5 Replies - 206 Views - Last Post: 19 June 2013 - 08:39 AM Rate Topic: -----

#1 warlordskull  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 3
  • Joined: 18-June 13

Images Flickering - how to change to bufferedImage

Posted 18 June 2013 - 07:05 AM

Trying to get rid of the flashing of the cars using c.drawimage as they move around the track (drawcars routine) in my class project due to the constant redrawing of them....but I don't understand how to convert this to buffering and still move the objects around the screen. Any help greatly appreciated.

L-car.png car moving left
U-car.png car moving up
R-car.png car moving right
D-car.png car moving down
(did this instead of rotation the image.

// The "BoardGame" class.
import java.awt.*;
import java.util.*;  // routine for making numbers more random
import hsa.Console;
import javax.swing.*;

import java.applet.*;
import java.io.*;

public class BoardGame
{
    static Console c;           // The output console

    public static void drawcars (int players, int[] xpos, int[] ypos, int[] dt)
    {
        // diplay board and cars
        //c.clear ();
        c.drawImage (new ImageIcon ("board.png").getImage (), 0, 0, null);
        for (int dc = 0 ; dc < players ; dc++)
        {
            if (dt [dc] < 6)
            {
                c.drawImage (new ImageIcon (dc + "L-car.png").getImage (), xpos [dc], ypos [dc], null);
            }
            else if (dt [dc] < 9)
            {
                c.drawImage (new ImageIcon (dc + "U-car.png").getImage (), xpos [dc], ypos [dc], null);
            }
            else if (dt [dc] < 15)
            {
                c.drawImage (new ImageIcon (dc + "R-car.png").getImage (), xpos [dc], ypos [dc], null);
            }
            else
            {
                c.drawImage (new ImageIcon (dc + "D-car.png").getImage (), xpos [dc], ypos [dc], null);
            }
        }
    }


    public static void playsnd (String filenm)
    {
        File wavFile = new File (filenm);
        AudioClip sound;
        try
        {
            sound = Applet.newAudioClip (wavFile.toURL ());
            sound.play ();
        }
        catch (Exception e)
        {
            ;
        }
    }


    public static void main (String[] args) throws Exception
    {
        c = new Console ();

        String[] nameList = new String [5];
        String name;
        String ndata = "1";
        String filenm;
        int players = 0;
        int dice;
        int findice;
        int sxpos;
        int sypos;
        int loop = 0;

        // dice total for players
        int[] dt;
        dt = new int [5];
        dt [0] = 0;
        dt [1] = 0;
        dt [2] = 0;
        dt [3] = 0;
        dt [4] = 0;

        // setup car starting x and y positions
        int[] xpos;
        xpos = new int [5];
        xpos [0] = 550;
        xpos [1] = 550;
        xpos [2] = 550;
        xpos [3] = 550;
        xpos [4] = 550;
        int[] ypos;
        ypos = new int [5];
        ypos [0] = 370;
        ypos [1] = 388;
        ypos [2] = 406;
        ypos [3] = 424;
        ypos [4] = 442;

        // setup fonts
        Font set = new Font ("Courier New", Font.BOLD, 20);
        c.setTextBackgroundColor (Color.black);
        c.setTextColor (Color.yellow);
        c.setColor (Color.red);
        c.setFont (set);

        c.clear ();

        // show game name
        c.drawImage (new ImageIcon ("Game Name.png").getImage (), 125, 355, null);
        c.drawImage (new ImageIcon ("checkered flag.jpg").getImage (), 125, 50, null);
        playsnd ("gmenstar.wav");
        Thread.sleep (4000);
        c.clear ();

        // show developer name
        c.drawImage (new ImageIcon ("Developer.png").getImage (), 100, 355, null);
        c.drawImage (new ImageIcon ("audi.png").getImage (), 20, 50, null);
        playsnd ("hothonda.wav");
        Thread.sleep (4000);
        c.clear ();

        // ask for number of players
        playsnd ("miata-hn.wav");
        while (players < 2 || players > 5)
        {
            c.println ("Please enter the number of players (PLAYERS 2-5 MAX)");
            players = c.readInt ();
            c.clear ();
        }

        // enter players names
        playsnd ("miata-hn.wav");
        for (int i = 0 ; i < players ; i++)
        {
            c.clear ();
            c.println ("Please enter Players " + (i + 1) + " name");
            nameList [i] = c.readString ();
        } //end for players names

        // display players names and numbers
        c.clear ();
        c.println ("Congradulations you have " + (players) + " players named: ");
        for (int i = 0 ; i < players ; i++)
        {
            c.println ((i + 1) + ". " + nameList [i]);
        }
        Thread.sleep (2000); //was 4000

        drawcars (players, xpos, ypos, dt);

        // set font colours for board
        c.setTextBackgroundColor (Color.black);
        c.setTextColor (Color.white);
        c.setColor (Color.white);
        c.setFont (set);


        for (int eog = 0 ; eog < 19 ; eog++) // loop for the end of game (EOG) (only 18 pos if rolled 1 everytime)
        {
            for (int pt = 0 ; pt < players ; pt++)  // loop for each players turn (PT)
            {
                int x = xpos [0];
                c.drawString ("Player " + (pt + 1) + " turn...hit a key", 170, 170);
                playsnd ("miata-hn.wav");
                char a;
                a = c.getChar (); //allows you to press ket without pressing enter


                drawcars (players, xpos, ypos, dt);

                c.drawString ("Player " + (pt + 1) + ": " + nameList [pt] + " Rolls", 170, 170);

                // flash a few randm dice to make look like dice rolling
                for (int r = 0 ; r < 8 ; r++)
                {
                    dice = (int) (Math.random () * 6) + 1;
                    c.drawImage (new ImageIcon (dice + "-dice.png").getImage (), 250, 200, null); // show the dice
                playsnd ("dice.wav");

                    Thread.sleep (30 * r);
                }
                findice = (int) (Math.random () * 6) + 1; // finds random number for dice
                c.drawImage (new ImageIcon (findice + "-dice.png").getImage (), 250, 200, null); // show the dice
                Thread.sleep (1000);
                playsnd ("wavviper.wav");
                Thread.sleep (2000);


                if (a != '!')
                {


                    for (int mc = 1 ; mc <= findice ; mc++) // loops thru for each dice
                    {

                        sxpos = xpos [pt];  // starting x position
                        sypos = ypos [pt];  // starting y position

                        // move car to left
                        if (dt [pt] < 6)
                        {
                            for (int l = sxpos ; l >= sxpos - (90) ; l = l - 2) //this makes the car move
                            {
                                xpos [pt] = l;  //update players cars position

                                try
                                {
                                    drawcars (players, xpos, ypos, dt); // call code to update the screen
                                    
                                    Thread.sleep (5);
                                }
                                catch (InterruptedException e)
                                {
                                    ;
                                }
                            }
                        } // end of move left

                        //move car to up
                        else if (dt [pt] < 9)
                        {
                            for (int u = sypos ; u >= sypos - (125) ; u = u - 2) //this makes the car move
                            {
                                ypos [pt] = u;  //update players cars position
                                try
                                {
                                    drawcars (players, xpos, ypos, dt); // call code to update the screen

                                    Thread.sleep (5);
                                }
                                catch (InterruptedException e)
                                {
                                    ;
                                }
                            }
                        } // end of move up

                        //move car to right
                        else if (dt [pt] < 15)
                        {
                            for (int r = sxpos ; r <= sxpos + (90) ; r = r + 2) //this makes the car move
                            {
                                xpos [pt] = r;  //update players cars position
                                try
                                {
                                    drawcars (players, xpos, ypos, dt); // call code to update the screen

                                    Thread.sleep (5);
                                }
                                catch (InterruptedException e)
                                {
                                    ;
                                }
                            }
                        } // end of move right

                        //move car to down
                        else
                        {
                            for (int u = sypos ; u <= sypos + (120) ; u = u + 2) //this makes the car move
                            {
                                ypos [pt] = u;  //update players cars position
                                try
                                {
                                    drawcars (players, xpos, ypos, dt); // call code to update the screen

                                    Thread.sleep (5);
                                }
                                catch (InterruptedException e)
                                {
                                    ;
                                }
                            }
                        } // end of move down

                        dt [pt] = dt [pt] + 1; // update total dice roll for player
                        if (dt [pt] == 18)  // winnder
                        {
                            c.clear ();
                            c.drawImage (new ImageIcon ("checkered flag.png").getImage (), 125, 50, null);
                            c.drawString ("Player " + (pt + 1) + ": " + nameList [pt] + " is the WINNER!", 120, 400);
                            playsnd ("fanfare.wav");
                            Thread.sleep (4000);
                            System.exit (0);
                        }
                    } // end of loop for dice


                }
            } // pt end of loop
        } // eog end of loop



    } // main method
} // BoardGame class





Is This A Good Question/Topic? 0
  • +

Replies To: Images Flickering - how to change to bufferedImage

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: Images Flickering - how to change to bufferedImage

Posted 18 June 2013 - 07:26 AM

It's difficult to comment in detail as

a. what's being drawn upon is not a JDK class (why?)
b. your code is all but unreadable since your main method (which should normally only be a few lines to start the app) is massive

You should almost always preload images before using them in painting operations
Was This Post Helpful? 1
  • +
  • -

#3 warlordskull  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 3
  • Joined: 18-June 13

Re: Images Flickering - how to change to bufferedImage

Posted 19 June 2013 - 07:54 AM

If you don't know the answer then SHUT UP! I'm a high school student LEARNING JAVA and we are forced to use ready to learn java....I know little about how or where to do stuff but my code does work. I am looking for HELP not your two cents...so please keep your trap shut if your not willing to help. Thanks

PS - Why is there ALWAYS an @33ho1e like you in these forums.,,,get a life!!!


View Postg00se, on 18 June 2013 - 07:26 AM, said:

It's difficult to comment in detail as

a. what's being drawn upon is not a JDK class (why?)
b. your code is all but unreadable since your main method (which should normally only be a few lines to start the app) is massive

You should almost always preload images before using them in painting operations

Was This Post Helpful? -6
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: Images Flickering - how to change to bufferedImage

Posted 19 June 2013 - 08:14 AM

Riiiiiiiiiiight.. Look sunshine, g00se was asking for clarification on your giant pile of code. You neglected to state any of your history a head of time so that's all on you.

If you cannot place nice with folks *trying* to help I'll just lock the thread. Got it? Flying off the handle for a comment you misread just makes you the bigger asshole.

If you have a questions or concerns about appropriate decorum you are certainly welcome to shoot me a pm.
Was This Post Helpful? 0
  • +
  • -

#5 warlordskull  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 3
  • Joined: 18-June 13

Re: Images Flickering - how to change to bufferedImage

Posted 19 June 2013 - 08:27 AM

I never asked HIM to FIX my code but HOW to do something....is doing java so difficult you can show a few lines of CODE, an example or a LINK? I don't understand GEEK or have a reference book to what he is talking about. I did say its a PROJECT which I guess I thought anyone with a brain would know I meant SCHOOL after all the code doesn't look like a WORK project. I'm TRYING to LEARNNNNNNNN! His comment was NOT HELPFUL or INFORMATIVE that is what this forum is about is it NOT??? If not DELETE, BLOCK or DO WHATEVER empowers you Mr. Modi. Anyways seems there is a PILE of arrogant assholes without a life here so I will go elsewhere to get some FRIENDLY HELPFUL INFORMATION. Cheers ahole!


View Postmodi123_1, on 19 June 2013 - 08:14 AM, said:

Riiiiiiiiiiight.. Look g00se was asking for clarification on your giant pile of code. You neglected to state any of your history a head of time so that's all on you.

If you cannot place nice with folks *trying* to help I'll just lock the thread. Got it? Flying off the handle for a comment you misread just makes you the bigger asshole.

If you have a questions or concerns about appropriate decorum you are certainly welcome to shoot me a pm.

Was This Post Helpful? -4
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: Images Flickering - how to change to bufferedImage

Posted 19 June 2013 - 08:39 AM

Wow.. I haven't witness a temper tantrum like this in a while over someone asking for clarification *AND* he gave you advice.

Again - he was asking for clarification so he can proceed to help you. Shocking.. I know.

I work on projects all day.. for a big boy paycheck. It's amazing that a word has different connotations.


Quote

His comment was NOT HELPFUL or INFORMATIVE that is what this forum is about is it NOT??

Again - he was asking for more information from you to help you.

Here's the deal. This thread is going no where. If you would like to start a new one with more clear information on what is going on that would be grand. Remember - we are not there with you so the more information you can provide us in turns makes our job easier to help you.

As it stands I am in a chipper mood thanks to the CWS and warn you if you keep up this sort of indignant verbal tirade you will no longer be welcomed here.

Got an issue - shoot me a pm.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1