The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet Redrawing Problems

5 replies on 1 page. Most recent reply: May 5, 2003 3:45 PM by Joe

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Joe

Posts: 3
Nickname: magueta
Registered: Apr, 2003

Applet Redrawing Problems Posted: Apr 24, 2003 8:29 PM
Reply to this message Reply
Advertisement
Hey all,

I'm just learning Java and I'm having problems drawing the GUI. If I try to put code to draw the GUI in the paint method the applet flickers and is unusable. Can someone please tell me what's wrong with the code? It's an applet that converts currency and allows you to edit the exchange rates. The code is below, I hope it keeps the formatting.

/* All packages are imported here. */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
/* Applet class*/
public class Currency extends Applet implements ActionListener, ItemListener {
    
    /* All variables, buttons, and fields are declared here. */
    private int Iface;
    private double usExchRate, euroExchRate, pesoExchRate, poundExchRate, rubleExchRate, canExchRate;
    private Choice ICurrency, FCurrency;
    private Button exchRatesIface, convIface; /* convert, */ 
    private TextField currInput, currOutput, usExch, euroExch, pesoExch, poundExch, rubleExch, canExch;
    private Label LInitScreen, LUsExch, LEuroExch, LPesoExch, LPoundExch, LRubleExch, LCanExch, LBlank;
    private Panel init = new Panel();
    private Panel con1 = new Panel();
    private Panel con2 = new Panel();
    private Panel ex1 = new Panel();
    private Panel ex2 = new Panel();
    private String ChoiceIn, ChoiceOut;
     
    /* Init method that every applet needs */
    public void init(){
                    
        /* Create and add contents of ListBox for input currency */
        ICurrency = new Choice();
        ICurrency.add("From");
        ICurrency.add("Canadian Dollar"); ICurrency.add("U.S. Dollar"); 
        ICurrency.add("Mexican Peso"); ICurrency.add("British Pound"); 
        ICurrency.add("Russian Ruble"); ICurrency.add("Euro");
        ICurrency.addItemListener(this);
        
        /* Create and add contents of ListBox for output currency */
        FCurrency = new Choice();
        FCurrency.add("To");
        FCurrency.add("Canadian Dollar"); FCurrency.add("U.S. Dollar"); 
        FCurrency.add("Mexican Peso"); FCurrency.add("British Pound"); 
        FCurrency.add("Russian Ruble"); FCurrency.add("Euro");
        FCurrency.addItemListener(this);
        
        /* Create buttons */
        //convert = new Button("Convert"); 
        exchRatesIface = new Button("Set Exchange Rates");
        convIface = new Button("Calculate Funds Conversion");
        
        /* Initialize exchange rates.  These are not final variables because they will change.
         * This rate is to convert to Canadian dollars.*/
        usExchRate = 0.68; euroExchRate = 0.75; pesoExchRate = 100; poundExchRate = 0.25;
        rubleExchRate = 1000; canExchRate = 1;
        
        /* Create TextFields for applet */
        currInput = new TextField(10); currOutput = new TextField(10); usExch = new TextField(5);
        euroExch = new TextField(5); pesoExch = new TextField(5); canExch = new TextField(5);
        poundExch = new TextField(5); rubleExch = new TextField(5);
        currInput.setText("0");
        currOutput.setText("0");
        
        /* Create labels for applet */
        LCanExch = new Label("Canadian Exchange Rate", Label.CENTER);
        LUsExch = new Label("U.S. Exchange Rate", Label.CENTER); 
        LEuroExch = new Label("Euro Exchange Rate", Label.CENTER);
        LPesoExch = new Label("Peso Exchange Rate", Label.CENTER); 
        LPoundExch = new Label("Pound Exchange Rate", Label.CENTER);
        LRubleExch = new Label("Ruble Exchange Rate", Label.CENTER);
        LBlank = new Label("", Label.CENTER);
        LInitScreen = new Label("What do you want to do?", Label.CENTER);
        
        /* Add the itemListener to all the buttons and TextFields */
        //convert.addActionListener(this); 
        exchRatesIface.addActionListener(this);
        convIface.addActionListener(this); currInput.addActionListener(this);
        usExch.addActionListener(this); euroExch.addActionListener(this); 
        pesoExch.addActionListener(this); poundExch.addActionListener(this); 
        rubleExch.addActionListener(this);
        
        /* create the initial screen */
        initialScreen();
        //converter();
        //exchange();
    }
    
    /* Method called to draw the first screen */
    public void initialScreen(){ // Iface = 1
        Iface = 1; 
        add(init);
        init.setLayout(new GridLayout(3,1));
        init.add(LInitScreen);
        init.add(convIface);
        init.add(exchRatesIface);
    }
    
    /* Method called to add appropriate pieces to Exchange Rates interface */
    public void exchange(){  // Iface = 2
        Iface = 2;
        add(ex1); add(ex2);
        ex1.setLayout(new GridLayout(6,2));
        ex1.add(LCanExch); ex1.add(canExch);
        ex1.add(LUsExch); ex1.add(usExch);
        ex1.add(LEuroExch); ex1.add(euroExch);
        ex1.add(LPesoExch); ex1.add(pesoExch);
        ex1.add(LPoundExch); ex1.add(poundExch);
        ex1.add(LRubleExch); ex1.add(rubleExch);
        ex2.add(convIface);
        
        // display all the current exchange rates
        canExch.setText(canExchRate + "");
        usExch.setText(usExchRate + "");
        euroExch.setText(euroExchRate + "");
        pesoExch.setText(pesoExchRate + "");
        poundExch.setText(poundExchRate + "");
        rubleExch.setText(rubleExchRate + "");
        
    }    
    
    /* Method called to add appropriate pieces to Converter interface */
    public void converter(){  // Iface = 3
        Iface = 3;
        add(con1); add(con2);
        con1.setLayout(new GridLayout(2,2));
        con1.add(ICurrency);
        con1.add(currInput);
        con1.add(FCurrency);
        con1.add(currOutput);
        con2.add(exchRatesIface);
    }
    
    /* Implementation of ActionListener */
    public void actionPerformed(ActionEvent ae) {  // used for buttons and TextFields
        Object source = ae.getSource(); double val = 0.0;
        
        /* the next four "if"s are for changing interfaces */
        if (source == exchRatesIface){
            if (Iface == 1){
                remove(init);
                exchange();
            }
            else{
                remove(con1);
                remove(con2);
                exchange();
            }
        }
        if (source == convIface){
            if(Iface == 1){
                remove(init);
                converter();
            }
            else{
                remove(ex1);
                remove(ex2);
                converter();
            }
        }
        /* end of "if"s for changing interfaces */
        
        /* this section is for setting the exchange rates */
        /*c.add(LCanExch); c.add(canExch);
        c.add(LUsExch); c.add(usExch);
        c.add(LEuroExch); c.add(euroExch);
        c.add(LPesoExch); c.add(pesoExch);
        c.add(LPoundExch); c.add(poundExch);
        c.add(LRubleExch); c.add(rubleExch);*/
        if (source == canExch){
            return;
            //repaint();
        }
        if (source == usExch){
            usExchRate = Double.parseDouble(usExch.getText());
            //repaint();
        }
        if (source == euroExch){
            euroExchRate = Double.parseDouble(euroExch.getText());
            //repaint();
        }
        if (source == pesoExch){
            pesoExchRate = Double.parseDouble(pesoExch.getText());
            //repaint();
        }
        if (source == poundExch){
            poundExchRate = Double.parseDouble(poundExch.getText());
            //repaint();
        }
        if (source == rubleExch){
            rubleExchRate = Double.parseDouble(rubleExch.getText());
            //repaint();
        }
        // End of setting the exchange rates
        
        /* These "if"s are for conversion and indicate when a value has been entered
         * in the "to" TextField */
        if (source == currInput){  // a value has been entered for conversion
            try {
                val = Double.parseDouble(currInput.getText());
            } catch(Exception e){
                val = 0.0;
            }
            
            //if (ChoiceIn.equals("Canadian Dollar")){
            //  currOutput.setText((val*canExchRate) + "");
            }
            if (ChoiceIn.equals("Canadian Dollar") && ChoiceOut.equals("Canadian Dollar")) {
                currOutput.setText(val + "");
            }
            if (ChoiceIn.equals("Canadian Dollar") && ChoiceOut.equals("U.S. Dollar")) {
                currOutput.setText(val * usExchRate + "");
            }
            if (ChoiceIn.equals("Canadian Dollar") && ChoiceOut.equals("Mexican Peso")) {
                currOutput.setText(val * pesoExchRate + "");
            }
            if (ChoiceIn.equals("Canadian Dollar") && ChoiceOut.equals("British Pound")) {
                currOutput.setText(val * poundExchRate + "");
            }
            if (ChoiceIn.equals("Canadian Dollar") && ChoiceOut.equals("Russian Ruble")) {
                currOutput.setText(val * rubleExchRate + "");
            }
            if (ChoiceIn.equals("Canadian Dollar") && ChoiceOut.equals("Euro")) {
                currOutput.setText(val * euroExchRate + "");
            }
        // Here's the U.S. Dollar
            if (ChoiceIn.equals("U.S. Dollar") && ChoiceOut.equals("Canadian Dollar")){
                currOutput.setText(val*1/usExchRate + "");
            }
            if (ChoiceIn.equals("U.S. Dollar") && ChoiceOut.equals("U.S. Dollar")){
                currOutput.setText(val + "");
            }
            if (ChoiceIn.equals("U.S. Dollar") && ChoiceOut.equals("Mexican Peso")){
                currOutput.setText(val*1/usExchRate*pesoExchRate + "");
            }
            if (ChoiceIn.equals("U.S. Dollar") && ChoiceOut.equals("British Pound")){
                currOutput.setText(val*1/usExchRate*poundExchRate + "");
            }
            if (ChoiceIn.equals("U.S. Dollar") && ChoiceOut.equals("Russian Ruble")){
                currOutput.setText(val*1/usExchRate*rubleExchRate + "");
            }
            if (ChoiceIn.equals("U.S. Dollar") && ChoiceOut.equals("Euro")){
                currOutput.setText(val*1/usExchRate*euroExchRate + "");
            }
        // Here's the Mexican Peso
            if (ChoiceIn.equals("Mexican Peso") && ChoiceOut.equals("Canadian Dollar")){
                currOutput.setText(val*1/pesoExchRate + "");
            }
            if (ChoiceIn.equals("Mexican Peso") && ChoiceOut.equals("U.S. Dollar")){
                currOutput.setText(val*1/pesoExchRate*usExchRate + "");
            }
            if (ChoiceIn.equals("Mexican Peso") && ChoiceOut.equals("Mexican Peso")){
                currOutput.setText(val + "");
            }
            if (ChoiceIn.equals("Mexican Peso") && ChoiceOut.equals("British Pound")){
                currOutput.setText(val*1/pesoExchRate*poundExchRate + "");
            }
            if (ChoiceIn.equals("Mexican Peso") && ChoiceOut.equals("Russian Ruble")){
                currOutput.setText(val*1/pesoExchRate*rubleExchRate + "");
            }
            if (ChoiceIn.equals("Mexican Peso") && ChoiceOut.equals("Euro")){
                currOutput.setText(val*1/pesoExchRate*euroExchRate + "");
            }
        // here's the British Pound
            if (ChoiceIn.equals("British Pound") && ChoiceOut.equals("Canadian Dollar")){
                currOutput.setText(val*1/poundExchRate + "");
            }
            if (ChoiceIn.equals("British Pound") && ChoiceOut.equals("U.S. Dollar")){
                currOutput.setText(val*1/poundExchRate*usExchRate + "");
            }
            if (ChoiceIn.equals("British Pound") && ChoiceOut.equals("Mexican Peso")){
                currOutput.setText(val*1/poundExchRate*pesoExchRate + "");
            }
            if (ChoiceIn.equals("British Pound") && ChoiceOut.equals("British Pound")){
                currOutput.setText(val + "");
            }
            if (ChoiceIn.equals("British Pound") && ChoiceOut.equals("Russian Ruble")){
                currOutput.setText(val*1/poundExchRate*rubleExchRate + "");
            }
            if (ChoiceIn.equals("British Pound") && ChoiceOut.equals("Euro")){
                currOutput.setText(val*1/poundExchRate*euroExchRate + "");
            }
        
        // Here's the Russian Ruble
            if (ChoiceIn.equals("Russian Ruble") && ChoiceOut.equals("Canadian Dollar")){
                currOutput.setText(val*1/rubleExchRate + "");
            }
            if (ChoiceIn.equals("Russian Ruble") && ChoiceOut.equals("U.S. Dollar")){
                currOutput.setText(val*1/rubleExchRate*usExchRate + "");
            }
            if (ChoiceIn.equals("Russian Ruble") && ChoiceOut.equals("Mexican Peso")){
                currOutput.setText(val*1/rubleExchRate*pesoExchRate + "");
            }
            if (ChoiceIn.equals("Russian Ruble") && ChoiceOut.equals("British Pound")){
                currOutput.setText(val*1/rubleExchRate*poundExchRate + "");
            }
            if (ChoiceIn.equals("Russian Ruble") && ChoiceOut.equals("Russian Ruble")){
                currOutput.setText(val + "");
            }
            if (ChoiceIn.equals("Russian Ruble") && ChoiceOut.equals("Euro")){
                currOutput.setText(val*1/rubleExchRate*euroExchRate + "");
            }
        
        // Here's the European Economic Union's Euro
            if (ChoiceIn.equals("Euro") && ChoiceOut.equals("Canadian Dollar")){
                currOutput.setText(val*1/euroExchRate + "");
            }
            if (ChoiceIn.equals("Euro") && ChoiceOut.equals("U.S. Dollar")){
                currOutput.setText(val*1/euroExchRate*usExchRate + "");
            }
            if (ChoiceIn.equals("Euro") && ChoiceOut.equals("Mexican Peso")){
                currOutput.setText(val*1/euroExchRate*pesoExchRate + "");
            }
            if (ChoiceIn.equals("Euro") && ChoiceOut.equals("British Pound")){
                currOutput.setText(val*1/euroExchRate*poundExchRate + "");
            }
            if (ChoiceIn.equals("Euro") && ChoiceOut.equals("Russian Ruble")){
                currOutput.setText(val*1/euroExchRate*rubleExchRate + "");
            }
            if (ChoiceIn.equals("Euro") && ChoiceOut.equals("Euro")){
                currOutput.setText(val + "");
            }
        // End of conversion "if"s
        
    }
    
    // implementation of the itemListener Interface
    public void itemStateChanged(ItemEvent ie){  // used for lists, and combo boxes
        Object source = ie.getSource();
        
        if (source == ICurrency){
            ChoiceIn = ICurrency.getSelectedItem();
        }
        else{ ChoiceOut = FCurrency.getSelectedItem();
        }
        
        
    }
    
    /* Paint method that every applet needs */
    //public void paint(Graphics g){
     
    //}
}


Joe

Posts: 3
Nickname: magueta
Registered: Apr, 2003

Re: Applet Redrawing Problems Posted: Apr 24, 2003 10:51 PM
Reply to this message Reply
I found the answer, you're supposed to use the method "validate()" instead of "repaint()"

Joe

Ajay

Posts: 8
Nickname: aju
Registered: Apr, 2003

Re: Applet Redrawing Problems Posted: Apr 25, 2003 12:26 AM
Reply to this message Reply
no joe validate wont reduce the number flickerence. its same as the repaint.. pls reply if u know any more opening for this ans. I also actually looking for this issue

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Applet Redrawing Problems Posted: Apr 25, 2003 3:23 AM
Reply to this message Reply
double buffering might be a solution to your problems.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Applet Redrawing Problems Posted: Apr 25, 2003 4:37 AM
Reply to this message Reply
http://koala.ilog.fr/jml/java/tricks/double-buffering.html

Joe

Posts: 3
Nickname: magueta
Registered: Apr, 2003

Re: Applet Redrawing Problems Posted: May 5, 2003 3:45 PM
Reply to this message Reply
Validate() worked great for me. Rather than calling repaint() which I didn't have to do anyway because I didn't even implement it, the validate() call fixed the problems. I got a good mark on the assignment so it must have worked alright. I only lost marks for some code redundancy. The instructor said I could have put the exchange rates in an array and saved myself some time.

Joe

Flat View: This topic has 5 replies on 1 page
Topic: Java mail questions.......need help Previous Topic   Next Topic Topic: don't understand the problem - using methods

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use