Why does this code not work...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Why does this code not work...

Hybrid View

  1. #1
    Join Date
    Jul 2006
    Posts
    5

    Why does this code not work...

    As far as I know, this code is supposed to draw a black circle, and then slowly move it across the screen.. only problem is.. it doesn't move.. and on my computer, if i extend the applets screen, the ball dissapears and moves back if i shrink the applet back down.. if someone can help me id appreciate it..

    Code:
    // This applet displays a message moving horizontally
    // across the screen.
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Banner extends JApplet
    {
    
      public void init()
      {
        Container c = getContentPane();
        repaint();
      }
    
      public void paint(Graphics g)
      {
        int x = 10;
        int y = 10;
        super.paint(g);
        g.fillOval(x, y, 20, 20); 
        x++;
      }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The init method is only run once, during applet initialization and when the html page is refreshed. The paint method is only run after explicit calls to repaint() and when the screen updates. The x-position value is reset upon every call to the paint method.

    And last but not least; you must implement a thread or a ticker in order to do animation.
    eschew obfuscation

  3. #3
    Join Date
    Apr 2006
    Posts
    28
    This program is OK for your request, but if you want more amination ,
    you have to improve it!

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Banner1 extends JFrame
    {

    /** Creates a new instance of Banner1 */
    public Banner1()
    {
    add(new MoveCircle());
    }
    public static void main(String[] arg)
    {
    Banner1 frame=new Banner1();
    frame.setTitle("Moving Circle: Move Circle");
    frame.getIconImage();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(200,200,400,300);
    frame.setVisible(true);
    }

    }
    class MoveCircle extends JPanel implements ActionListener
    {
    private int x=(int)(Math.random()*getSize().width);
    private int y=(int)(Math.random()*getSize().height);
    private Timer timer=new Timer(50,this);
    public MoveCircle()
    {
    timer.start();
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    if(x<-20)
    {
    x=(int)(Math.random()*getSize().width);
    y=(int)(Math.random()*getSize().height);
    }
    else
    x-=10;
    if(y<-20)
    {
    x=(int)(Math.random()*getSize().width);
    y=(int)(Math.random()*getSize().height);
    }
    else
    y-=10;
    if(x%2==0 &&y%2==0)
    g.setColor(Color.BLUE);
    else if(x%2==0 &&y%2==1)
    g.setColor(Color.RED);
    else if(x%2==1 &&y%2==0)
    g.setColor(Color.PINK);
    else if(x%2==1 &&y%2==1)
    g.setColor(Color.GREEN);

    g.fillOval(x, y, 20, 20);

    }
    public void actionPerformed(ActionEvent actionEvent)
    {
    repaint();
    }

    }

Similar Threads

  1. Replies: 20
    Last Post: 10-21-2005, 10:53 AM
  2. Replies: 1
    Last Post: 08-25-2005, 11:19 PM
  3. Control Arrays in VB.NET
    By Gary Nelson in forum .NET
    Replies: 277
    Last Post: 10-01-2003, 12:00 AM
  4. Make VB code work in MS Access 2000?
    By Pihlis in forum VB Classic
    Replies: 2
    Last Post: 07-17-2002, 12:34 AM
  5. Code security
    By Rob Teixeira in forum .NET
    Replies: 5
    Last Post: 11-27-2001, 06:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center