Alguem ai tem alguma ideia de como ir lendo um arquivo .java e ir colorindo o codigo. usando Graphics
Ideia colorir codigo java
3 Respostas
1 like
Antonio não entendi muito bem seu problema, mas se for o que estou pensado cria um arrayList com todas as palavras reservadas do java, dai você vai lendo as strings do seu arquivo .java e as que forem palavras resevadas você coloca de uma cor e as que não forem de outra cor.
to tentando fazer um editor com sintax higlighting em j2me. pra poder ir editando ums codigos no celular.
to aqui pensando num algoritimo. isso que vc falou é uma boa ideia. vlw
consegui:
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class EditCanvas extends Canvas {
private String texto;
private String[] keywords = {"abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", " volatile", "const", " float", "native", "super", "while"};
private Vector iniciosPalavraChave;
private Vector finsPalavraChave;
private Vector iniciosString;
private Vector finsString;
public EditCanvas(String texto) {
this.texto = texto;
}
public void atualizaIndices() {
iniciosPalavraChave = new Vector();
finsPalavraChave = new Vector();
iniciosString = new Vector();
finsString = new Vector();
//Indices keywords
String k;
int idx;
int endidx = 0;
for (int i = 0; i < keywords.length; i++) {
k = keywords[i];
idx = 0;
System.out.println("Keyword: " + k);
while (true) {
idx = texto.indexOf(k, endidx);
if (idx == -1) {
System.out.println("Nao encontrou");
break;
}
endidx = (idx + k.length()) - 1;
System.out.println("Primeira letra em: " + idx + " " + texto.charAt(idx));
System.out.println("Ultima letra em: " + endidx + " " + texto.charAt(endidx));
iniciosPalavraChave.addElement("" + idx);
finsPalavraChave.addElement("" + endidx);
}
}
//Literais string
endidx = 0;
while (true) {
idx = texto.indexOf('"', endidx+1);
if (idx == -1) {
break;
}
iniciosString.addElement("" + idx);
endidx = texto.indexOf('"', idx+1);
finsString.addElement("" + endidx);
}
}
protected void paint(Graphics g) {
int linha = 0;
Font fonte = g.getFont();
int height = fonte.getHeight();
int corPalavraChave = 0x0000FF;
int corStringLiteral = 0xFF8000;
int corTexto = 0x000000;
int idx;
int x = 0;
char nextChar;
//Limpar a tela
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < texto.length(); i++) {
if ((idx = iniciosPalavraChave.indexOf("" + i)) != -1) {
g.setColor(corPalavraChave);
int endidx = Integer.parseInt((String) finsPalavraChave.elementAt(idx));
while (i <= endidx) {
nextChar = texto.charAt(i);
g.drawChar(nextChar, x, linha * height, 0);
x += fonte.charWidth(nextChar);
i++;
}
continue;
}
if ((idx = iniciosString.indexOf("" + i)) != -1) {
g.setColor(corStringLiteral);
int iFim = Integer.parseInt((String) finsString.elementAt(idx));
while (i <= iFim) {
nextChar = texto.charAt(i);
g.drawChar(nextChar, x, linha * height, 0);
x += fonte.charWidth(nextChar);
i++;
}
continue;
}
nextChar = texto.charAt(i);
if (nextChar == '\n' || nextChar == '\r') {
linha++;
continue;
}
g.setColor(corTexto);
g.drawChar(nextChar, x, linha * height, 0);
x += fonte.charWidth(nextChar);
}
}
}
sera que é eficiente ?
Criado 17 de maio de 2012
Ultima resposta 19 de mai. de 2012
Respostas 3
Participantes 2