Flickering problem with applet

After doing a programming lab assignment, with no work, I decided to modify one of the completed programs, which was a pong-like game. Anyways, after changing it to a white on black setup, I now get major flickering. One of the previous programs was to animate something without it flickering using a buffered image. So I converted it over, but it had no effect.
import java.applet.Applet;
import java.awt.*;
public class PongGame2 extends Applet
     int paddleX, paddleY, ballX, ballY, incX, incY, score, appletWidth, appletHeight;
     Image virtualMem;
     Graphics gBuffer;
     public void init()
          ballX = 10;
          ballY = 10;
          incX  = 3;
          incY  = 3;
          score = 0;
          appletWidth = getWidth();
          appletHeight = getHeight();
          virtualMem = createImage(appletWidth, appletHeight);
          gBuffer = virtualMem.getGraphics();
     public void paint(Graphics g)
          draw(g, paddleX, paddleY, ballX, ballY);
          ballX += incX;
          ballY += incY;
          if (incY > 0 && ballY > 580-incY)
              incY = -incY;
          else if (incY < 0 && ballY < 2*incY)
              incY = -incY;
          if (ballY < paddleY && ballY >= paddleY-20 && ballX > paddleX && ballX < paddleX+100)
               incY = -incY;
          if (ballY > paddleY + 10 && ballY < paddleY + 20 && ballX > paddleX && ballX < paddleX+100)
              incY = -incY;
          if (incX > 0 && ballX > 780-incX)
              incX = -incX;
          else if (incX < 0 && ballX < 2*incX)
              incX = -incX;
          if (ballY < 1)
               score++;
           try
                Thread.sleep(7);
           catch(InterruptedException ex)
          repaint();     
     public boolean mouseMove(Event e, int x, int y)
          paddleX = x-50;
          paddleY = y-10;          
          repaint();
          return true;
     public boolean mouseIdle(Event e, int x, int y)
          repaint();
          return true;
     public void createBackground(Graphics g)
          gBuffer.setColor(Color.black);
          gBuffer.fillRect(0,0,800,600);
          gBuffer.setColor(Color.white);   
          gBuffer.drawString("Score: " + score/5,200,20);
     public void draw(Graphics g, int paddleX, int paddleY, int ballX, int ballY)
          createBackground(g);
          gBuffer.setColor(Color.white);
          gBuffer.fillRect(paddleX,paddleY,100,20);
          gBuffer.fillOval(ballX,ballY,20,20);
          g.drawImage(virtualMem,0,0,this);
}

Petes1234, thanks!
It works perfectly now, and without having warnings of using deprecated features. It took me a while though to find how to set this up, and proper methods.
Here's the code right now:
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class JavaPongGame1 extends JApplet implements MouseMotionListener
     int paddleX, paddleY, ballX, ballY, incX, incY, score;
     public void init()
          ballX = 10;
          ballY = 10;
          incX  = 3;
          incY  = 3;
          score = 0;
          addMouseMotionListener(this);
     public void paint(Graphics g)
          draw(g, paddleX, paddleY, ballX, ballY);
          ballX += incX;
          ballY += incY;
          if (incY > 0 && ballY > 580-incY)
              incY = -incY;
          else if (incY < 0 && ballY < 2*incY)
              incY = -incY;
          if (ballY < paddleY && ballY >= paddleY-20 && ballX > paddleX && ballX < paddleX+100)
               incY = -incY;
          if (ballY > paddleY + 10 && ballY < paddleY + 20 && ballX > paddleX && ballX < paddleX+100)
              incY = -incY;
          if (incX > 0 && ballX > 780-incX)
              incX = -incX;
          else if (incX < 0 && ballX < 2*incX)
              incX = -incX;
          if (ballY < 1)
               score++;
          try
                Thread.sleep(7);
           catch(InterruptedException ex)
          repaint();
     public void mouseMoved(MouseEvent e)
          paddleX = e.getX()-50;
          paddleY = e.getY()-10;          
          repaint();
          e.consume();
     public void mouseDragged(MouseEvent e)
     public void createBackground(Graphics g)
          g.setColor(Color.black);
          g.fillRect(0,0,800,600);
          g.setColor(Color.white);   
          g.drawString("Score: " + score/5,200,20);
     public void draw(Graphics g, int paddleX, int paddleY, int ballX, int ballY)
          createBackground(g);
          g.setColor(Color.white);
          g.fillRect(paddleX,paddleY,100,20);
          g.fillOval(ballX,ballY,20,20);
}

Similar Messages

  • DoubleBuffering problem with applet

    Hi,
    We are developing an applet with multiple screens. CardLayout is being used to flip between different panels in the systems. The applet contains huge JTable and chart components and huge amount of data, which makes the refreshing of the applet very slow. We tried the DoubleBuffering concept to reduce the flickering in the JApplet class.
    Now the questions are:
    1) Do we need to do double buffering for all the panels that are used in the system ?
    2) Is there any other better way that Sun suggests to avoid flickering problems ?
    Any help will be much appreciated. Also with the email content and problem definition thread is going on, any feedback about this email is also welcomed.
    Thanks
    Anoop

    One solution is to put all the packages in a jar file.

  • Problem with Applets in same page

    Hai to all,
    I am having a problem with GUI in applets
    My first class extends a JPanel named A_a
    import javax.swing .*;
    import java.awt.*;
    import java.awt.event.*;
    public class A_a extends JPanel
    JButton jb;
    JTextArea text;
    public A_a()
    setLayout(new FlowLayout());
    jb=new JButton("Click me");
    //add(jb);
    text=new JTextArea(5,20);
    add(text);
    public void text_appendText(String aa)
    System.out.println("I AM IN A_a");
    text.append(aa);
    text.revalidate();
    revalidate();
    /*public static void main(String ags[])
    A_a a = new A_a();
    JFrame frame=new JFrame();
    frame.getContentPane().add(a);
    frame.pack();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) { System.exit(0); }
    frame.setSize(200,200);
    frame.show();
    and then I am using other class B_b which is an applet carries a exitsing panel (A_a) inside it .
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class B_b extends JApplet
    public A_a a;
    public void init()
    a=new A_a();
    getContentPane().add(a);
    public void text_appendText(String aa)
    final String aaa =aa;
    new Thread(new Runnable()
         public void run()
              a=new A_a();
              a.setBackground(new java.awt.Color(255,200,200));
              System.out.println("I AM IN B_b");
              a.text.append(aaa);
              a.text.revalidate();
              getContentPane().remove(a);
              resize(500,500);
         }).start();
    and the I am using the second applet C_c in which by performing a button action the old panel A_a should get removed and replace the new panel D_a (which is not here )in the applet B_b with all other components(namely button , text fields etc)
    import javax.swing .*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.*;
    public class C_c extends JApplet implements ActionListener
    JButton jbt;
    JTextArea jta;
    public void init()
    getContentPane().setLayout(new FlowLayout());
    jbt=new JButton("Click me");
    jbt.addActionListener(this);
    getContentPane().add(jbt);
    jta=new JTextArea(5,20);
    getContentPane().add(jta);
    public void actionPerformed(ActionEvent ae)
    Enumeration e = getAppletContext().getApplets();
    Applet applets = null;
              while(e.hasMoreElements())
                   applets=(Applet)e.nextElement();
                   if ( applets instanceof B_b)
                             System.out.println("I AM CLASS C_c");
                             ((B_b)applets).text_appendText(jta.getText());          
                             ((B_b)applets).remove());
                             ((B_b)applets).getContentPane().add(D_d);
    both the applets C_c and B_b are in same browser page
    How can i achive that pls help .

    please help me its urgent for my project

  • Problem with Applet for Microsoft VM

    Hi there!
    I implemented an applet (first for SUN JVM 1.4.1_02) that has to communicate with a servlet via serialized objects. Therefore the applet sends a request and expects the response :
    //establish connection
    URLConnection l_con= l_url.openConnection();
    l_con.setUseCaches(false);
    l_con.setDoOutput(true);
    l_con.setDoInput(true);
    l_con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
    //send request to servlet               
    ObjectOutputStream l_outputStream = new ObjectOutputStream(l_con.getOutputStream());     
    l_outputStream.writeObject(l_obj);                    
    l_outputStream.flush();               
    l_outputStream.close();
    //get response
    ObjectInputStream l_obInStr = new ObjectInputStream(l_con.getInputStream());
    l_response = (MyResponseClass)l_obInStr.readObject();
    l_obInStr.close();This works absolutely fine for the Sun VM. Now I have to use the Microsoft VM (because I don't want the users to install anything on their clients).
    I compiled the client classes like that:
    javac -target 1.1 classname.javaThe applet starts correctly but there is a problem with the communication that I recognized in the console:
    java.io.IOException: Cannot write output after reading input.
         at com/ms/net/wininet/http/HttpURLConnection.getOutputStream (HttpURLConnection.java)
         at myclass/MyApplet.callToServlet (MyApplet.java:109)
    Does anyone know what this error means ?
    Thanks a lot in advance.

    Hi there!
    I solved my problem by removing all the System.out.println-statements that I used for debugging issues.
    Anyway, I want to know if there are more restrictions concerning this matter for using the Microsoft VM instead of the Sun VM. (Because it HAS worked with the debugging statements with the Sun VM).
    Thank you.

  • Facing "Display Flickering" problem with my 15" macbook pro(A1286)while swapping the desktops and closing the tabs.....need help...reply soon

    Suggest Solution for this Display Flickering problem ....

    Hi, I read the post about calibration and I probably do that a little too often TBH! As I get so little time out the battery and mainly use my MBP on my lap, usually with the power supply near by, it gets charged to full, run to empty to the point of sleep, or sometimes it even cuts out and then is recharged to full again, so I really think it's new battery time. I've seen non Apple Official ones for between £35 and £65 that have 3 year warranties and state they are around 5200mh but am unsure due to reviews.
    Later...
    Anyway's I have now ordered a non Apple replacement from Amazon for about £35 and it has several good reviews and 4.5 out of 5 stars so I'll see if it works and report back. If it doesn't I'll send it back and get the Official Apple one.
    Message was edited by: Missy Steph

  • Flickering problem with Apple TV

    I am suddenly having flickering problems after updating Apple TV.  The box has worked fine for several months though I did have this same problem when I first got it in February.  I have a Panasonic 37' viera.  I have unplugged both the TV and the Apple TV box, reset it a number of times.  Don't know what to do.

    I'm having the same issue, albeit right out of the box.  I hooked up my Apple TV according to the instructions, have no other issues with my wifi, and the HDMI and power cables are all securely connected.  My TV's also a Samsung, experiecing the same "flickering" of audio.  In addition, during set-up, the screen was also flickering to white while inputting my wifi info.
    Am hoping swapping it out in store will fix the problem. I was so excited to get my AppleTV in the mail and everything else Apple-to-TV has been working great. (I had my ancient black MacBook -turning 6 in June- hooked up with zero connectivity problems.)  Wondering if this will be an ongiong issue? Had decided against AppleCare for such an inexpensive addition to my home, but maybe I should re-think that decision...

  • Flickering problem with R9 270X 4G LE

    I am having a problem with a flickering screen during and 2D operation, everything is fine during any 3D operations(ie. gaming). I assume from reading other forum posts that I need to update my bios even though LiveUpdate says that its good. My S/N is 602-V303-20SB1403059248. Any help would be appreciated.

    I think that I have attached the file you are wanting. Thanks for your help. Pitcairn.zip (96.75 kB - downloaded 13 times.)

  • Flickering problem with live viewing with IMAQ Image Display

    Hi everyone,
    I am trying to write a program to do live imaging with Andor camera. I am using some examples from Andor. The images are acquired in sequence and send into IMAQ Image Display and also Intensity Graph. There is minimal flickering issue in the Intensity Graph but there is very bad flickering problem in IMAQ Image Display. Perhaps I misunderstand how to optimize the vision tools. Does anyone know how to overcome this issue? I have attached the code that I am testing. Thanks in advance.
    Best regards,
    Han Yen
    Solved!
    Go to Solution.
    Attachments:
    iXonRTA_live_0.0.1.vi ‏70 KB

    You can use Vision Assistant to test our vision functions. Vision Assistant is really meant to prototype - it does not have all teh functionality of Vision Development Module and is not as flexible as we don't allow you access to all the settings for every function.
    I recommend starting in Vision Assistant and then porting over to LabVIEW or C (whatever you are most comfortable with)...this is built-in functionality. There is nothing wrong with using Vision Assistant to do live imaging, just that you run into limitations and you will only be able to use this code on computers that have Vision Assistant.
    Hope this helps.
    --Michelle
    Instrument Control R&D
    National Instruments
    Instrument Control
    Machine Vision

  • Error loading class problem with applet (Newbie)

    Hi,
    I am new to Java applets. I try to display a simple sample. I put this into html file: <APPLET codebase="classes" code="NewApplet.class" width=350 height=200></APPLET>
    And it always appears one problem:
    Error loading class: NewApplet
    java.lang.NoClassDefFoundError
    java.lang.ClassNotFoundException: NewApplet
         at com/ms/vm/loader/URLClassLoader.loadClass
         at com/ms/vm/loader/URLClassLoader.loadClass
         at com/ms/applet/AppletPanel.securedClassLoad
         at com/ms/applet/AppletPanel.processSentEvent
         at com/ms/applet/AppletPanel.processSentEvent
         at com/ms/applet/AppletPanel.run
         at java/lang/Thread.run
    Can anybody help please? Thanks in advance.

    Now I am able to load the applet on MAC OS X 10.4.11. This is due to network issue.
    Now my problem with the applet functionality. My applet will display the map image. Applet contains the navigation arrow keys at the top left of my applet. These keys will be used to navigate through the map.
    The problem is while user using the navigation buttons, the image got squash and stretch.
    This is happened only on MAC OS X 10.4.11 and Its working fine on PC and MAC OS X 10.5.5.
    Please anyone help in this regard. Thanks in advance.

  • Problems with applets.

    Hi to everybody.
    I've got a problem with simple applets.
    I've built several applets using Swing components (JApplet, JPanel, JButton, etc.)
    In my browser, the background color of my applets does correctly appear.
    But none of my components does !!!! No text fields, no buttons etc.
    Can someone explain to me which could be the reason of that ?
    Thank's.

    I've just tried appletviewer. And I discovered that there is a problem while trying to acces to a jpeg file for my JBuutons and JLabel.
    Appletviewer considers these files are on the client machine and refuses the acces.
    But when I try my site, the server is my computer too ! So the jpegs are on the server side !
    Furthermore, when I start my web page (that contains my applets), nothing does appear (no exception) in the java console...
    How can I solve this problem (I'm newer with applets)?

  • Still Having Problems With Applets!!!!

    First i'd like to thank all who responded to my last post regarding a downloadable copy of the J2SE API Documentation. I have downloaded it and am quite content.
    I am still, however having a problem with my applets (all applets at that). Like i said before. I can view any applets i want through the command prompt using appletviewer. My problem is residing in execution directly through Microsoft Internet Explorer 5.x.
    When i open the .html file containing my applet, the file loads in the browser but the applet does nothing. When i mouseover the applet a message is displayed in the browser's status bar stating "loading class. classname: class not found".
    I have tried every applet i can get my hands on including downloaded applets from applet resource sites (even the example applets that came with the JDK1.4 when i downloaded it). They all react the same way, but will execute fine with the direct command prompt reference to appletviewer.
    It's getting very irritating. I have tries everything including setting the classpath in the command prompt. I'm loosing hope of a solution so if anyone might have an idea of what i need to do to fix my problem, or anything i am doing wrong or haven't done, please give me a hint or help.
    I am loving the java language and wish to further my knowledge and farmiliarity with it but i need to make sure everything works.
    Sorry for rambling and i thank whoever offers a reply in advance.
    ChanceH

    1. Check the codebase parameter in the html file that calls the applet
    for example if you have an applet named MyApplet.class inside a directory applets and your html file outside that directory you should have a codebase like this "applets/".
    <applet CODE="MyApplet.class" codebase="applets/" WIDTH=640
    HEIGHT=480 ALIGN= center>
    </applet>
    2. Be sure that your IE support java check the properties and enable the JVM.
    3. If your JVM version does not support Java 2, applets build on Java 2 will not work.

  • Problem with applet file loading

    I have a problem with loading files into a java applet. import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.net.*;
    public class main extends Applet {
         private static ArrayList<String> SGM;
         public void init() {
              SGM = new ArrayList<String>();
              String gameDir = getParameter("game_dir");
              try {
                   File fHandle = new File(gameDir+"game.sgm");
                   Scanner fScanner = new Scanner(fHandle);
                   while(fScanner.hasNextLine()) {
                        SGM.add(fScanner.nextLine());
                   //System.out.println(SGM.get(0));
              } catch(Exception e) {
                   System.out.println("Applet threw error: "+e);
    ...The problem is with File fHandle = new File(gameDir+"game.sgm"); I tried putting File(gameDir+"/game.sgm"); and appending / to the folder in the <PARAM> tag, but it still changes it to \, which means I can only run it locally, and in Windows. Why is it doing this, and how can I fix it?

    This file you were hoping to load: it's on the server, right? Not on the computer where the applet is running in a browser?
    (At least that's a common mistake made by people when they write their first applet.)
    If that's the case then you can't use a File object to access a file on a different computer. To access a file on the server, first you will have to put the file in a place on the server where it can be downloaded. Then in your applet you need to get a URL for the file. (The getDocumentBase() method can be used to prevent you from having to hard-code the full URL for the file.) Next, "this.getClass().getResourceAsStream(URL)" gets you an InputStream from which you can read the data from the file.

  • Problem with applet, popup window and focus loss

    Hi all !
    I've got a problem when my applet lose the focus. Let me explain :
    I've got an applet embed in a jsp page. There are several buttons in it that allow the user to open modal windows.
    The problem comes when the user gives the focus to another application whithout closing the modal window first.
    When he tries to come back to the web page owning the applet (using the task bar), then the modal window stays behind it and blocks any action on the applet.
    Does anyone know how to force the modal window to be displayed in front of the applet, even when the user plays with focuses ?
    Thank's in advance.

    thank you for your help, sils.
    I've written that code :
    * Cr�� le 31 ao�t 05
    package com.scor.apricot.web.rpc.ltnp.applet.listener.ldf;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    public class AppletWindowListener extends WindowAdapter {
         private JPanel panel;
         private JDialog dialogWindow;
         public AppletWindowListener(JPanel panel, JDialog dialogWindow) {
              this.panel = panel;
              this.dialogWindow = dialogWindow;
         public void windowActivated(WindowEvent e) {
              if (dialogWindow!=null && dialogWindow.isShowing())
                   dialogWindow.toFront();
    }Is that right ?
    I don't know how to add the listener, where must I put the addWindowListener method ? It seems that this method cannot be used with a JApplet object.

  • Problem with applet , Not able to detect class file

    Hello friends,
    I have a swing/applet class file, and i am trying to integrate it with Tomcat webapps
    My HTMl code goes like this...........
    <APPLET ALIGN="CENTER"
                   CODEBASE="/../../classes/org/jgraph/"
                   CODE="SimpleGraph.class"
                   WIDTH="800" HEIGHT="500">
                   </APPLET>
    My classes are found inside the code base URL............
    CODEBASE="/../../classes/org/jgraph/"
    My folder structure is like below.....in which "SimpleGraph.class" is found
    D:\June5WF\Archicentro\web\WEB-INF\classes\org\jgraph\SimpleGraph.class
    My HTML resides in the following directory structure...
    D:\June5WF\Archicentro\web\WEB-INF\jsp\secure\admin\ramesh.jsp
    But When I run the JSP page .... I am Getting Class not found exception..........
    Please can anyone tell me how to solve this problem....
    I have spent 4 hrs in this......PLZ help me out.....

    Hi,
    I did not mean file object. I meant workbook object. The problem is that I can not see from your code how do you get that OLE object reference. It should be something like this
    GET PROPERTY OF h_excel 'ActiveWorkbook' = h_workbook.
    CALL METHOD OF h_workbook 'SAVEAS' EXPORTING #1 = l_file
    Regards

  • Problems with applets in IE

    When i try to make my applet work in internet explorer it just comes up with a blank grey box and an error message saying:
    load: class classname not found
    Does anyone know how i can correct this?

    if all my problems were as simple as this then i would have an easy life!
    you are probably using the applet from swing rather than awt - you need the java plugin for swing but not for awt. either use only awt gui's or download the jre plugin.

Maybe you are looking for

  • Some tech related questions...

    Ok, I know enough about computers, but with switching to a mac, hardware issues are becoming somewhat confusing to me. I bought this http://www.newegg.com/product/product.asp?item=N82E16822136032 to use with my macbook pro over eSATA, but I wanna sel

  • How to import jpegs at 100 dpi?

    The main layer of my movie is a jpeg at 100 dpi. When I import it to the stage, Flash converts it to 72 dpi, which makes everything too small. I'm using Flash MX. Do later versions of Flash support 100 dpi, and if yes, which ones? If no, what can I d

  • Installing Eclipse plug-in

    After extracting PDK plugin to C:\eclipse2.1\eclipse\plugins, I do not see the expected entry in Eclipse: Window > Preferences > Portal Development Kit. Nor is there a PDK entry for Project, Import, or Export as the PDK documentation states should ex

  • Slow Wireless Connection... Fast cabled connections.

    I have an autonomous 1142N WAP connected to a 2960 and when I cable my laptop into the switch I get 15mb down and 8mb up through my Time Warner Internet.  When connected to the 1142 I get 5mb down and .9 up. My network is flat and very simple.  I am

  • Slow 3G

    Got my 3G yesterday and I am a little puzzled. It is slower than my original iPhone when pulling up things like Photos, Settings, etc it takes about 5 seconds for the screen to appear. I don't remember having that happen with the old iPhone. Also, I