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


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



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.red,Color.magenta,Color.cyan,Color.green};



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();
thePanel = new JPanel();
thePanel.setLayout(null);


menubar = new JMenuBar();
setJMenuBar(menubar);
editMenu = new JMenu ("Edit");
menubar.add(editMenu);
baroptionsMenu = new JMenu("Options");  menu


for (int i=0; i <5;i++) {
buttons= new JButton(labels);
buttons.setBackground(colors);
buttons.setSize(115,30);
thePanel.add(buttons);
buttons.addActionListener(new ButtonHandler());
}


buttons[0].setLocation(0,260);
buttons[1].setLocation(115,260);
buttons[2].setLocation(230,260);
buttons[3].setLocation(0,290);
buttons[4].setLocation(115,290);


for (int i=0; i <5;i++) {
items= new JMenuItem(labels);
editMenu.add(items);
items.addActionListener(new MenuHandler());
}
editMenu.add(optionsMenu);



ButtonGroup group = new ButtonGroup();


options[0]=new JRadioButton("Color");
options[0].setSelected(true);
options[0].addActionListener(new OptionHandler());
options[1]=new JRadioButton("Text");
options[1].setSelected(false);
options[1].addActionListener(new OptionHandler());


options[2]=new JRadioButton("Hits");
options[2].setSelected(false);
options[2].addActionListener(new OptionHandler());



group.add(options[0]);
group.add(options[1]);
group.add(options[2]);
optionsMenu.add(options[0]);
optionsMenu.add(options[1]);
optionsMenu.add(options[2]);


pane.add(thePanel);


}
public void paint(Graphics g){
super.paint(g);
calculateChanges();
paintBars(g);


}
public void update(Graphics g){
paint(g);


}



public void paintBars(Graphics g){
Graphics2D g2d = ( Graphics2D ) g;


for (int i=0;i<5;i++) {
g2d.setPaint( colors );


g2d.fill3DRect( x_gap , y_gap+((bar_height+gap_between_bars)*(i)), bar_width, bar_height, true);
g.drawString(hits + ", " + percentage+"%",bar_width+x_gap+10,y_gap + bar_height + ((bar_height+gap_between_bars)*(i)));
}



}



public void calculateChanges(){


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



}


for (int i=0 ; i<5;i++) {
percentage=(hits*100.0) / total;
buttons.setText(labels);
buttons.setBackground(colors);
items.setText(labels);


}



}


public boolean isAlpha(String str) {



boolean blnAlpha = true;


char chr[] = null;
if(str != null)
chr = str.toCharArray();



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 + ">>");
}
}
}
return blnAlpha;
}



private class MenuHandler implements ActionListener{
public void actionPerformed (ActionEvent event){
if (option==0){
for (int i=0; i<5;i++){
if(event.getSource()==items){
Color c= JColorChooser.showDialog(Assign2.this, "Choose a color for " + labels,colors);
if (c!=null){
colors = c;
buttons.setBackground(c);
repaint();



}//check for c being null



}//getsource



}//end for


}//end of option==0


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


}//else


}//if


}// get source


}//for


}//end of option==1


if(option==2){
for(int i=0; i < 5; i++){
if(event.getSource()==items){
try {
int h= Integer.parseInt (JOptionPane.showInputDialog(Assign2.this,"Edit Department Hits - " +
labels,"Department Hits",1));
if ((h<=250) && (h>0)) {
hits = h;
calculateChanges();
repaint();
} else {
showStatus("Hits should be between 1 and 250!"); //show error
}



} catch(NumberFormatException e){


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


}


}
}
}



}



}


private class OptionHandler implements ActionListener {



public void actionPerformed( ActionEvent e )
{
//showStatus("Applet Running!");


if ( e.getSource() == options[0] ) {
options[1].setSelected(false);
options[2].setSelected(false);
options[0].setSelected(true);
option = 0;
}
else if ( e.getSource() == options[1]) {
options[1].setSelected(true);
options[2].setSelected(false);
options[0].setSelected(false);
option = 1;
}
else if ( e.getSource() == options[2] ) {
options[1].setSelected(false);
options[2].setSelected(true);
options[0].setSelected(false);
option = 2;
}
}
}



class ButtonHandler implements ActionListener {



public void actionPerformed( ActionEvent e ) {
showStatus("Applet Running!");


for (int i=0;i<5;i++) {
if (e.getSource()==buttons) {
if (hits>0 && hits < max_bar_width) {
hits++;
calculateChanges();
repaint();
}else{
showStatus("Hits have to be between 1 and 250!"); //give error
}
}
}
}
}


public void showStatus(String Status) {
stat = Status;
getAppletContext().showStatus(stat);
}



}

copy this code and run the program..................
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??? :(

Please don't cross post, it is considered rude.

Please continue with the original thread: http://www.daniweb.com/techtalkforums/thread77555.html

And do what was asked of you there (namely repost your code using code tags, in that thread).

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.