JavaME wielkość czcionki z odczytanego pliku z pomocą Canvas i inne pytania

0

m
mam pewien problem w JavaME
a mianowicie
1.
chce odczytać plik z pomocą Canvas, ale mi nie działa
natomiast, z pomocą form odczyt pliku działa
2
chce powiększyć czcionkę, zmienić kolor, kolor tło itp. z odczytanego pliku
3
w linijce




 private Canvas createTextCanvas() {
        Canvas canvas = new TextCanvas();        
     //   addCommands(canvas);
        return canvas;
    }

wywala mi błąd
C:_test płyty\12\src\hello\HelloMIDlet.java error: cannot find symbol
addCommands(canvas);
symbol: method addCommands(Canvas)
location: class HelloMIDlet
1 error

wiem, że jest problem z identyfikacja, tylko nie wiem jaką ścieżkę dopisać

pełny kod bez odczytu pliku


 
package hello;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;  
import java.io.*;
import java.lang.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
/**
 * @author pol
 */
public class HelloMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
 
    private Command exitCommand;
    private Command backCommand;
    private Form form;
    private StringItem stringItem;
    private List list;
    private Form form1;
    private StringItem stringItem1;
	private Display display;
	protected boolean started;
	private Canvas[] canvases; 
 

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }
 
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
      
     */
    private void initialize() { 
    } 
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() { 
        switchDisplayable(null, getList()); 
    } 
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() { 
    } 
    
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) { 
        Display display = getDisplay(); 
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        } 
    } 
 
    public void commandAction(Command command, Displayable displayable) { 
        if (displayable == form) { 
            if (command == backCommand) { 
                switchDisplayable(null, getList()); 
            } 
        } else if (displayable == form1) {
            if (command == backCommand) { 
                switchDisplayable(null, getList()); 
            } 
        } else if (displayable == list) {
            if (command == List.SELECT_COMMAND) { 
                listAction(); 
            } else if (command == exitCommand) { 
                exitMIDlet(); 
                
            } 
        } 
    } 
   
    public Command getExitCommand() {
        if (exitCommand == null) { 
            exitCommand = new Command("Exit", Command.EXIT, 0); 
        } 
        return exitCommand;
    } 
    
    public Form getForm() 
	{
	String lancuch = "",lancuch2 = "";
	int dlugosc = 0;
	byte[] bufor = new byte[1024];
	if (form == null) 
	{
	   form = new Form("Jeden", new Item[]{});
        try 
		{
            InputStream plik = getClass().getResourceAsStream("1.txt");
            while (dlugosc > -1)
			{
	    lancuch2 =  new String(bufor, 0, dlugosc);
	    lancuch += lancuch2;
            dlugosc = plik.read(bufor);
            }
	//    Command exit = new Command("exit", Command.EXIT, 1);
     // form.addCommand(getHelpCommand());
      form.addCommand(getBackCommand());
      form.setCommandListener(this);
      form.append(lancuch);
      plik.close();       
		}
   catch (Exception e)
   {
       e.printStackTrace();
   }
	}
        
         return form;
	} 
  
    public StringItem getStringItem() {
        if (stringItem == null) { 
            stringItem = new StringItem("Hello", "Hello, World!"); 
        } 
        return stringItem;
    } 
    
    public Command getBackCommand() {
        if (backCommand == null) { 
            backCommand = new Command("Back", Command.BACK, 0); 
        } 
        return backCommand;
    }
 
 
    public List getList() {
        if (list == null) { 
            list = new List("list", Choice.IMPLICIT); 
            list.append("List Element 1", null);
            list.append("List Element 2", null);
            list.addCommand(getExitCommand());
            list.setCommandListener(this);
            list.setSelectedFlags(new boolean[]{false, false}); 
        } 
        return list;
    } 
    /**
     * Performs an action assigned to the selected list element in the list
     * component.
     */
    public void listAction() { 
        String __selectedString = getList().getString(getList().getSelectedIndex());//GEN-BEGIN:|22-action|1|36-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("List Element 1")) { 
                switchDisplayable(null, createTextCanvas()); 
            } else if (__selectedString.equals("List Element 2")) { 
               
            } 
        }   
    }   /**
     * Returns an initialized instance of form1 component.
     *
     * @return the initialized component instance
     */
    public Form getForm1() {
        if (form1 == null) { 
           
            form1 = new Form("form1", new Item[]{getStringItem1()}); 
            form1.addCommand(getBackCommand());
            form1.setCommandListener(this); 
           
        } 
        return form1;
    }
    /**
     * Returns an initialized instance of stringItem1 component.
     *
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("stringItem1", null);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return stringItem1;
    }
//</editor-fold>//GEN-END:|40-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
         
            
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
	
   private Canvas createTextCanvas() {
        Canvas canvas = new TextCanvas();        
     //   addCommands(canvas);
        return canvas;
    }
     
   
     
} 
	class TextCanvas extends Canvas {
    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        // Create a black background
        g.setColor(0);
        g.fillRect(0, 0, width, height);
        // Top-left of canvas
        g.setColor(0xffffff);
        g.drawString("Top left", 0, 0, Graphics.TOP | Graphics.LEFT);
        // Draw another string one line below
        Font font = g.getFont();
        g.drawString("Below top left", 0, font.getHeight(), Graphics.TOP | Graphics.LEFT);
        // Bottom-right of canvas
        g.drawString("Bottom right", width, height, Graphics.BOTTOM | Graphics.RIGHT);
        // Mixed fonts and colors
        String str = "Multi-font ";        
        font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_SMALL);
        g.setFont(font);
        g.drawString(str, 0, height/2, Graphics.LEFT | Graphics.BASELINE);
        int x = font.stringWidth(str);
        g.setColor(0x00ff00);
        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD | Font.STYLE_ITALIC, 
    Font.SIZE_MEDIUM));

        g.drawString("and multi-color", x, height/2, Graphics.LEFT | Graphics.BASELINE);
    }
}     

kod odczytu pliku za pomocą form

public Form getForm7()
	{
	String lancuch = "",lancuch2 = "";
	int dlugosc = 0;
	byte[] bufor = new byte[1024];
	if (form7 == null) 
	{
	   form7 = new Form("Siedem", new Item[]{});
        try 
		{
            InputStream plik = getClass().getResourceAsStream("7.txt");
            while (dlugosc > -1)
			{
	    lancuch2 =  new String(bufor, 0, dlugosc);
	    lancuch += lancuch2;
            dlugosc = plik.read(bufor);
            }
	//    Command exit = new Command("exit", Command.EXIT, 1);
     // form.addCommand(getHelpCommand());
      form7.addCommand(getBackCommand());
      form7.setCommandListener(this);
	  form7.append(lancuch);
      plik.close();       
		}
   catch (Exception e)
   {
       e.printStackTrace();
   }
	}
         return form7;
	} 

dziękuje za odpowiedz

0

Witam
udało mi się odczytać plik w canvas, ale mam mały problem otóż
po dodaniu pliku program go odczytuje,natomiast, gdy
plik skasuje i ponownie uruchomię program to i tak jest odczytany
ostatni plik, którego go nie ma
moje pytanie jak wyczyścić ekran oraz jak zrobić, żeby wyświetlił mi komunikat o braku pliku

podaje kod

package hello;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
 import java.io.*;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

public class HelloMIDlet extends MIDlet implements CommandListener {

    // The MIDlet's Display object
    private Display display;
       
    // Flag indicating first call of startApp
    protected boolean started;
   
    // Exit command
    private Command exitCommand;
   
    // Back to examples list command
    private Command backCommand;
   
    // The example selection list
    private List examplesList;
   
    // The Canvases used to demonstrate different Items
    private Canvas[] canvases;
   
    // The example names. Used to populate the list.
    private String[] examples = {
        "test_javaio"
    };

    protected void startApp() {
        if (!started) {
            started = true;
            display = Display.getDisplay(this);
           
       
            createCommands();
           
     
            createCanvases();
           
         
            createList();
           
         
            display.setCurrent(examplesList);
        }
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (d == examplesList) {
            // New example selected
            int index = examplesList.getSelectedIndex();
            display.setCurrent(canvases[index]);
        } else if (c == exitCommand) {
           
            notifyDestroyed();
        } else if (c == backCommand) {
         
            display.setCurrent(examplesList);
        }
    }
   
    private void createCommands() {
        exitCommand = new Command("Wyjście", Command.EXIT, 0);
        backCommand = new Command("Cofnij", Command.BACK, 1);
    }
   
    private void createList() {
        examplesList = new List("wybierz", List.IMPLICIT);
        for (int i = 0; i < examples.length; i++) {
            examplesList.append(examples[i], null);
        }
        examplesList.setCommandListener(this);
    }
   
    private void createCanvases() {
        canvases = new Canvas[examples.length];
         canvases[0] = createtest();
       
    }

    private void addCommands(Displayable d) {
        d.addCommand(exitCommand);
        d.addCommand(backCommand);
        d.setCommandListener(this);
    }
       private Canvas createtest() {
        Canvas canvas = new test();       
        addCommands(canvas);
        return canvas;
    }
class test extends Canvas
{
public InputStream inputStream;
 
   String lancuch = "",lancuch2 = "";
   int dlugosc = 0;
   byte[] bufor = new byte[1024];
   int count;
   
    public void paint(Graphics g)
    {
        int width = getWidth();
        int height = getHeight();
 
    g.fillRect(0, 0, width, height);
    g.setColor(0xffffff);
    g.drawString("Test_pliku", 0, 4, Graphics.TOP | Graphics.LEFT);
 //   g.drawString("555555555555", width/2, height/2, Graphics.HCENTER | Graphics.BASELINE);
       
     if (inputStream == null)
       {
        try
            {
     InputStream plik = getClass().getResourceAsStream("1.txt");
     while (dlugosc > -1)
        {
        lancuch2 =  new String(bufor, 0, dlugosc);
        lancuch += lancuch2;
        dlugosc = plik.read(bufor);
        }
    g.setColor(0xffffff);
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
    g.drawString(lancuch, 0, 55, Graphics.TOP | Graphics.LEFT);
    plik.close();       
                }
   catch (IOException e)
   {
  e.printStackTrace();
    g.setColor(0xffffff);
    g.drawString("Błąd pliku !", 0, 66, Graphics.TOP | Graphics.LEFT);
    return;
   }
       }
    }
}
}

dziękuje za odpowiedz

nie mam pomysłu jak zrobić, żeby program
gdy nacisnę menu wielkie litery to dla wszystkich plików otwierały się wszystkie pliki

dziękuje za odpowiedz

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