×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    31

    Choicegroup + event listener

    Code:
    package animationMaker;
    
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.m2g.SVGImage;
    import javax.microedition.m2g.ScalableGraphics;
    import javax.microedition.m2g.ScalableImage;
    import javax.microedition.media.*;
    import javax.microedition.rms.*;
    import java.util.*;
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.io.*;
    import java.util.*;
    import javax.microedition.rms.*;
    import org.w3c.dom.Document;
    import org.w3c.dom.svg.SVGSVGElement;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.m2g.ScalableGraphics;
    import javax.microedition.m2g.ScalableImage;
    
    /**
     *
     * @author Shiro
     */
    public class EditChar extends Canvas implements CommandListener {
        
        private mainMenuMidlet midlet;
        private Display display;
        private Command bkCmd, sltCmd, textCommand;
        Form displayForm;
        Form form;
        ChoiceGroup sizeChoice;
        ChoiceGroup AnimChoice;
        private int x,y;
        character characterOne;
        character characterTwo;
        private ScalableImage background;
        private String text=" ";
        private String fontSize = "";
        private String fontColour = "";
        private CanvasList cl;
        private String addChoice="";
        
        //ALICE
        private Sprite alice; //Sprite: movement of individual graphic objects
        private Image image;
        private AliceThread aThread;
        
        public EditChar(String title, mainMenuMidlet m, Display d, ScalableImage background, String text, int x, int y, String fontSize, String fontColour,character chracterOne1, character characterTwo1,String addChoice) {
            
            this.x=x;
            this.y= y;
            characterOne=chracterOne1;
            characterTwo=characterTwo1;
            this.background= background;
            midlet=m;
            display= d;
            this.text=text;
            this.fontColour=fontColour;
            this.fontSize=fontSize;
            this.addChoice=addChoice;
            System.out.println("add choice in edit char"+ addChoice);
            System.out.println("edit char "+ characterOne.getCharacterHeadIndex());
            
            bkCmd = new Command("Back", Command.EXIT, 2);
            sltCmd = new Command("Done", Command.OK, 1);
            addCommand(bkCmd);
            addCommand(sltCmd);
            setCommandListener(this);
            setTitle(title);
            
            //alice
            try {
                image = Image.createImage("/picture/Tortoise1.png");
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            alice = new Sprite(image, 351/3, 126);
            
            x = getWidth();
            y = 50;
            aThread = new AliceThread();
            aThread.start();
        }
        
        class AliceThread extends Thread{
            
            public void run(){
                while (true) {
                    try{
                        sleep(100000);
                    }catch(InterruptedException ie)	{
                        ie.printStackTrace();
                    }
                    x -= 20;
                    if(x <= 0 - image.getWidth())
                        x = getWidth();
                    repaint();
                }
            }
        }
        
        public void commandAction(Command c, Displayable d) {
            
        }
        
        public void paint(Graphics g) {
            clearScreen(g);
            form= getForm();
            display.setCurrent(form);
            g.setColor(0xFFFFFF);
            
            //g.fillRect(60,100,getWidth(),getHeight());
        }
        
        Form getForm() {
            if (form == null) {
                System.out.println("HIHI");
                form = new Form("Option");
                
                sizeChoice = new ChoiceGroup("Character Size:", Choice.POPUP);
                sizeChoice.append("Small", null);
                sizeChoice.append("Medium",null);
                sizeChoice.append("Large", null);
                form.append(sizeChoice);
                
                AnimChoice = new ChoiceGroup("Animation Choice", Choice.POPUP);
                AnimChoice.append("Walk", null);
                AnimChoice.append("Jump",null);
                System.out.println(characterOne.getSize()+" WAN ting");
                if(addChoice.equalsIgnoreCase("character1")) {
                    
                    System.out.println("it is coming here character1");
                    if(characterOne.getSize()==0 ) {
                        sizeChoice.setSelectedIndex(0,true);
                    }else if(characterOne.getSize()==1) {
                        sizeChoice.setSelectedIndex(1,true);
                    }else if(characterOne.getSize()==2) {
                        sizeChoice.setSelectedIndex(2,true);
                    }
                    
                    if(characterOne.getMovement()==0) {
                        AnimChoice.setSelectedIndex(0,true);
                    }else if(characterOne.getMovement()==1){
                        AnimChoice.setSelectedIndex(1,true);
                    }
                    
                }else if(addChoice.equalsIgnoreCase("character2")) {
                    
                    
                    System.out.println("it is coming here character2");
                    if(characterTwo.getSize()==0 ) {
                        sizeChoice.setSelectedIndex(0,true);
                    }else if(characterTwo.getSize()==1) {
                        sizeChoice.setSelectedIndex(1,true);
                    }else if(characterTwo.getSize()==2) {
                        sizeChoice.setSelectedIndex(2,true);
                    }
                    
                    if(characterTwo.getMovement()==0) {
                        AnimChoice.setSelectedIndex(0,true);
                    }else if(characterTwo.getMovement()==1){
                        AnimChoice.setSelectedIndex(1,true);
                    }
                }
                
                form.append(AnimChoice);       
                form.addCommand(bkCmd);
                form.addCommand(sltCmd);
                form.setCommandListener(this);
                form.append(new SimpleItem("Animation Preview"));
            }
            return form;
        }
        
        private void clearScreen(Graphics g) {
            g.setColor(0xFFFFFF);
            g.fillRect(0,0,getWidth(),getHeight());
        }
        
        
         class SimpleItem extends CustomItem {
     
      public SimpleItem(String title) { super(title); }
      
      // CustomItem abstract methods.
      
      public int getMinContentWidth() { return 200; }
      public int getMinContentHeight() { return 150; }
      
      public int getPrefContentWidth(int width) {
        return getMinContentWidth();
      }
      
      public int getPrefContentHeight(int height) {
        return getMinContentHeight();
      }
      
      
      
      public void paint(Graphics g, int w, int h) {
        g.setColor(0xFFFFFF);
            g.fillRect(10,6,getWidth(),getHeight());
            alice.setRefPixelPosition(10,6);
            alice.nextFrame();
            alice.paint(g);
            
            repaint();
      }
     }
    
    }
    Hi, this is my class.
    when chociegroup change index, i want my threading image to change.
    how should i go about doing it?
    thanks for the help =D

  2. #2
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    166

    Re: Choicegroup + event listener

    try implementing itemStateListner and override the required method and in that method if(item==choicegroup)then change image you need not use canvas and make the code complicated with the above method code will be quite simple.
    hope this gives what u want

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    31

    Re: Choicegroup + event listener

    Hi i need to use a CustomItem to draw as it is a threading, i use the itemListener, i have no idea how to let the customItem to show the new thread.
    Any suggestion?

    Code:
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Gauge;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemStateListener;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.m2g.SVGImage;
    import javax.microedition.m2g.ScalableGraphics;
    import javax.microedition.m2g.ScalableImage;
    import javax.microedition.media.*;
    import javax.microedition.rms.*;
    import java.util.*;
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.io.*;
    import java.util.*;
    import javax.microedition.rms.*;
    import org.w3c.dom.Document;
    import org.w3c.dom.svg.SVGSVGElement;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.m2g.ScalableGraphics;
    import javax.microedition.m2g.ScalableImage;
    
    public class GaugeTracker extends Canvas implements ItemStateListener, CommandListener {
        private Gauge mGauge = new Gauge("GaugeTitle", true, 5, 3);
        private ChoiceGroup sizeChoice;
        private ChoiceGroup AnimChoice;
         private Sprite alice,alice2; //Sprite: movement of individual graphic objects
        private Image image;
        private AliceThread aThread;
        private StringItem mStringItem = new StringItem(null, "[value]");
            private StringItem mStringItem1 = new StringItem(null, "[value]");
            private int x,y;
              private Display display;
                Form form;
                int AnimChoiceindex=0;
            
        public GaugeTracker(HelloMidlet midlet, Display display) {
            System.out.println("HELLO");
            itemStateChanged(mGauge);
            
            this.display=display;
           
            
           // Display.getDisplay(this).setCurrent(form);
            
        }
        
         class AliceThread extends Thread{
            
            public void run(){
                while (true) {
                    try{
                        sleep(300);
                    }catch(InterruptedException ie)	{
                        ie.printStackTrace();
                    }
                    x -= 20;
                    if(x <= 0 - image.getWidth())
                        x = getWidth();
                }
            }
        }
        
         public void paint(Graphics g)
         {
               form= getForm();
            display.setCurrent(form);
             
         }
         
         Form getForm() {
             Form form = new Form("GaugeTracker");
            form.addCommand(new Command("Exit", Command.EXIT, 0));
            form.setCommandListener(this);
            // Now add the selected items.
            
            sizeChoice = new ChoiceGroup("Character Size:", Choice.POPUP);
            sizeChoice.append("Small", null);
            sizeChoice.append("Medium",null);
            sizeChoice.append("Large", null);
            form.append(sizeChoice);
             form.append(mStringItem1);
            
            AnimChoice = new ChoiceGroup("Animation Choice", Choice.POPUP);
            AnimChoice.append("Walk", null);
            AnimChoice.append("Jump",null);
            
            form.append(AnimChoice);
            form.append(new SimpleItem("Animation Preview"));
     ;
            form.setItemStateListener(this);
            
                 try {
                    image = Image.createImage("/Tortoise"+AnimChoiceindex+".png");
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
                alice = new Sprite(image, image.getWidth()/3, image.getHeight());
                
            return form;
        }
        
        public void itemStateChanged(Item item) {
            if(item==AnimChoice)
            {
                  mStringItem1.setText("AnimChoice = " + AnimChoice.getSelectedIndex());
                  AnimChoiceindex=AnimChoice.getSelectedIndex();
                
                  
            }
        }
        
        public void commandAction(Command c, Displayable s) {
       
        }
        
      
        
          class SimpleItem extends CustomItem {
            
            public SimpleItem(String title) { super(title);}
            // CustomItem abstract methods.
            
            
            public int getMinContentWidth() { return 200; }
            public int getMinContentHeight() { return 150; }
            
            public int getPrefContentWidth(int width) {
                return getMinContentWidth();
            }
            
            public int getPrefContentHeight(int height) {
                return getMinContentHeight();
            }
            public void paint(Graphics g, int w, int h) {
                 g.fillRect(8,6,100,100);
                 alice.setRefPixelPosition(8,6);
                alice.nextFrame();
                alice.paint(g);
                repaint();
            }
            public void paint2(Graphics g, int w, int h) {
               
            }
            
      
        }
        public void pauseApp() {
        }
        
        public void destroyApp(boolean unconditional) {
        }
    }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: Choicegroup + event listener

    Hi i need help, according to my codes below, when i run this midlet, my character threading image will change according to the AnimChoice choicegroup upon selection, but this will only work twice, which means i change from walk to jumping, then jumping to walk, and third time changing it, the threading image wont change accordingly, anyone have any ideas?

    and one more thing, the threaded image wil be running at a very quick speed, i change sleep(300) to sleep(1000) , the speed of the thread does not seems to slow down. why?

    thanks for the help in advance =)


    Code:
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Gauge;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemStateListener;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.m2g.SVGImage;
    import javax.microedition.m2g.ScalableGraphics;
    import javax.microedition.m2g.ScalableImage;
    import javax.microedition.media.*;
    import javax.microedition.rms.*;
    import java.util.*;
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.io.*;
    import java.util.*;
    import javax.microedition.rms.*;
    import org.w3c.dom.Document;
    import org.w3c.dom.svg.SVGSVGElement;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.m2g.ScalableGraphics;
    import javax.microedition.m2g.ScalableImage;
    
    public class GaugeTracker extends MIDlet implements ItemStateListener, CommandListener {
        private Gauge mGauge = new Gauge("GaugeTitle", true, 5, 3);
        private ChoiceGroup AnimChoice= new ChoiceGroup("Animation Choice", Choice.POPUP);
        private Sprite alice,alice2; //Sprite: movement of individual graphic objects
        private Image image;
        private AliceThread aThread;
        private StringItem mStringItem = new StringItem(null, "[value]");
        private int x,y;
        Form form;
        int AnimChoiceIndex=0;
        public GaugeTracker() {
            AnimChoice.append("Walk", null);
            AnimChoice.append("Jump",null);
            AnimChoice.setSelectedIndex(0,true);
            AnimChoiceIndex=AnimChoice.getSelectedIndex();
            itemStateChanged(AnimChoice);
        }
        class AliceThread extends Thread{
            
            public void run(){
                while (true) {
                    try{
                        sleep(300);
                    }catch(InterruptedException ie)	{
                        ie.printStackTrace();
                    }
                    x -= 20;
                    if(x <= 0 - image.getWidth())
                        x = 100;
                }
            }
        }
        public void itemStateChanged(Item item) {
            if (item == AnimChoice)
      
            Form form1= getForm(AnimChoice.getSelectedIndex());
            
        }
        
        public void commandAction(Command c, Displayable s) {
            if (c.getCommandType() == Command.EXIT)
                notifyDestroyed();
        }
        
        public void startApp() {
    
            form=getForm(AnimChoiceIndex);
            
            Display.getDisplay(this).setCurrent(form);
        }
        
        
        Form getForm(int AnimChoiceIndex123) {
            
            Form form = new Form("GaugeTracker");
            form.addCommand(new Command("Exit", Command.EXIT, 0));
            form.setCommandListener(this);
            // Now add the selected items.
            
            
            
            AnimChoice = new ChoiceGroup("Animation Choice", Choice.POPUP);
            AnimChoice.append("Walk", null);
            AnimChoice.append("Jump",null);
            
            form.append(AnimChoice);
            form.append(new SimpleItem("Animation Preview"));
            ;
            form.setItemStateListener(this);
            
            try {
                image = Image.createImage("/Tortoise"+AnimChoiceIndex123+".png");
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            alice = new Sprite(image, image.getWidth()/3, image.getHeight());
            
            return form;
        }
        
        class SimpleItem extends CustomItem {
            
            public SimpleItem(String title) { super(title);}
            // CustomItem abstract methods.
            
            
            public int getMinContentWidth() { return 200; }
            public int getMinContentHeight() { return 150; }
            
            public int getPrefContentWidth(int width) {
                return getMinContentWidth();
            }
            
            public int getPrefContentHeight(int height) {
                return getMinContentHeight();
            }
            public void paint(Graphics g, int w, int h) {
                g.fillRect(8,6,100,100);
                alice.setRefPixelPosition(8,6);
                alice.nextFrame();
                alice.paint(g);
                repaint();
            }
            
        }
        public void pauseApp() {
        }
        
        public void destroyApp(boolean unconditional) {
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 2009-03-12, 21:07
  2. CLogEvent problem
    By silviuccia in forum Symbian
    Replies: 2
    Last Post: 2006-12-22, 15:28
  3. Install a system event listener?
    By profoundwhispers in forum Symbian
    Replies: 2
    Last Post: 2005-03-13, 13:24
  4. how to write alert with event listener
    By eng00787 in forum Mobile Java General
    Replies: 1
    Last Post: 2004-02-14, 00:17
  5. Event Listener Application
    By vincent32 in forum Symbian
    Replies: 0
    Last Post: 2002-11-12, 12:32

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×