New to java, need a applet loop

Hello, I am trying to create a simple double buffering tile drawing applet based on a two dimensional map array. I have it working fine, however I would like to create a main loop which will keep calling pain() until the escape key is pressed. Can someone please show me how I can do this? Would I need to call repaint() in paint() until escape is pressed? Here is my code.
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
public class Tile_engine extends JApplet {
  private final int TILE_MAX = 4;
  private final int TILE_SIZE = 32;
  private final int APPLET_WIDTH = 640;
  private final int APPLET_HEIGHT = 480;
  private final int SKY = 0;
  private final int CLOUD_1 = 1;
  private final int CLOUD_2 = 2;
  private final int RED_BRICKS = 3;
  private final int MAP_ROWS = 15;
  private final int MAP_COLS = 20;
  private Image buffer = null;
  private Graphics bufferg;
  private Image[] tile = new Image[TILE_MAX];
  private final int map[][] =
    {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,0,0,0,0},
     {0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,3,3,0,0,3,0,0,0,0,0,0,0,0,0},
     {3,3,3,3,3,3,3,3,0,0,3,3,3,3,3,3,3,3,3,3},
     {3,3,3,3,3,3,3,3,0,0,3,3,3,3,3,3,3,3,3,3}};
  public void init( ) {
    tile[SKY] = getImage(getDocumentBase(), "sky.png");
    tile[CLOUD_1] = getImage(getDocumentBase(), "cloud_1.png");
    tile[CLOUD_2] = getImage(getDocumentBase(), "cloud_2.png");
    tile[RED_BRICKS] = getImage(getDocumentBase(), "red_bricks.png");
    buffer = createImage(APPLET_WIDTH, APPLET_HEIGHT);
    bufferg = buffer.getGraphics();
  public void paint(Graphics g) {
    if(buffer != null) {
      int tile_id;
      for(int y=0;y<MAP_ROWS;y++) {
        for(int x=0;x<MAP_COLS;x++) {
          tile_id = map[y][x];
          bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
    g.drawImage(buffer, 0, 0, this);
}

Isn't %d an integer? You are passing a double. You need to use the correct formatting type for a double.
When posting code, please use code tags (see button above posting box). It makes the code much easier to read.

Similar Messages

  • Very New to Jave, need to do totals/subtotals

    Hi, (I hope this is the right place)
    I'm VERY new to Java (I mainly do wourk with php interfaces for MyAQL databases).
    Up until now, there hasn't been anything I couldn't do with PHP that I needed to...
    My boss recently requested subtotals/totals at various points on an input sheet. The sheet inputs numbers of people, so I'm not worried about rounding (The php already takes care of people trying to input parts of people....) or tax.
    It's a rather large form (~144 lines) and the math is already in place to do the subtotaling (among other things) before entering the information into the database.
    How would I go about writing script that would display sub totals, and recalculate them every time one of the boxes value's is modified?
    Is this something javascript is designed to do? Is there a better language out there for this task?
    I've found sites out there that have canned subtotal/total scripts, but I don't want something pre-made unless it comes with a detailed explaination of what everything does; if I'm going to maintain it, I have to understand it.
    Thanks

    Is this a web form? e.g. a html form with input fields?
    You can add onchange or onblur events to your input fields to detect changes. Then you can use javascript to retrieve the values from the form and do the calculation.
    -S

  • Hi, i'm new to java. need help setting the path in win XP

    hi all,
    i'm new to java technology. i've just downloaded the JDK and ran my first java program (hello world). i love it. java's gr8. i need help. i run win XP and how can i setup the path sothat i can execute my programs from the root dir??? any help in this direction will be greatly appreciated. please email me @ [email protected]
    Best regards
    Mrinal

    Go to Start menu and select Control Panel. In the Control Panel, double click on System. In the System dialogue, choose the Advanced tab. Then click on Environmental Variables. Select Path and Edit. Put ;c:\j2sdk1.4.0\bin at the end of the Path (or c:\j2sdk1.4.0\bin;) at the start of the Path. That's it.

  • New to java(need help on access specifier)

    hi! i am new to java.plzzzzz help me i have to make a project on access specifier's i know all theroy.but
    i am unable to understand how i can define all specifiers practicly.i mean in a program.
    thanks.plzzzzzzzz help me

    the most common project i can think of is a payroll system..
    you can have real implementation of all the access specifiers
    good luck

  • New to java needs advice

    Hi,
    I'm completely new to java and after a few days search found Studio Creator and decided to use it. Happily developing for a week in JSC, I reached the deployment to Tomcat stage.
    Mybe I'm missing something but I find this stage ruining the ease-of-use feeling which I got when I started using JSC.
    Am I right that JSC doesn't have a built-in functionality to deploy to Tomcat whithout my intervention, considering my total unfamiliarity with xml, jsp, mysql, etc.?
    If so do you guys know of another easy-to-use IDE like JSC?
    Any pointers are greatly appreciated.
    Thanks, Abraham

    Hi,
    Try this FAQ on
    How do I deploy web applications developed with Java Studio Creator to the Tomcat Servlet/JSP Container?
    at http://developers.sun.com/prodtech/javatools/jscreator/reference/faqs/technical.jsp
    MJ

  • New to Java need to parse a page on HTTPS server

    Greetings:
    I am writing a "widget" on Macintosh that goes out and gets a simple piece of information from a page within a secure sever (HTTPS).
    The big picture is: I need to send login information which is stored in a JavaScript form to a java application that would go to the https site and login and then go to a page and parse it for a number. This has been done in Perl originally but the Perl requires a specific library added to your system. My java application needs to pass the number back to JavaScript and hence a number will show up in my widget's window. There will be no browser involved... the widgets are JavaScript/XML based.
    I would need a good example of code to do this as I am VERY new. If anyone can help please let me know.

    http://onesearch.sun.com/search/developers/index.jsp?charset=UTF-8&qt=%2Bparse+%2Bhttps&col=javadoc&col=devforums&col=javatecharticles&col=javatutorials&col=devarchive&col=javasc&col=devall

  • New to java-need help for debugging

    hey there
    i just wanted my first program in java and here it is below with the error when i'm compiling it, i cant understand whats wrong:
    public class SquareArea {
    public static void main(String[] args); {
    float length;
    float width;
    float SquareArea;
    System.out.println ("Please enter length"+ length);
    System.out.println ("Please enter width"+ width);
    SquareArea= length*width;
    System.out.println ("The area of the square is" + SquareArea);
    The error is:C:\Documents and Settings\User\Desktop\SquareArea.java:5: missing method body, or declare abstract
    public static void main(String[] args); {
    ^
    1 error
    Tool completed with exit code 1

    Reading inputs from the command line is a bit complicated in java, here is how it is done:
    first add the line:
    import java.io.*;to the beginning of your program.
              float length;
              float width;
              float SquareArea;
              BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
              try
                   System.out.println ("Please enter length:");
                   String lengthStr = reader.readLine();
                   length = Float.parseFloat(lengthStr);
                   System.out.println ("Please enter width");
                   String widthStr = reader.readLine();
                   width = Float.parseFloat(widthStr);
                   SquareArea= length*width;
                   System.out.println ("The area of the square is" + SquareArea);
              catch (IOException ioe)
                   System.out.println("Error reading values.");
                   ioe.printStackTrace();
              }

  • New to Java, need assistance

    First, I'd like to apologize in case this post is in the wrong place.
    Now I'm new to Jcreator and I missed about a month from my computer programming class in school, and am far behind. My assignment right now is this problem p5.12 from Computing Concepts with Java Essentials it says:
    "A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for ex.,1980). However, since the introduction of the Gregorian calender on October 15, 1582, a year is not a leap year if it is divisible by 100 (for ex., 1900); however, it is a leap year if it is divisible by 400 (for ex., 2000). Write a program that asks the user for a year and computes whether that year is a leap year., Implement a class *Year* with the method *boolean isLeapYear*
    This is the code that I came up with:
    import javax.swing.JOptionPane;
    public class LeapYear {
         public static void main(String[] args) {
              String input = JOptionPane.showInputDialog("Enter Year");
              double year = Double.parseDouble(input);
              LeapYear newYear = new LeapYear(year);
              System.out.println(newYear.boolean isLeapYear()); //line 11
    public class LeapYear {
         private double year;     
         public LeapYear(double year)
                   a = year;
              public boolean isLeapYear(){
                   if(a % 4 == 0)
                             if(a % 100 != 0) System.out.println("Leap Year");
                   else if (a % 400 == 0) System.out.println("Leap Year");
                   else System.out.println("Not Leap Year");
    The error I am receiving is on the 11th line of the test class. I know if there is something wrong besides that. I'm not asking for someone to fix this for me but I would like if somebody could give me advice on what to do, hints, explain where I went wrong, or know of any good tutorials I could read that could help me with this it would be much appreciated.

    Year           Is Leab Year?
    1           no      
    4            yes
    1580            yes
    1582           no      
    1584            yes      
    1600            yes      
    1700            no      
    1800            no      
    1900            no      
    1996            yes      
    1997           no      
    1999            no      
    2000            yes      
    2100            no      
    2200            no      
    2300            no      
    2400            yes      
    2800            yes      
    2900            no      
    3200            yes      
    3300            no      
    3600            yes      
    3800            no      
    4000            yes      
    4200            no      
    4400            yes      
    4700            no      
    4800            yes      
    5100            no      
    5200            yes      
    6400            yes      
    6500            no      
    6800            yes      
    6900            no      
    7200            yes      
    7400            no      
    7600            yes      
    7800            no           

  • New to Java -- Need to know if something is possible

    We have an application in which some complex calculations are being done in MatLab. In order to speed things up, we are converting it to C/C++. But there is also a GUI in the MatLab code. This is all to run on Windows.
    We will eventually (2 - 4 months from now, after it has been successfully converted and running) need to also port it to Linux. I am thinking that I would like to write the GUI portion in Java, so that it can be easily transferred from Windows to Linux.
    There is also an "embedded" portion running in C/C++. This must remain in C++ in because it is a real-time application where literally each microsecond will matter, Java will be too slow. In addition, some of the calculations will run on a parallel processing board, and the COTS libaries that support running on the card are C/C++ libraries.
    So, what I want is, to make a Java GUI that I can move from Windows to Linux, and in this GUI, each of the callbacks will then do nothing but call a C++ routine. I need the C++ routine to run completely independent of the Java Virtual Machine, to preserve the speed of the C++ library.
    My questions:
    If I write the GUI in Java, and have separate libraries/fucntions in C/C++ that are compiled for both Windows and Linux, can the Java GUI call these, and will they run as fast as they would in a normal C++ app? Or will it be slowed down by the JVM, and run at "interpreter speed"?
    How do you call the C++ code from inside a Java interpreter? Do we compile it all in a dll, and the dll is called from Java? Do we make a C++ library, and that can be called from the Java GUI? Or is there something else. This must have been done many times, does anybody have any suggestions or experience to get me going. I think I can glom together a simple Java GUI ( I produced some Java programs several years ago), and I can get the C++ libraries going, it is connecting them that I could use some guidance.
    Thanks.
    - Martin

    georgemc wrote:
    DrLaszloJamf wrote:
    georgemc wrote:
    DrLaszloJamf wrote:
    georgemc wrote:
    Ignore the myth that Java executes slowly because it's "interpreted". It's notHear, hear. Brian Goetz is a good source of Java myth debunking. Check out his columns:
    http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=practice:
    Indeedie. Here's his article I like to trot out on occasions such as this
    http://java.sun.com/developer/technicalArticles/Interviews/goetz_qa.html
    I love to point out on some benchmarks, Java can be faster that C. "Faster than C? That's impossible!", people sputter, as if C were a physical constant, like the speed of light -- that's "c", folks!That's borne out of the misconception that any operation in Java is simply translated by the runtime into a similar operation in native code, and, were that the case, they'd probably have a point. It completely ignores runtime optimizationsAnd other things like Java's object allocation/GC being superior to malloc(), ...

  • New to Java-Need book that explains OOP concepts

    Hi-
    This as a great forum!
    I have been programming in VB for a while and now I'm trying to learn Java. Can someone please tell me which book is best for learning concepts like objects, classes, visibility etc; Most of the books I have seen go too fast or do not cover enough detail. I would like to find something that has lots of examples (diagrams are nice) and covers OOP thoroughly.
    Thanks!
    Ken

    The "Head First" books might be a good, gentle intro:
    http://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678/ref=pd_bbs_sr_1/104-1237750-7651950?ie=UTF8&s=books&qid=1184865003&sr=8-1
    %

  • New to Java and need help with this program..please!

    I'd really appreciate any helpful comments about this program assignment that I have to turn in a week from Friday. I'm taking a class one night a week and completely new to Java. I'd ask my professor for help, but we can't call him during the week and he never answers e-mails. He didn't tell us how to call from other classes yet, and I just can't get the darn thing to do what I want it to do!
    The assignment requirements are:
    1. Change a card game application that draws two cards
    and the higher card wins, to a Blackjack application
    2. Include a new class called Hand
    3. The Hand class should record the number of draws
    4. The application should prompt for a number of draws
    5. The game is played against the Dealer
    6. The dealer always draws a card if the dealer's hand total is <= 17
    7. Prompt the player after each hand if he wants to quit
    8. Display the total games won by the dealer and total and the total games wond by the player after each hand
    9. Display all of the dealer's and player's cards at the
    end of each hand
    10. Player has the option of drawing an additional card after the first two cards
    11. The Ace can have a value of 11 or 1
    (Even though it's not called for in the requirements, I would like to be able to let the Ace have a value of 1 or an 11)
    The following is my code with some comments about a few things that are driving me nuts:
    import java.util.*;
    import javax.swing.*;
    import java.text.*;
    public class CardDeck
    public CardDeck()
    deck = new Card[52];
    fill();
    shuffle();
    public void fill()
    int i;
    int j;
    for (i = 1; i <= 13; i++)
    for (j = 1; j <= 4; j++)
    deck[4 * (i - 1) + j - 1] = new Card(i, j);
    cards = 52;
    public void shuffle()
    int next;
    for (next = 0; next < cards - 1; next++)
    int rand = (int)(Math.random()*(next+1));
    Card temp = deck[next];
    deck[next] = deck[rand];
    deck[rand] = temp;
    public final Card draw()
    if (cards == 0)
    return null;
    cards--;
    return deck[cards];
    public int changeValue()
    int val = 0;
    boolean ace = false;
    int cds;
    for (int i = 0; i < cards; i++)
    if (cardValue > 10)
    cardValue = 10;
    if (cardValue ==1)     {
    ace = true;
    val = val + cardValue;
    if ( ace = true && val + 10 <= 21 )
    val = val + 10;
    return val;
    public static void main(String[] args)
    CardDeck d = new CardDeck();
    int x = 3;
    int i;
    int wins = 1;
    int playerTotal = 1;
    do {
    Card dealer = (d.draw());
    /**I've tried everything I can think of to call the ChangeValue() method after I draw the card, but nothing is working for me.**/
    System.out.println("Dealer draws: " + dealer);
    do {
    dealer = (d.draw());
    System.out.println(" " + dealer);
    }while (dealer.rank() <= 17);
    Card mine = d.draw();
    System.out.println("\t\t\t\t Player draws: "
    + mine);
    mine = d.draw();
    System.out.println("\t\t\t\t\t\t" + mine);
    do{
    String input = JOptionPane.showInputDialog
    ("Would you like a card? ");
    if(input.equalsIgnoreCase("yes"))
         mine = d.draw();
    System.out.println("\t\t\t\t\t\t" + mine);
         playerTotal++;
         else if(input.equalsIgnoreCase("no"))
    System.out.println("\t\t\t\t Player stands");
         else
    System.out.println("\t\tInvalid input.
    Please try again.");
    I don't know how to go about making and calling a method or class that will combine the total cards delt to the player and the total cards delt to the dealer. The rank() method only seems to give me the last cards drawn to compare with when I try to do the tests.**/
    if ((dealer.rank() > mine.rank())
    && (dealer.rank() <= 21)
    || (mine.rank() > 21)
    && (dealer.rank() < 22)
    || ((dealer.rank() == 21)
    && (mine.rank() == 21))
    || ((mine.rank() > 21)
    && (dealer.rank() <= 21)))
    System.out.println("Dealer wins");
    wins++;
         else
    System.out.println("I win!");
    break;
    } while (playerTotal <= 1);
    String stop = JOptionPane.showInputDialog
    ("Would you like to play again? ");
    if (stop.equalsIgnoreCase("no"))
    break;
    if (rounds == 5)
    System.out.println("Player wins " +
    (CardDeck.rounds - wins) + "rounds");
    } while (rounds <= 5);
    private Card[] deck;
    private int cards;
    public static int rounds = 1;
    public int cardValue;
    /**When I try to compile this nested class, I get an error message saying I need a brace here and at the end of the program. I don't know if any of this code would work because I've tried adding braces and still can't compile it.**/
    class Hand()
    static int r = 1;
    public Hand() { CardDeck.rounds = r; }
    public int getRounds() { return r++; }
    final class Card
    public static final int ACE = 1;
    public static final int JACK = 11;
    public static final int QUEEN = 12;
    public static final int KING = 13;
    public static final int CLUBS = 1;
    public static final int DIAMONDS = 2;
    public static final int HEARTS = 3;
    public static final int SPADES = 4;
    public Card(int v, int s)
    value = v;
    suit = s;
    public int getValue() { return value; }
    public int getSuit() { return suit;  }
    public int rank()
    if (value == 1)
    return 4 * 13 + suit;
    else
    return 4 * (value - 1) + suit;
    /**This works, but I'm confused. How is this method called? Does it call itself?**/
    public String toString()
    String v;
    String s;
    if (value == ACE)
    v = "Ace";
    else if (value == JACK)
    v = "Jack";
    else if (value == QUEEN)
    v = "Queen";
    else if (value == KING)
    v = "King";
    else
    v = String.valueOf(value);
    if (suit == DIAMONDS)
    s = "Diamonds";
    else if (suit == HEARTS)
    s = "Hearts";
    else if (suit == SPADES)
    s = "Spades";
    else
    s = "Clubs";
    return v + " of " + s;
    private int value; //Value is an integer, so how can a
    private int suit; //string be assigned to an integer?
    }

    Thank you so much for offering to help me with this Jamie! When I tried to call change value using:
    Card dealer = (d.changeValue());
    I get an error message saying:
    Incompatible types found: int
    required: Card
    I had my weekly class last night and the professor cleared up a few things for me, but I've not had time to make all of the necessary changes. I did find out how toString worked, so that's one question out of the way, and he gave us a lot of information for adding another class to generate random numbers.
    Again, thank you so much. I really want to learn this but I'm feeling so stupid right now. Any help you can give me about the above error message would be appreciated.

  • New To Java. Learning About Java Applet.

    Hi all, i am new to java. Actually i am facing problem with java applet. I am trying to use my cellphone to surf web. When i open a webpage from cellphone, the part which contain the java applet, it cannot be shown and appeared blank. I am using Nokia 7610, can it be solve?
    Can anybody help me? Thank You.

    JSE JRE stand for Sun Java Studio Enterprise 8.1 Final Release or Java SE Runtime Environment (JRE)? Which Shall i download? By the way the file type are .exe and .sh , how to let it become .sis?

  • I have a macbook purchased in 2009. I just purchased a new ipod and am in a loop of updates that seems to have no end.  Do I need snow leopard? Will that then let me use the latest itune update?

    I have a macbook purchased in 2009. I just purchased a new ipod and am in a loop of updates that seems to have no end.  Do I need snow leopard? Will that then let me use the latest itune update?

    You need to buy Snow Leopard from the online Apple Store. You don't need Mountain Lion.
    (85170)

  • I'm new to Java, what do i need?

    Hi,
    I've been programming C++ for a while, and now I have decided to start programming in Java too.
    So, what do i need to program in Java?
    Got any Java compiler to recommend?

    Installation Notes - JDK 5.0 Microsoft Windows (32-bit)
    Your First Cup of Java
    The Java� Tutorial - A practical guide for programmers
    The Java� Tutorial - Trail: Learning the Java Language
    New to Java Center
    Java Programming Notes - Fred Swartz
    How To Think Like A Computer Scientist
    Introduction to Computer Science using Java
    The Java Developers Almanac 1.4
    Object-Oriented Programming Concepts
    Object-oriented language basics
    Don't Fear the OOP
    Books:
    The Java Programming Language - 4th Edition
    Head First Java, by Bert Bates and Kathy Sierra
    Thinking in Java (Free online), by Bruce Eckel
    Core Java, by Cay Horstmann and Gary Cornell
    Effective Java, by Joshua Bloch
    http://java.sun.com/developer/Books/javaprogramming/

  • Need guidness, New to Java

    Hi
    i am 100% new to java, plz help, i have good concepts of programing but i dont know any thing about java. so plz tell me how to start, what should i install n from where to download,
    i will be so thank full to u
    plz email me at [email protected]
    regards
    imran

    Hi! I am also new to java. I was suggested in this forum two days back to start at Java Tutorial, and I am really thankful to the person who suggested me the tutorial. Its a good starting point.
    http://java.sun.com/docs/books/tutorial/
    Hope you find it useful too.

Maybe you are looking for