Java ME - problem z paint()

0

Mam oto taki kod:
ConsoleCanvas.java - Odpowiada za wyświetlanie i zmienianie Ekranu konsoli

package Console;


import javax.microedition.lcdui.*;
import java.io.*;

public class ConsoleCanvas extends Canvas implements CommandListener{
    int screenWidth;
    int screenHeight;
    int screenCellsX;
    int screenCellsY;

    int x;
    int y;

    static int i=0;

    ConsoleCell[][] workSpace;
    public ConsoleCanvas(){
        super();
        initConsole();
    }

    public void initConsole(){
        screenWidth = getWidth();
        screenHeight= getHeight();

        screenCellsX=screenWidth/8;
        screenCellsY=screenHeight/8;
        workSpace=new ConsoleCell[screenCellsX][screenCellsY];

        for(int i=0;i<screenCellsX;i++){
            for(int a=0;a<screenCellsY;a++){
                workSpace[i][a]=new ConsoleCell();
            }
        }
        System.out.println("Screen ok!");
        new Thread(new RepaintTask()).run();
    }
    public void printChar(char chr){
        workSpace[x][y].setChar(chr);
        x++;
        if(x>screenCellsX){
            x=0;
            y++;
        }
        if(y>screenCellsY){
            y=0;
        }
    }

    public void newLine(){
        x=0;
        y++;
        if(y>screenCellsY){
            y=0;
        }
    }

    public void setChar(int _x, int _y, char chr){
        workSpace[_x][_y].setChar(chr);
    }
    public void setChar(char chr){
        workSpace[x][y].setChar(chr);
    }
    public void setPos(int _x, int _y){
        x=_x;
        y=_y;
    }
    public int getPosX(){
        return x;
    }
    public int getPosY(){
        return y;
    }
    public ConsoleCell getCell(int _x, int _y){
        return workSpace[_x][_y];
    }
    public ConsoleCell getCell(){
        return workSpace[x][y];
    }
    protected void paint(Graphics g) {
        System.out.println("Painted");
        g.setColor(0,0,0);
        g.fillRect(0, 0, screenWidth, screenHeight);

        for(int i=0;i<screenCellsX;i++){
            for(int a=0;a<screenCellsY;a++){
                g.drawImage(workSpace[i][a].getImage(), i*8, a*8, 0);
            }
        }
    }

    protected void update(){
        repaint();
        serviceRepaints();
        System.out.println("Uptaded");
    }

    
    class RepaintTask implements Runnable{
        public void run(){
            while(true){
                try {
                    Thread.sleep(1000);
                    update();
                    System.out.println("Runned");
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

ConsoleCell.java - Reprezentuje jedną komórkę ekranu w której można zapisać dowolny znak

package Console;

import java.io.IOException;
import javax.microedition.lcdui.*;

public class ConsoleCell {
    private char current;
    private Image img;

    public ConsoleCell(){
        setChar(' ');
    }

    public Image getImage(){
        return img;
    }
    public void setChar(char c){
        try {
            current = c;
            img = Image.createImage("/font/F" + ((int) current)+".png");
            System.out.println((ConsoleCanvas.i++)+" ImageLoaded: "+c+":"+(int)c);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public char getChar(){
        return current;
    }
}

ConsoleTester.java - Kod Testujący

package Console;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ConsoleTester extends MIDlet {
    ConsoleCanvas c;

    public ConsoleTester(){
        c=new ConsoleCanvas();
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(new ConsoleCanvas());
        c.setFullScreenMode(true);
        c.setPos(10, 10);
        c.setChar('A');
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

Jest to kod "konsoli" na komórki, a raczej ta część która odpowiada za pamięć ekranu.
Chodzi o to że po wywołaniu metody repaint() i serviceRepaints() powinna zostać wywołana metoda paint(), ale nie jest bo na ekranie nie wypisuje się Painted. Do tego na emulatorze nic nie wyświetla i działa w tle.
Co robię źle i jak zrobić tak aby program malował ekran co jakiś określony czas? Proszę o szybką odpowiedz.

0

Już nawet nie wnikałem w kod za bardzo, ale od razu rzuca się w oczy, że powinieneś raczej napisać new Thread(new RepaintTask()).start(); zamiast new Thread(new RepaintTask()).run();

Jeśli to nic nie da, to napisz ;-)

0

Nie no, żeby takiego błędu nie zauważyć. Jak poprawię to wprawdzie aplikacja rysuje początkowe ustawienie wszystkich komórek, ale nie są one odświeżane (w komórce 10,10 powinno pojawić się A). Znaczy wykonuje poprawnie metodę paint() ale nie wyświetla poprawnego stanu komórek.

0

No, w gruncie rzeczy, to wystarczy napisać Display.getDisplay(this).setCurrent(c), zamiast Display.getDisplay(this).setCurrent(new ConsoleCanvas()); xD

Btw., postaraj się znaleźć jakąś lepszą metodę na wczytywanie tej "czcionki", np. najpierw wczytaj wszystkie obrazy do jakiejś tablicy, a potem je z tamtąd zczytuj. Tak będzie znacznie szybciej ;-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1