Click to See Complete Forum and Search --> : some silly simple mistake..


confused
April 27th, 2000, 11:22 PM
what is wrong with this program??
it says error in line 57: type expected. i have pointed to the error next to it.i am trying to draw a box as an array horizontally on the left of the screen.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.Math; //to get a random no

public class adB7 extends Applet{

private AddBox newAddBox;
int Width=100,Height=100,posX=120,posY=120;

int a, temp,temp1,temp2,posX1, posX2;
Font courier12i = new Font("Courier",Font.ITALIC,12);
Font timesroman12p = new Font("TimesRoman",Font.PLAIN,12);//defining 3 font styles
Font aerial12b=new Font("Aerial",Font.BOLD,12);

//***********class adbox starts here***************////
class AddBox {
private AddBox[] boxArray = new AddBox[4] ; // declare the array of object--box
private int temp1 = 120 + (int)(Math.random() * 60 + 1); // the size of the boxes are randomly determined
//private int temp2 = 120 + (int)(Math.random() * 60 + 1);

public Font myFont;
public int posXVal;
public int posYVal;
public int widthVal;
public int heightVal;
public boolean flag;

public AddBox(){ ///default constructor
posXVal=40;
posYVal=100;
//widthVal=100;
//heightVal=100;
//private int blue=150;
flag=true;
int temp=(int) ((Math.random() *10) %8); //this will give a randon no between 0 and 2
}
public AddBox(Graphics g, int PosX, int PosY, int Width, int Height, boolean flag){
posXVal=PosX;
posYVal=PosY;
temp1=Width;
temp1=Height;
//temp1=Height;
}

//********************
//describing the method
public void paint(Graphics g){
//for(a=0;a<4;a++){
//AddBox.draw( g,40,100,temp1,temp1,true);//here we use temp1 to determine box size
//posX1=posX+temp1; //keeping 40 pixels for the top error display area as seperate
//posX=posX1+5;//keeping a space of 5 between adjoining adBoxes
//a +=1 ;//draws the box 5 times
//}
}
for(int a=0;a<4;a++){//>>>>>>>it says type expected for this line why??
posX1=posX+temp1;
posX=posX1+5;

private void draw(Graphics g, int posX, int posY, int width, int height, boolean flag){
if(flag==true)
{
switch (temp)
{
case 1:g.setFont(courier12i);break;
case 2:g.setFont(timesroman12p);break;
case 3:g.setFont(aerial12b);break;
default:g.setFont(timesroman12p);break;
}

g.setColor(Color.blue);//this gives the initial values
g.fillRect(posX,posY,width,height); //this will use the parameters passed in the method
g.setColor(Color.yellow);
g.fillRect((posX+(width*6/9)+(width*1/7)),(posY+(height/800)+(height*2/3)), height*1/10, height*3/9); //rectangle formed in such a way as to change size with the change in box size
g.setColor(Color.black);
g.drawString("Work smart..", posX+10, posY+30);//text output: again at relative positions. these are relative to posX and posY, since they do not change in size

g.setColor(Color.red);
g.drawString("..with inet.",posX+10, posY+75);

}
//else if (!flag)
{
g.drawString("Error",10,50);
}
a +=1 ;//draws the box 5 times
}
}
public void display(Graphics g){
draw(g,posXVal,posYVal,temp1,temp2,true);
}//end of display method
}
}

April 28th, 2000, 01:38 AM
hai,

the for loop is not inside any method, it open outside the class thats why givein the error

regs,
bharath

Email To: bharath@vsofti.stph.net

April 28th, 2000, 04:42 PM
thanks Bharat,
i put he counter in the "draw" method. now it compiles, but doesnt show anything in the appletviewer.I think theres something
wrong where i have called Graphics g: i have put no method in the {} there. what do i need to put there? if not there, where is the
error? thanks again..

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.Math; //to get a random no

public class adB7 extends Applet{

private AddBox newAddBox;
int Width=100,Height=100,posX=120,posY=120;

int a, temp,temp1,temp2,posX1, posX2;
Font courier12i = new Font("Courier",Font.ITALIC,12);
Font timesroman12p = new Font("TimesRoman",Font.PLAIN,12);//defining 3 font styles
Font aerial12b=new Font("Aerial",Font.BOLD,12);

//***********class adbox starts here***************////
class AddBox {
private AddBox[] boxArray = new AddBox[4] ; // declare the array of object--box
private int temp1 = 120 + (int)(Math.random() * 60 + 1); // the size of the boxes are randomly determined
//private int temp2 = 120 + (int)(Math.random() * 60 + 1);

public Font myFont;
public int posXVal;
public int posYVal;
public int widthVal;
public int heightVal;
public boolean flag;

public AddBox(){ ///default constructor
posXVal=40;
posYVal=100;
//widthVal=100;
//heightVal=100;
//private int blue=150;
flag=true;
int temp=(int) ((Math.random() *10) %8); //this will give a randon no between 0 and 2
}
public AddBox(Graphics g, int PosX, int PosY, int Width, int Height, boolean flag){
posXVal=PosX;
posYVal=PosY;
temp1=Width;
temp1=Height;
//temp1=Height;
}

//********************
//describing the method
public void paint(Graphics g){ ////>>>>>>>>>>>>THIS METHOD HAS NOTHING IN IT. WHAT DO I PUT IN IT??
//for(a=0;a<4;a++){
//AddBox.draw( g,40,100,temp1,temp1,true);//here we use temp1 to determine box size
//posX1=posX+temp1; //keeping 40 pixels for the top error display area as seperate
//posX=posX1+5;//keeping a space of 5 between adjoining adBoxes
//a +=1 ;//draws the box 5 times
//}
}
private void draw(Graphics g, int posX, int posY, int width, int height, boolean flag){
for(int a=0;a<4;a++){//>>>>>>>it says type expected for this line why??
posX1=posX+temp1;
posX=posX1+5;
if(flag==true)
{
switch (temp)
{
case 1:g.setFont(courier12i);break;
case 2:g.setFont(timesroman12p);break;
case 3:g.setFont(aerial12b);break;
default:g.setFont(timesroman12p);break;
}

g.setColor(Color.blue);//this gives the initial values
g.fillRect(posX,posY,width,height); //this will use the parameters passed in the method
g.setColor(Color.yellow);
g.fillRect((posX+(width*6/9)+(width*1/7)),(posY+(height/800)+(height*2/3)), height*1/10, height*3/9); //rectangle formed in such a
way as to change size with the change in box size
g.setColor(Color.black);
g.drawString("Work smart..", posX+10, posY+30);//text output: again at relative positions. these are relative to posX and posY, since
they do not change in size

g.setColor(Color.red);
g.drawString("..with inet.",posX+10, posY+75);

}
//else if (!flag)
{
g.drawString("Error",10,50);
}
a +=1 ;//draws the box 5 times
}
}
public void display(Graphics g){
draw(g,posXVal,posYVal,temp1,temp2,true);
}//end of display method
}
}