Results 1 to 2 of 2
  1. #1
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Question Help with moving a black fillRect across the screen (Applet)

    My code compiles and runs perfectly, however nothing appears on the screen when I run the applet. Here is the code:
    Java Code:
    //This applet displays a message moving horizontally
    //across the screen.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Bannerz extends JApplet
      implements ActionListener
    {
      private int xPos, yPos; //hold the coordinates of the banner
    
      public void init()
      {
        Container c=getContentPane();
        c.setBackground(Color.WHITE);
        xPos=c.getWidth();
        yPos=c.getHeight() /2;
        Timer clock=new Timer(30, this); //fires every 30 milliseconds
        clock.start();
      }
    
      //called automatically after a repaint request
      public void paint (Graphics g)
      {
        super.paint(g);
        g.setColor(Color.black);
        g.fillRect(80, 40, xPos, yPos);
      }
    
      //called automatically when the timer fires
      public void actionPerformed (ActionEvent e)
        {
          Container c=getContentPane();
    
          //Adjust the horizontal position of the banner:
          xPos--;
          if (xPos<-100)
           {
             xPos=c.getWidth();
           }
    
           //Set the vertical position of the banner:
           yPos=c.getHeight() /2;
    
           repaint();
         }
    }
    Last edited by HalfAZN; 08-27-2012 at 08:11 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with moving a black fillRect across the screen (Applet)

    Don't draw directly in the JApplet but rather draw in a JPanel's paintComponent method and have the JApplet display the JPanel in its contentPane.

Similar Threads

  1. Robot screen capture returns black image
    By Jaeela in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 09:51 AM
  2. Ball moving across the screen?
    By Slicknife in forum New To Java
    Replies: 1
    Last Post: 05-29-2012, 11:53 PM
  3. problem about Unknown Black Screen
    By lawlawlaws in forum Java 2D
    Replies: 1
    Last Post: 02-02-2012, 02:08 AM
  4. Problem about Black Screen
    By lawlawlaw in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 04:18 PM
  5. Moving an icon on screen?
    By TP-Oreilly in forum New To Java
    Replies: 5
    Last Post: 12-28-2011, 11:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •