User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,269 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,440 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 633 | Replies: 1
Reply
Join Date: May 2007
Posts: 3
Reputation: ranindu is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ranindu ranindu is offline Offline
Newbie Poster

Help How can i fix this problem!!!

  #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???
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: How can i fix this problem!!!

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:24 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC