DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2006
    Posts
    28

    Text Being Written Over The Top Of Each Other

    Hi everyone,

    I have a simple applet to display the perfect numbers from 1-500...but when the numbers that are considered to be perfect are displayed, they are written over the top of each other...

    Ive used a simple for loop, but it doesnt seem to help...does anyone have any ideas as to what is going on?

    Thanks very much!

    Code:
    import java.applet.*;
    import java.awt.*;
     
    public class PerfectNumbers extends Applet {
     
        int maxNum=500;
        int perfectCount=0;
     
        public void paint (Graphics g){
        {        
            for(int ctr=maxNum;ctr>=2;ctr--)
            {
                int sum = 0;
                int sum2 = 0;
                for(int ctr2 = ctr; ctr2>=2; ctr2--)
                {
                    if(ctr%ctr2==0)
                    {
                        sum = sum+(ctr/ctr2);
                        sum2 = ctr/ctr2;
                    }
                }
            if(ctr==sum)
            {
                for (int i = 1; i<=4; i++)
                {
                    g.drawString(""+ctr,20,120+i*20);
                    i++;
                }
                perfectCount++;
                }            
            }
        }
        g.drawString("There are "+perfectCount+" Perfect numbers from 1-" +maxNum,20,60);    
    }
    }
    -Crawf

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    it's because you have two for loops.
    the inner loop for (int i = 1; i<=4; i++) which does the drawing is
    initialized allways equal when the outer loop iterates.
    try to draw the strings dependent on the variable of the outer loop,
    eg g.drawString(""+ctr,20,120+ctr*20); or sth like that.

  3. #3
    Join Date
    May 2006
    Posts
    28
    ah right! i see what you mean now! im pretty stupid! i was looking at it for ages trying to figure out that tiny little thing! I guess i must have been looking TOO hard!


    Thanks for your help graviton! you've helped lots, all is good now!

    -Crawf

Similar Threads

  1. Top fixed screen
    By Cheng in forum Web
    Replies: 1
    Last Post: 11-06-2001, 09:43 AM
  2. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  3. Double Text 1.0
    By George Gilbert in forum vb.announcements
    Replies: 0
    Last Post: 08-19-2001, 11:34 AM
  4. Printing text on top of grapics to the printer object
    By Sharon Thomas in forum VB Classic
    Replies: 4
    Last Post: 04-25-2000, 07:23 AM
  5. Printing text on top of grapics to the printer object
    By Sharon Thomas in forum VB Classic
    Replies: 0
    Last Post: 04-24-2000, 10:59 AM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links