| |

VerySource

 Forgot password?
 Register
Search
View: 1063|Reply: 6

There is a complete description of the problem with the loading path of the picture in j2ME

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-11-30 18:00:01
| Show all posts |Read mode
classpath:E:\JBuilder\jdk1.4\lib\tools.jar;E:\JBuilder\jdk1.4\lib\dt.jar;.
Project path: D:\javawork\j2me\SlideshowMidlet
Picture storage location: D:\javawork\j2me\SlideshowMidlet\
The program is written, compiled, and run in JBuilder
When running, throw an exception: Failed loading images!
The source code is as follows: (There are two classes, the running class is not posted)
import javax.microedition.lcdui.*;
import java.io.*;

public class SSCanvas extends Canvas {
    private Display display;
    private Image [] slides;
    private String [] captions = {"A","B","C","D","E"};
    private int curSlide = 0;

    public SSCanvas(Display d){
        super();
        display = d;
    //The picture loader starts
        try {
            slides = new Image[5];
            slides[0] = Image.createImage("/A.gif");
            slides[1] = Image.createImage("/B.gif");
            slides[2] = Image.createImage("/C.gif");
            slides[3] = Image.createImage("/D.gif");
            slides[4] = Image.createImage("/E.gif");
        } catch (IOException e) {
            System.err.println("Failed loading images!");
        }//Picture loading end
    }
      void start(){
          display.setCurrent(this);
          repaint();
        }
      public void keyPressed(int keyCode){
          int action =getGameAction(keyCode);

          switch(action){
             case LEFT:
                 if(-- curSlide <0 ){
                     curSlide = slides.length-1;
                 }
                 repaint();
                 break;

            case RIGHT:
                if(++ curSlide >= slides.length){
                    curSlide = 0;
                }
                repaint();
                break;
          }
      }
    public void paint(Graphics g){
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(),getHeight());

        g.drawImage(slides[curSlide],getWidth()/2,getHeight()/2,Graphics.HCENTER | Graphics.VCENTER);
        Font f = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
        g.setFont(f);

        g.setColor(0,0,0);
        g.drawString(captions[curSlide],getWidth()/2,0,Graphics.HCENTER | Graphics.TOP);
    }
}
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-12-1 15:45:01
| Show all posts
Pictures and source files together
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-1 16:00:01
| Show all posts
I put the picture in SRC and put it together with the source file, but it still doesn't work.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-12-1 16:15:01
| Show all posts
Try referencing the absolute path
import javax.microedition.midlet.*; add this sentence if you are doing a complete program
Image slides[0] = Image.createImage("/A.gif");
It’s no problem to put this picture and project files in one folder
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-1 16:30:01
| Show all posts
gosh! What simulator do you use? I have never heard of any models that support GIF animation. You try to change to png,
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-1 16:45:01
| Show all posts
Put the picture in the classes folder
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-1 17:00:01
| Show all posts
Thank you, everyone, the problem has been solved! ~ WTK 2.0 said that it can support GIF format pictures! But I did not consider the actual model, asconan609said, there will be no problem when changing to png format !~ And the picture should be placed in the classes file!~ Absolute path can also be used, but in that case, it will not be easy to do when publishing,! ~~`Please give me more advice in the future!~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list