How can i fix this problem!!!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 3
Reputation: ranindu is an unknown quantity at this point 
Solved Threads: 0
ranindu ranindu is offline Offline
Newbie Poster

How can i fix this problem!!!

 
0
  #1
May 6th, 2007
/*
<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[i]= new JButton(labels[i]);
buttons[i].setBackground(colors[i]);
buttons[i].setSize(115,30);
thePanel.add(buttons[i]);
buttons[i].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[i]= new JMenuItem(labels[i]);
editMenu.add(items[i]);
items[i].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[i] );

g2d.fill3DRect( x_gap , y_gap+((bar_height+gap_between_bars)*(i)), bar_width[i], bar_height, true);
g.drawString(hits[i] + ", " + percentage[i]+"%",bar_width[i]+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[i]=hits[i];// set hit=pixel
total += hits[i];


}



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

}




}



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[i] < 'A' || chr[i] > 'Z') && (chr[i] < 'a' || chr[i] > 'z')){
if (!(chr[i]==' ')) { // check for space
blnAlpha = false;
//System.out.println(">>" + chr[i] + ">>");
}
}
}
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[i]){
Color c= JColorChooser.showDialog(Assign2.this, "Choose a color for " + labels[i],colors[i]);
if (c!=null){
colors[i] = c;
buttons[i].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[i]){
String label = JOptionPane.showInputDialog(Assign2.this, "Edit Department Name - "
+ labels[i] ,"Department Name",1);
if(label!= null){
if(isAlpha(label)) {
labels[i]=label;
items[i].setText(label); //set menu label
buttons[i].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[i]){
try {
int h= Integer.parseInt (JOptionPane.showInputDialog(Assign2.this,"Edit Department Hits - " +
labels[i],"Department Hits",1));
if ((h<=250) && (h>0)) {
hits[i] = 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[i]) {
if (hits[i]>0 && hits[i] < max_bar_width) {
hits[i]++;
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???
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How can i fix this problem!!!

 
0
  #2
May 7th, 2007
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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC