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