1
import java.awt.*;
import java.applet.*;

public class oef2ap extends Applet {

    public void paint(Graphics g){
        int x;
        int y;
        int width;
        int height;
        int teller;
        width=10;
        height=10;    
        teller= 0;
        for(x=0;x<10;x++)
        {
            for(y=0;y<10;y++)
            {
                teller = teller + 1;
                g.drawRect(x*width,y*height,width,height);
                g.drawString(String.valueOf(teller), x, y);
            }

        } 
    }
}

This is my code but the g.drawstring doesn't give me what I want , it needs to put a ordered number from 1 to 100 in each rect.

fmodos
  • 4,472
  • 1
  • 16
  • 17
Bondjens
  • 55
  • 7
  • *"it needs to put a ordered number from 1 to 100 in each rect."* I'd do that using a 10x10 `GridLayout` showing either labels (no interaction) or buttons (interaction). – Andrew Thompson Feb 20 '14 at 03:03
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Feb 20 '14 at 03:04

2 Answers2

3

did you forget multiply x,y in drawString ?

g.drawString(String.valueOf(teller), x*width, y*height);
karci10
  • 375
  • 3
  • 15
0

Couple of issues:

1) You're not adjusting for the height location in when calling drawString you need to x & y by width and height respectively:

g.drawString(String.valueOf(teller), x * width, (y * height);

2) You also need to adjust the height downward again by height distance so your drawString ends up in the box:

g.drawString(String.valueOf(teller), x * width, (y * height)+height);

Putting it to work:

public class oef2ap extends Applet {
    public void paint(Graphics g) {
        int x;
        int y;
        int width;
        int height;
        int teller;
        width = 25;
        height = 25;
        teller = 1;
        for (x = 0; x < 10; x++) {
            for (y = 0; y < 10; y++) {
                g.drawRect(x * width, y * height, width, height);
                g.drawString(String.valueOf(teller), x * width, (y * height)+height);
                teller += 1;

            }
        }
    }
}

Generates this output:

applet output

Durandal
  • 5,575
  • 5
  • 35
  • 49
  • It needs to be from left to right ordered in the way like this --> 1 2 3 4 5 6 7 8 9 10 ( next row ) 11 12 13 14 15 .... – Bondjens Feb 24 '14 at 12:58