here is the code.... below is my problem.............

<applet code="Assign2.class" width=350 height=350>

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


Class Name - Assign2


private JButton buttons[] // array to store buttons
private String labels[]  // array to stoer the department name
private JPanel thePanel; // JPanel object
private JMenu editMenu,optionsMenu; //JMenu objects for EDIT menu and OPTIONS menu
public JMenuItem items[]  //array to store 'department name' menus inside EDIT menu
private JRadioButton options[] // array to store Color, Text, Hits options as radio buttons
private JMenuBar menubar; // MAIN MENU for the applet
private Color colors[] // array to hold colors the given colors

private final int max_bar_width // maximum width of a bar- set to 250, cna not be changed
private final int bar_height // maximum height of a bar
private int bar_width[] // array to store the bar widths for each department, can be changed by increasing the hits, 1 hit= 1 pixel
private int hits[]// array to store the number of hits for each department
private double percentage[] // array to store the number of percentage for each department

private int x_gap // x co-ordinate of each bar
private int y_gap / y co-ordinate of each bar
private int gap_between_bars // the height between each bar when displayed in the applet
private String stat // string that states the current status of the applet

private int option // helps to choose between the 3 options, "Color, Text, Hits"
// Color =0, Text=1, Hits= 2

private class MenuHandler // inner class to handle menus. Listens to menu events
private class OptionHandler // inner class to handle Options in option menu.Listens to option events
private class ButtonHandler // inner class to handle buttons. Listens to button events


# public void init() - Initializes the applet and draws the buttons and the menus
# public void paint()
# public void update()
# public void paintBars() - draws the horizontal bars for each department
# public void calculateChanges() - increases the number of hits,calculates the total no of hits and calculate the percentages
# public boolean isAlpha() - returns true or false. A method written to check if a given input string contains only characters
# public void showStatus() - Shows the current staus of the applet and used to diaply error message on the message bar of the applet

public class Assign2 extends JApplet {

private JButton buttons[]=new JButton[5];
private String labels[]= {"Footwear","Kitchen Goods","Stationery","Music","Ladies Wear"};
private JPanel thePanel;
private JMenu editMenu,optionsMenu;
public JMenuItem items[]=new JMenuItem[5];
private JRadioButton options[]= new JRadioButton[3];
private JMenuBar menubar;
private Color colors[]= {Color.yellow,,Color.magenta,Color.cyan,};

private int max_bar_width=250;
private int bar_height= 25;
private int bar_width[]= {100,100,100,100,100};
private int hits[]= {200,200,200,200,200};
private double percentage[]={80.00,80.00,80.00,80.00,80.00};

private int x_gap= 15;
private int y_gap=35;
private int gap_between_bars=15;
private String stat= "Applet started!";

private int option=0;

public void init(){
Container pane = getContentPane();// returns the ContentPane
thePanel = new JPanel(); // creates panel object
thePanel.setLayout(null); // setting the layout for the panel

menubar = new JMenuBar(); // creating a menu bar object
setJMenuBar(menubar); // addind the menu bar to the applet
editMenu = new JMenu ("Edit");
menubar.add(editMenu);  // adding a the EDIT menu to the menu bar
optionsMenu = new JMenu("Options");  // creating th OPTIONS menu

for (int i=0; i <5;i++) { // the for loop create a button for each dept, sets the colors, set the size and add the buttons to the panel
buttons= new JButton(labels);// creates a new button object
buttons.setBackground(colors); // sets the color
buttons.setSize(115,30);// Sets the size
thePanel.add(buttons);// add the button to the panel
buttons.addActionListener(new ButtonHandler());  // adding a listener object to listen to button events for each button

buttons[0].setLocation(0,260); // set button location

for (int i=0; i <5;i++) {
items= new JMenuItem(labels); // create each menu item and setting its label
editMenu.add(items); // add the menu items to the EDIT menu
items.addActionListener(new MenuHandler()); //adding a listener object to listen to menu events
editMenu.add(optionsMenu);  // adding  the OPTIONS menu to the EDIT menu

ButtonGroup group = new ButtonGroup(); // creating a group to hold the 3 radio buttons for options

options[0]=new JRadioButton("Color"); // creating a radio button for COLOR option
options[0].setSelected(true);  // setting the Color option to be selected as default
options[0].addActionListener(new OptionHandler());  // adding a listener object to listen to option events
options[1]=new JRadioButton("Text");// creating a radio button for TEXT option
options[1].setSelected(false);// setting the Text option to be unselected as default
options[1].addActionListener(new OptionHandler());// adding a listener object to listen to option events

options[2]=new JRadioButton("Hits");// creating a radio button for HITS option
options[2].setSelected(false);// setting the Hits option to be unselected as default
options[2].addActionListener(new OptionHandler());// adding a listener object to listen to option events

group.add(options[0]); // add options to the group
optionsMenu.add(options[0]); // add option radio buttons to the OPTIONS menu

pane.add(thePanel);  // add the panel to the container

public void paint(Graphics g){
super.paint(g);  // calls the components paint method

public void update(Graphics g){


public void paintBars(Graphics g){ // method that draws the bars
Graphics2D g2d = ( Graphics2D ) g; // creating a 2d graphics object

for (int i=0;i<5;i++) { //paint bars
g2d.setPaint( colors ); // set the color for each bar
/* the filling area for each bar is calculated as follows
fill3DRect(int x, int y, int width, int height)
int x = x_gap ( the x co-ordinate of the bar)
int y= y co-ordinate of the bar + (bar height + the distance between 2 bars)* bar number
i.e ; for bar 1 --->  just the y co-ordinate
for bar 2 ----> y co_ordinate + (bar height of bar 1 + the distance betwen bar1 and bar2 ) * 1 etc

drawString (String message, int x, int y)
String message = No of hits and the Percentage for each department
int x = bar width of each dept + 10 pixels( for clarity)+ x -co-ordinate of bar position
int y = y co-ordinate of the bar + bar height(cos the string is dispalyed at the bottom of the bar)+ ( bar height + the distance between 2 bars)
i.e ; for bar 1 --->  just the y co-ordinate
for bar 2 ----> y co_ordinate + bar height of 1 +(bar height of bar 1 + the distance betwen bar1 and bar2 ) * 1 etc

g2d.fill3DRect( x_gap , y_gap+((bar_height+gap_between_bars)*(i)), bar_width, bar_height, true);// Paints the rectangle filled with the current color.
g.drawString(hits + ", " + percentage+"%",bar_width+x_gap+10,y_gap + bar_height + ((bar_height+gap_between_bars)*(i)));// prints the number of hits and precentage.

} // end of paintbars

public void calculateChanges(){// method that calculates the total no of hits and calculate the percentages

int total =0;// initialize total to 0
for(int i=0; i<5; i++) {
bar_width=hits;// set hit=pixel
total += hits;


//calculating percentages
for (int i=0 ; i<5;i++) {
percentage=(hits*100.0) / total;
buttons.setText(labels); // set label for button
buttons.setBackground(colors); //set button background
items.setText(labels); //set menu item labels

}// end for

}// end of calculatechanges

public boolean isAlpha(String str) {
//input: string, output: true/false
//Loops therough the character array and return false if invalid character found.

boolean blnAlpha = true;// define and initialize blnAlpha

char chr[] = null;// define char array and initialize to null
if(str != null)// check whether the  string is null
chr = str.toCharArray();// break the string and insert into the chr array

for(int i=0; i<chr.length; i++) {

if((chr < 'A' || chr > 'Z') && (chr < 'a' || chr > 'z')){
if (!(chr==' ')) { // check for space
blnAlpha = false;
//System.out.println(">>" + chr + ">>"); // when debugging
return blnAlpha;
}//end isAlpha

//inner class to handle menus
private class MenuHandler implements ActionListener{
public void actionPerformed (ActionEvent event){
if (option==0){
for (int i=0; i<5;i++){
Color c= JColorChooser.showDialog(Assign2.this, "Choose a color for " + labels,colors);
if (c!=null){
colors = c; //set new color to array
buttons.setBackground(c); //change button background color

}//check for c being null


}//end for

}//enf of option==0
if (option==1){
for (int i=0; i <5; i++){
String label = JOptionPane.showInputDialog(Assign2.this, "Edit Department Name - "
+ labels ,"Department Name",1);
if(label!= null){
if(isAlpha(label)) {
items.setText(label); //set menu label
buttons.setText(label); //set button label
showStatus("Make sure you've ONLY entered characters!!");



}// get source


}//end of option==1

for(int i=0; i < 5; i++){
try {
int h= Integer.parseInt (JOptionPane.showInputDialog(Assign2.this,"Edit Department Hits - " +
labels,"Department Hits",1));
if ((h<=250) && (h>0)) { //check for validity
hits = h; //assign new value to hits array
} else {
showStatus("Hits should be between 1 and 250!"); //show error

} catch(NumberFormatException e){

showStatus("You must input a number!!");


}// getting soure

}//end for
}//end of option==2

}//end of actionPerformed

}//end of MenuHandler

private class OptionHandler implements ActionListener {

// respond to checkbox events
public void actionPerformed( ActionEvent e )
//showStatus("Applet Running!");

if ( e.getSource() == options[0] ) { // option Color clicked
option = 0;
else if ( e.getSource() == options[1]) { //Option Text Clicked
option = 1;
else if ( e.getSource() == options[2] ) { //Option Hits Clicked
option = 2;
}//end action performed
} // OptionListner

class ButtonHandler implements ActionListener {

// respond to checkbox events
public void actionPerformed( ActionEvent e ) {
showStatus("Applet Running!");

for (int i=0;i<5;i++) {
if (e.getSource()==buttons) { //loop until correct button is found
if (hits>0 && hits < max_bar_width) { //hits cant be more than 250
hits++; //increment hits
calculateChanges(); //calculate new bar widths
showStatus("Hits have to be between 1 and 250!"); //give error
}//end for
}//end action performed
} // ButtonHandler

public void showStatus(String Status) {
stat = Status;
}//end showStatus

} //end Assign2

here when you run the applet and click on the menu, its hidden Beneath the bars that are drawn. but once you click on a menu item and come back it works fine... what is the problem with the code ?? how can i fix this??? :(

Member Avatar for iamthwee

Use code tags not many ppl will be able to read that coherently.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.