×

Discussion Board

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

    threading help J2ME

    firs question:
    is it possible for 2 threading to be in one canvas.

    second question:
    how do i preview two threading in one canvas.
    i can display one threading but second threading will encounter a nullpointer.
    the problem lies in this line i presume:

    spriteTwo.setRefPixelPosition(100,100);

    the full code of the preview class is below
    Thanks =D

    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;
    
    public class previewCanvas extends Canvas implements CommandListener {
        
        private mainMenuMidlet midlet;
        private Display display;
        private ScalableImage background;
        private String text;
        private int x;
        private  int y;
        private String fontSize;
        private String fontColour;
        private boolean mustRepaintText;
        private String addChoice;
        private character characterOne,characterTwo;
        private  String fontStyle;
        
         ScalableGraphics sg = null;
             private String character1="";
        private String character2="";
          private int x1 = getWidth()/2 - 10;
        private int y1 = getHeight()/2 - 10;
        private int x2 = getWidth()/2 - 20;
        private int y2 = getHeight()/2 - 20;
        
           private Command bkCmd, saveCmd;
            //CharacterOne
        private Sprite spriteOne,spriteTwo; //Sprite: movement of individual graphic objects
        private Image image;
        private characterOneThread characterOneThread;
        
        private Image image2;
        private characterTwoThread characterTwoThread;
        /** Creates a new instance of previewCanvas */
        public previewCanvas(mainMenuMidlet m, Display d,ScalableImage background, String text, int x, int y, String fontSize, String fontColour,  boolean mustRepaintText,String addChoice,character characterOne, character characterTwo, String fontStyle) {
       
            this.midlet=m;
            this.display=d;
            this.background=background;
            this.text=text;
            this.x=x;
            this.y=y;
            this.fontSize=fontSize;
            this.fontColour=fontColour;
            this.characterOne=characterOne;
            this.mustRepaintText=mustRepaintText;
            this.addChoice=addChoice;
            this.characterTwo=characterTwo;
            this.fontStyle=fontStyle;
            
             this.character1=characterOne.getCharacterName();
            this.character2=characterTwo.getCharacterName();
            this.x1=characterOne.getX();
            this.y1=characterOne.getY();
            this.x2=characterTwo.getX();
            this.y2=characterTwo.getY();
            
            System.out.println("Entere preview Canvas "+characterTwo.getCharacterName()+" "+characterTwo.getX());
            
             bkCmd = new Command("Back", Command.EXIT, 2);
            saveCmd = new Command("Save", Command.OK, 1);
            addCommand(bkCmd);
            addCommand(saveCmd);
            setCommandListener(this);
            
            
            
                try {
                image = Image.createImage("/picture/Tortoise1.png");
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            spriteOne = new Sprite(image, 351/3, 126);
            
            x = getWidth();
            y = 50;
            characterOneThread = new characterOneThread();
            characterOneThread.start();
              characterTwoThread = new characterTwoThread();
            characterTwoThread.start();
        }
        
         class characterOneThread extends Thread{
            
            public void run(){
                while (true) {
                    try{
                        sleep(500);
                    }catch(InterruptedException ie)	{
                        ie.printStackTrace();
                    }
                    x -= 50;
                    if(x <= 0 - image.getWidth())
                        x = getWidth();
                    repaint();
                }
            }
        }
         
            
         class characterTwoThread extends Thread{
            
            public void run(){
                while (true) {
                    try{
                        sleep(500);
                    }catch(InterruptedException ie)	{
                        ie.printStackTrace();
                    }
                    x -= 50;
                    if(x <= 0 - image.getWidth())
                        x = getWidth();
                    repaint();
                }
            }
        }
        
        
        public void commandAction(Command c, Displayable d) {
            
            if(c==bkCmd)
            {
               mainCanvas mainCanvas= new mainCanvas( midlet, display , background,text,  x, y, fontSize, fontColour,  mustRepaintText,addChoice, characterOne,  characterTwo, fontStyle);
              display.setCurrent(mainCanvas);
              
            }else if(c==saveCmd)
            {
                
            }
            
            
        }
        
        public void paint(Graphics g) {
            
            
               clearScreen(g);
            
            
              if(!characterOne.getCharacterName().equalsIgnoreCase(""))
            {    
            // g.fillRect(10,6,getWidth(),getHeight());
            spriteOne.setRefPixelPosition(characterOne.getX(),characterOne.getY());
            spriteOne.nextFrame();
            spriteOne.paint(g);
              }
            
            if(!characterTwo.getCharacterName().equalsIgnoreCase(""))
            {
                spriteTwo.setRefPixelPosition(100,100);
            spriteTwo.nextFrame();
            spriteTwo.paint(g);
            }
        }
        
        
         private void clearScreen(Graphics g) {
            g.setColor(0xFFFFFF);
            g.fillRect(0,0,getWidth(),getHeight());
        }
         public String getFontSize() {
            return fontSize;
        }
        
        public void setFontSize(String fontSize) {
            this.fontSize = fontSize;
        }
        
        public String getFontColour() {
            return fontColour;
        }
        
        public void setFontColour(String fontColour) {
            this.fontColour = fontColour;
        }
        
            public String getFontStyle() {
            return fontStyle;
        }
        
        public void setFontStyle(String fontStyle) {
            this.fontStyle = fontStyle;
        }
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: threading help J2ME

    Yes... spriteTwo is always null, you never create an object for it.

    You look to have a bunch of other issues... both threads do exactly the same thing, you never use image2, etc.

    I strongly recommend you use an IDE like eclipse, which can show you unused member variables.

    Your threads use image.getWidth() and [canvas].getWidth(), when they look like they should be using the same width each time.

    Before you cut'n'paste code (like your character thread classes), think about whether you can add a parameter and re-use. Remember that less code = less bugs = less headache! If you're tempted to put a number in a class or variable name (characterOneThread, etc.), this is the time to think about adding parameters or using arrays.

    And again, "while(true)" in the thread loop... these threads will never end. The garbage collector does not terminate threads.

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

    Re: threading help J2ME

    thanks graham i checked and spriteTwo is indeed null.
    i reference the image2 into sprite two with the following upgrade
    Code:
    if(character2!="") {
                System.out.println(" ENTER HERE FOR CHARACter2");
                try {
                    System.out.println("HERE character");
                    Image characterImage= Image.createImage("/picture/"+character2+".png");
                    g.drawImage(characterImage,x2,y2,Graphics.VCENTER| Graphics.HCENTER);
                } catch(Exception e) {
                    e.printStackTrace();
                }
                
                 if(!characterTwo.getCharacterName().equalsIgnoreCase(""))
            {
                spriteTwo.setRefPixelPosition(100,100);
            spriteTwo.nextFrame();
            spriteTwo.paint(g);
            }
            }
    but theres still nullpointerexception
    the end result i am can animate the first character, but not the 2nd. which is spriteTwo.

    thanks =D

  4. #4
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: threading help J2ME

    Quote Originally Posted by shirodas View Post
    firs question:
    is it possible for 2 threading to be in one canvas.

    second question:
    how do i preview two threading in one canvas.
    Hi,
    @ Well you can think yourself that if you would create two threads in the same canvas then there has to be two run()..but that can not be done..On the other hand if you will share the same run()..then there might be deadlock..
    Therefore That can not be done..

    @ What exactly is the requirement for the two thread in canvas.
    Have you fix the issue ..
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

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

    Re: threading help J2ME

    thanks i solved this already
    thanks guys for all your help ^^

Similar Threads

  1. J2ME or C++ for VoIP on Mobile
    By sandeepkumar03 in forum VoIP
    Replies: 4
    Last Post: 2009-02-02, 10:28
  2. J2ME threading help
    By shirodas in forum Mobile Java General
    Replies: 5
    Last Post: 2008-12-01, 09:43
  3. threading image to a gif image ? (j2me)
    By intheworldofmyown in forum Mobile Java General
    Replies: 3
    Last Post: 2008-11-18, 17:31
  4. J2ME or C++ for VoIP on Mobile Phone
    By sandeepkumar03 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2008-11-13, 20:14
  5. J2ME Polish Version 1.0 RC6
    By enough in forum Mobile Java General
    Replies: 1
    Last Post: 2004-07-20, 11:25

Posting Permissions

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