Can you repaint certain elements (ie a ball) while NOT repainting the board

I'm really just starting out writing java applets. I don't use Applet, i use JApplet because the coding is much more familiar. I understand that this might be a bad decision and if it is, please tell me! Anyway, my question is: Can I have the paint method (called via repaint()) paint the board one time, then only repaint the ball and it's constant change in position? I want to do this because the only way I can think of to have constant motion of the ball (I'm trying to simulate Pong) is by adding repaint() to the end of the paint function. This causes the applet to repaint very rapidly and everything gets all cut up and looks like crap. Any ideas on how I can make it run smoother? my code is teh following, if it helps (I realize that it isn't really commented, I am very messy and I do apologize):
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
public class NewProj extends JApplet implements ActionListener, MouseListener, MouseMotionListener, KeyListener{
/** Creates a new instance of NewProj */
private int xBar1,yBar1,xBar2,yBar2;
private String code;
private int bar1H, bar2H, yMovement, xMovement;
private int frame;
private int ballX,ballY;
private boolean hitTop,hitBottom,hitLeft,hitRight,hitPaddle1,hitPaddle2;
private int score1,score2;
private Date date;
public NewProj() {
date = new Date();
score1=0;
score2=0;
ballX=175;
ballY=125;
yMovement=ballY+3;
xMovement=ballX+3;
frame=0;
bar1H=40;
bar2H=40;
code="";
xBar1=30;
yBar1=130;
xBar2=320;
yBar2=130;
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.addKeyListener(this);
public void paint(Graphics g){
super.paint(g);
g.drawString("Score: "+score1,220,20);
g.drawString("Score: " + score2,80,20);
if(frame==25){
//initiate a and xMovement as moving linearly with a slope of 1.
yMovement=ballY+3;
xMovement=ballX+3;
if(ballX<=xBar1+8 && ballX>=xBar1 && ballY<=yBar1+bar1H && ballY>=yBar1){
hitPaddle1=true;
hitPaddle2=false;
hitRight=false;
hitLeft=false;
if(ballX<=xBar2+8 && ballX>=xBar2 && ballY<=yBar2+bar2H && ballY>=yBar2){
hitPaddle2=true;
hitRight=false;
hitLeft=false;
//check if the ball hit the Top
if(ballY<1){
hitBottom=false;
hitTop=true;
if(ballY>250){
hitTop=false;
hitBottom=true;
if(ballX<10){
hitPaddle2=false;
hitPaddle1=false;
hitRight=false;
hitLeft=true;
score1++;
if(ballX>350){
hitPaddle2=false;
hitPaddle1=false;
hitLeft=false;
hitRight=true;
score2++;
if(hitBottom){
yMovement=ballY-3;
if(hitTop){
yMovement=ballY+3;
if(hitLeft){
xMovement=ballX+3;
if(hitRight)
xMovement=ballX-3;
if(hitPaddle1){
xMovement=ballX+3;
if(hitPaddle2){
xMovement=ballX-3;
System.out.println(yMovement+" is yMovement " + "and" + xMovement + " is xmove");
ballY=yMovement;
ballX=xMovement;
frame=0;
repaint();
if(code.equals("penis")){
if(bar1H>=60)
bar1H=60;
else bar1H+=5;
code="";
//top & bottom
g.drawLine(0,0,350, 0);
g.drawLine(0,250,350,250);
//left & right sides
g.drawLine(0,0,0,250);
g.drawLine(350,0,350,250);
//Midline
g.drawLine(175,0,175,250);
//ball
g.setColor(Color.MAGENTA);
g.fillOval(ballX,ballY,8,8);
g.setColor(Color.RED);
g.fillRect(xBar1,yBar1,5,bar1H);
g.setColor(Color.BLUE);
g.fillRect(xBar2,yBar2,5,bar2H);
frame++;
for(int b = 0; b<1000;b++){
int hilbo=b+3;
repaint();
public void mouseClicked(MouseEvent e) {
public void mouseEntered(MouseEvent e) {
repaint();
public void mouseExited(MouseEvent e) {
public void mousePressed(MouseEvent e) {
public void mouseReleased(MouseEvent e) {
public void actionPerformed(ActionEvent e) {
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e) {
int y = e.getY();
if(y<=(250-bar1H))
yBar1 = y-(int)bar1H/2;
else yBar1 = 250-bar1H;
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==40){
yBar2+=10;
if(e.getKeyCode()==38){
yBar2-=10;
if(yBar2>=225)
yBar2=225;
if(yBar2<=0)
yBar2=0;
repaint();
for(int i=65;i<=90;i++){
String s = "";
switch(e.getKeyCode()){
case 80: code=code+"p";
break;
case 69: code=code+"e";
break;
case 78: code=code+"n";
break;
case 73: code=code+"i";
break;
case 83: code=code+"s";
break;
case 82: code=code+"r";
break;
case 65: code=code+"a";
break;
case 75: code=code+"k";
default:code="";
break;
public void keyReleased(KeyEvent e) {
public void keyTyped(KeyEvent e) {

Check out the tutorials on this site. Start here: http://java.sun.com/docs/books/tutorial/
You can have a thread like this:public class Game extends java.awt.Component {
  public void init() {
    Thread t = new Thread(new GameLoop());
    t.start();
  public void paint(Graphics g) {
    // draw your game here.  Do nothing other than drawing.
  private class GameLoop { // inner class
    public void run() {
       while(true) {
          // update your game state here -- e.g., move the ball's position
          try {
             Thread.sleep(200); // 200 miliseconds, 1/5 of a second
           catch(InterruptedException e) {
              // this is the rare exception that it's generally OK to ignore
           repaint();
}that's not perfect but it will get you started

Similar Messages

  • How can you take our money for gifts cards and not fix the problems or offer support AFTER it's paid for?

    I am EXTREAMLY annoyed after a 3 hour run around in trying to use my gift card! I promised some apps to my little kids and mommy is busy trying to figure out why it shows a $25 credit but also says my credit card security code is wrong. It's CORRECT and my credit card shouldn't even be used!! I have been raving about IPAD to my friends and told them the apps were great learning tools for their kids. I even shared mine with my child care provider and she was thinking about getting one for her daycare.... Then showing parents who may like it. I WILL be sharing the lack of support for your gift cards! Perhaps I will just go back to the other learning games for my kids and buy games I can count on. We have Lots of Leap Frog and others and not once have I found such a run around!

    Did you try logging into your iTunes account on your computer rather than on the iPad? It may not make any difference but you don't know until you try.
    One other thought - why dont you restart your iPad and see if that helps. Hold down on the sleep button until the red slider appears and then slide to shut down. hold the sleep button until the Apple logo appears to power up. Let the iPad fully restart and see if you can go into your account in settings and remove the credit cards - see if None shows up in the settings.
    I restarted my iPad yesterday when I was having iTunes store issues and the issues cleared up. It may have been purely coincidental but its worth a try.

  • Can you import a player package? I do not have the .odarc file...

    Hey there,
    My hard drive crashed with my backup odarc file that I did not move to my backup! I do have the latest player package file. This was created in 3.6.1. Is there a way to rebuild the library from a player package? Please say yes...

    I had a similar thing happen years ago, and luckily Oracle was able to help me out. I'd try logging a support ticket in My Oracle Support.
    Good luck, and learn to back up regularly!!!

  • Can you load photoshop elements, disc version, on more than one computer?

    Can you load photoshop elements, disc version, on more than one computer?

      It‘s been part of recent versions, perhaps from PSE8 onward; I‘m not exactly sure. The software activates automatically upon installation. If you ever change computers and already have two activations simply open the Editor on the old machine and on the top menu click help à deactivate
     

  • Can you use home share where there are 2 itunes on the same computer but under different user profiles?

    Can you use 'Home Share' where there are 2 itunes on the same computer, but under different user profiles?

    check out method one from this support article -> How to use multiple iPods, iPads, or iPhones with one computer

  • Can you only use a credit card to purchase products in the Appstore

    Can you only use a credit card to purchase products in the Appstore?

    You can also use prepaid iTunes cards, if those are available in your country. PayPal is also an option if you're in the US.
    Regards.

  • HT204266 Article isn't correct anymore, can you tell me where to find Nano G5 games on the Apple store now?  Please send me a link?

    Article isn't correct anymore, can you tell me where to find Nano G5 games on the Apple store now?  Please send me a link?

    Try posting in the iPod Nano forum. You posted in the iPod touch forum.

  • Can you install logic pro onto more than one computer with the same cd key?

    can you install logic pro onto more than one computer with the same cd key?

    Only a desktop and a laptop, both of which must be Macs you own.
    (60296)

  • Can you build a battery for iphone that will last till the end of the day?

    Can you build a battery for iphone that will last till the end of the day?

    No, I can't. I've never taken any classes in either electrical or chemical engineering.
    While you're looking for someone who can, you may want to try searching the forums for the many other threads that give tips on extending your battery life (things like turning off Background Refresh). You may also want to look at battery cases such as those made by Mophie.
    Best of luck.

  • Can't delete certain emails from Mail.  Instead, I get the message "An error occurred while moving messages to the mailbox, etc."

    Can't delete certain emails from Mail.  Instead, I get the message "An error occurred while moving messages to the mailbox, etc."  The problem appears randomly.

    I had the same problem, and it was solved by going to the Mailbox menu and selecting "rebuild."

  • I made an error asking to receive Windows product of Adobe Element 13 and Premiere while I needed the Mac Version.  How is it possible to correct this error ? and what do I have to do if you accept ?

    I made an error asking to receive Windows product of Adobe Element 13 and Premiere while I needed the Mac Version.  How is it possible to correct this error ? and what do I have to do if you accept ?

    Normally an Elements license is valid for both Windows and Mac systems.  So you might only need to download the Mac versions of the applications and use your serial number to activate them.

  • Can you sort ibookshelf books by collections and list all of the books in each collection?

    can you sort ibookshelf books by collections and list all of the books in each collection?

    The collection changes a little by the criteria you use to view the books, and you can view them by Title, Author, and Category. So, yes, you can.
    The default view is Book view. On the upper right part of the page, you'll see four white blocks - that's the icon for book view. The icon on the right has three white lines; that's where you find Title, Author, and Category view. You can't create folders in any view.

  • Can you install adobe flash player onto a Galaxy Note Tablet

    Can you install Adobe Flash Player onto a Galaxy Note Tablet

    Please see this page for a list of all certified devices:
    Flash Player Certified Devices
    If your device is supported, this FAQ will walk you through the process of manually installing the plugin:
    How do I manually install Flash Player on my Android device?

  • Can you layer a photo on top of a video in the iMovie app on an iPhone or an iPad?

    Can you layer a photo on top of a video in the iMovie app on an iPhone or an iPad?

    Not without a third party app. You can search the app store for apps that let you caption and write on photos but there is no native functionality for this in iOS.

  • Can you resume an unfinished/failed Airdrop transfer?  Where do the unfinished transfer files go?  Thanks in advance!

    Can you resume an unfinished/failed Airdrop transfer? 
    Where do the unfinished transfer files go?
    I was 75% thru a 8 gig transfer before I had to stop.  Just wondering if there's a way to resume instead of starting all over, otherwise I'll break up the files into smaller segments I transfer in the future.

    Can you resume an unfinished/failed Airdrop transfer? 
    Where do the unfinished transfer files go?
    I was 75% thru a 8 gig transfer before I had to stop.  Just wondering if there's a way to resume instead of starting all over, otherwise I'll break up the files into smaller segments I transfer in the future.

Maybe you are looking for

  • Any way to get an image file's height?

    I am now trying to dynamically insert a Canvas component with an image component inside of it. I need to re-used the code for many images. Apparently it is only working if I give the canvas a width and height, which needs to be the size of the image

  • Need to personalize the Remittance screen making 'Manual Create' Button invisible

    Dear All, I highly appreciate you people responding to my before questions. All are solved/clarified. Below is the requirement from my friend, I am trying to help him. Please suggest. In Receivables>Receipts>Remittance screen Need to disable the 'Man

  • Cannot ssh using jsp

    Hi I have a jsp page which contains the shell script for running an instance and then doing ssh to the remote host server .When the jsp page is loaded the instance is run but ssh cannot be done .The ssh to instance takes time (20 to 25 sec ) ,so i tr

  • Safari fails to load any page. Chrome & Firefox all working fine. I've tried everything

    Safari will not open any sites. I get the 'can't connect to the server' error page Weird thing is the top sites page shows up and shows current website images, but click one and it won't connect Its been like this for weeks, but isn't my main browser

  • Get DNG Date Created from dng sdk

    I have successfully compiled the dng sdk and the validate app and I wondered what is a simple way to just get the date created out?