Repaint() qui n'appelle pas paintComponent()

Fermé
benjtlv Messages postés 26 Date d'inscription jeudi 26 avril 2012 Statut Membre Dernière intervention 26 avril 2016 - 21 avril 2015 à 16:05
benjtlv Messages postés 26 Date d'inscription jeudi 26 avril 2012 Statut Membre Dernière intervention 26 avril 2016 - 21 avril 2015 à 23:40
Bonjour,





J'ai un programme à faire il s'agit d'un analyseur syntaxique qui construit un arbre de syntaxe. Il s'agit d'une spécification avec des instructions comme Avance 19; Donc lors de cet instruction j'aimerais dessiner une ligne de 19 pixels. J'appelle donc repaint() qui ne s'execute pas.. J'avais un projet à faire il y a 4 mois beaucoup plus compliqué que ça et où tout marchait bien. Et il n'y a rien de différent dans la méthode. J'ai vraiment besoin d'aide.

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
21 avril 2015 à 18:03
Bonjour,

Impossible de t'aider sans le code. On ne va pas lister tous les cas possibles d'erreurs en espérant tomber sur la bonne...
0
benjtlv Messages postés 26 Date d'inscription jeudi 26 avril 2012 Statut Membre Dernière intervention 26 avril 2016
21 avril 2015 à 23:40
jai beaucoup de code en fait ya une classe AbstractSyntax qui contient plusieurs classes telles que Declaration Instruction qui sont des objets à part entière. Et Parser qui contient les fonctions recursives de parsage des tokens qui se chargent de construire l'arbre de Syntaxe. Une fois construit on execute Parser.nontermProg() qui teste si le code est syntaxiquement correct. Ensuite on execute prog.run() qui execute les instructions du langage qui ici se charge de tracer des lignes et d'effectuer des rotations (pour changer la direction de la prochaine ligne)
Le problème est dans la fonction exec de la classe Avance c'est la que j'appelle repaint() que j'ai redéfinie par paintComponent de la classe Panneau (le JPanel)
et qui s'occupe de faire le drawLine. En fait repaint() est appellé deux fois à la fin. c'est extremement bizarre et je suis dépassé par cela.
La classe AbstractSyntax :
import java.util.*;
import java.lang.Math;
import java.awt.Color;

abstract class Expression {
abstract int eval(ValueEnvironment env) throws Exception;
}
class Int extends Expression {
private int value;
public Int(int i) {
value = i;
}
public int eval(ValueEnvironment env) {
return value;
}
}
class Var extends Expression {
private String name;
public Var(String s) {
name = s;
}
public int eval(ValueEnvironment env) throws Exception {
return env.getValue(name);
}
}
class Sum extends Expression {
private Expression left, right;
public Sum(Expression l, Expression r) {
left = l;
right = r;
}
public int eval(ValueEnvironment env) throws Exception {
return left.eval(env) + right.eval(env);
}
}
class Difference extends Expression {
private Expression left, right;
public Difference(Expression l, Expression r) {
left = l;
right = r;
}
public int eval(ValueEnvironment env) throws Exception {
return left.eval(env) - right.eval(env);
}
}
class Product extends Expression {
private Expression left, right;
public Product(Expression l, Expression r) {
left = l;
right = r;
}
public int eval(ValueEnvironment env) throws Exception {
return left.eval(env) * right.eval(env);
}
}
class Division extends Expression {
private Expression left, right;
public Division(Expression l, Expression r) {
left = l;
right = r;
}
public int eval(ValueEnvironment env) throws Exception {
int droit = right.eval(env);
if (droit != 0) {
return left.eval(env) / droit;
} else {
throw new Exception("Div / 0");
}
}
}

class Program {
private Declaration d;
private Instruction i;
public Program(Declaration d, Instruction i) {
this.d = d;
this.i = i;
}
public void run(ValueEnvironment env) throws Exception {
if (this.d != null) this.d.exec(env);
this.i.exec(env);
}
}

class Declaration {
private String varName;
private Declaration suivante;
public Declaration(String s, Declaration suivante) {
this.varName = s;
this.suivante = suivante;
}
public void exec(ValueEnvironment env) throws Exception {
if(this.suivante != null) {
env.addVariable(varName);
this.suivante.exec(env);
}
}
}

abstract class Instruction {
abstract void exec(ValueEnvironment env)
throws Exception;
}

class Avance extends Instruction {
private Fenetre.Panneau pan;
private Expression exp;
public Avance(Fenetre.Panneau p, Expression e) {
this.pan = p;
this.exp = e;
}
public void exec(ValueEnvironment env) throws Exception {
int ang = env.getValue(env.angle);
int coef = 10;
if (Math.abs(ang) % 90 == 0) {
if (this.exp != null) {
this.pan.x2 = this.pan.x1+(coef*(int)Math.cos(Math.PI*ang/180)*this.exp.eval(env));
this.pan.y2 = this.pan.y1+(coef*(int)Math.sin(Math.PI*ang/180)*this.exp.eval(env));
System.out.println("x1 = " + this.pan.x1 + ", y1 = " + this.pan.y1 + ", x2 = " + this.pan.x2 + ", y2 = " + this.pan.y2);
//this.pan.repaint();
this.pan.x1 = this.pan.x2;
this.pan.y1 = this.pan.y2;
}
} else {
throw new Exception("Expected angle % 90 == 0");
}
}
}

class Tourne extends Instruction {
private Fenetre.Panneau pan;
private Expression exp;
public Tourne(Fenetre.Panneau pan, Expression e) {
this.pan = pan;
this.exp = e;
}
public void exec(ValueEnvironment env) throws Exception {
if (this.exp != null)
env.setVariable(env.angle,env.getValue(env.angle) + this.exp.eval(env));
System.out.println("Tourne");
this.pan.repaint();
}
}

class BasPinceau extends Instruction {
private Fenetre.Panneau pan;
public BasPinceau(Fenetre.Panneau pan) {
this.pan = pan;
}
public void exec(ValueEnvironment env) throws Exception {
this.pan.changeColor(Color.BLACK);
//this.pan.repaint();
}
}

class HautPinceau extends Instruction {
private Fenetre.Panneau pan;
public HautPinceau(Fenetre.Panneau pan) {
this.pan = pan;
}
public void exec(ValueEnvironment env) throws Exception {
this.pan.changeColor(Color.WHITE);
//this.pan.repaint();
}
}

class Assignment extends Instruction {
private String varName;
private Expression exp;
public Assignment(String s, Expression e) {
this.varName = s;
this.exp = e;
}
public void exec(ValueEnvironment env) throws Exception {
if (this.exp != null)
env.setVariable(this.varName,this.exp.eval(env));
}
}

class BlocInstruction extends Instruction {
private Instruction i;
private BlocInstruction rest;
public BlocInstruction(Instruction i, BlocInstruction r) {
this.i = i;
this.rest = r;
}
public void exec(ValueEnvironment env) throws Exception {
if(this.rest != null) {
this.i.exec(env);
this.rest.exec(env);
}
}
}

class ValueEnvironment extends HashMap<String, Integer> {
protected String angle;

public ValueEnvironment() {
this.put(this.angle,0);
}
public void addVariable(String name) throws Exception {
this.put(name,0);<code>

}
public void setVariable(String name, int value) throws Exception {
this.put(name,value);
}
public int getValue(String name) throws Exception {
return this.get(name);
}
}</code>

La classe Parser :

class Parser {
protected LookAhead reader;
protected Fenetre fen;

/*

programme -> declarations instruction
declarations -> Var identificateur; declarations | e
instruction -> Avance expression | Tourne expression | BasPinceau | HautPinceau | identificateur = expression | Debut blocInstruction Fin
blocInstruction -> instruction; blocInstruction | e
expression -> nombre expressionSuite | identificateur expressionSuite | ( expression ) expressionSuite
expressionSuite -> + expression | - expression | * expression | / expression
  • / public Parser(LookAhead r, Fenetre fen) { reader=r; this.fen = fen; } public Program nontermProg() throws Exception { Declaration d = nontermDeclarations(); Instruction i = nontermInstruction(); reader.eat(Sym.EOF); return new Program(d,i); } public Declaration nontermDeclarations() throws Exception { if (reader.check(Sym.VAR)) { reader.eat(Sym.VAR); String nomVar = reader.getStringValue(); reader.eat(Sym.IDENTIFICATEUR); reader.eat(Sym.CONCAT); return new Declaration(nomVar,nontermDeclarations()); } else if (reader.check(Sym.AVANCE) || reader.check(Sym.TOURNE) || reader.check(Sym.BASP) || reader.check(Sym.HAUTP) || reader.check(Sym.IDENTIFICATEUR) || reader.check(Sym.DEBUT)) { return new Declaration(null,null); } else { throw new Exception("Expected FOLLOW of declarations (FIRST of instructions) or Token Var"); } } public Instruction nontermInstruction() throws Exception { if (reader.check(Sym.AVANCE)) { reader.eat(Sym.AVANCE); return new Avance(this.fen.getPan(),nontermExpr()); } else if (reader.check(Sym.TOURNE)) { reader.eat(Sym.TOURNE); return new Tourne(this.fen.getPan(),nontermExpr()); } else if (reader.check(Sym.BASP)) { reader.eat(Sym.BASP); return new BasPinceau(this.fen.getPan()); } else if (reader.check(Sym.HAUTP)) { reader.eat(Sym.HAUTP); return new HautPinceau(this.fen.getPan()); } else if (reader.check(Sym.IDENTIFICATEUR)) { String nomVar = reader.getStringValue(); reader.eat(Sym.IDENTIFICATEUR); reader.eat(Sym.EQ); return new Assignment(nomVar,nontermExpr()); } else if (reader.check(Sym.DEBUT)) { reader.eat(Sym.DEBUT); BlocInstruction bloc = nontermBlocInstruction(); reader.eat(Sym.FIN); return bloc; } else { throw new Exception("Expected AVANCE / TOURNE / BASPINCEAU / HAUTPINCEAU / IDENTIFICATEUR or DEBUT"); } } public BlocInstruction nontermBlocInstruction() throws Exception { if (reader.check(Sym.AVANCE) || reader.check(Sym.TOURNE) || reader.check(Sym.BASP) || reader.check(Sym.HAUTP) || reader.check(Sym.IDENTIFICATEUR) || reader.check(Sym.DEBUT)) { Instruction i = nontermInstruction(); reader.eat(Sym.CONCAT); return new BlocInstruction(i,nontermBlocInstruction()); } else if (reader.check(Sym.EOF) || reader.check(Sym.FIN)) { return new BlocInstruction(null,null); } else { throw new Exception("Expected FIRST of Instruction or FOLLOW of BlocInstruction"); } } public Expression nontermExpr() throws Exception { if (reader.check(Sym.NOMBRE)) { int x = reader.getIntValue(); reader.eat(Sym.NOMBRE); return nontermExpSuite(new Int(x)); } else if (reader.check(Sym.IDENTIFICATEUR)) { String nomVar = reader.getStringValue(); reader.eat(Sym.IDENTIFICATEUR); return nontermExpSuite(new Var(nomVar)); } else if (reader.check(Sym.LPAR)) { reader.eat(Sym.LPAR); Expression exp = nontermExpr(); reader.eat(Sym.RPAR); return nontermExpSuite(exp); } else { throw new Exception("Expected NOMBRE or IDENTIFICATEUR or LPAR"); } } public Expression nontermExpSuite(Expression debut) throws Exception { if (reader.check(Sym.PLUS) || reader.check(Sym.MINUS) || reader.check(Sym.TIMES) || reader.check(Sym.DIV)) { Expression exp = nontermOperateur(debut); return nontermExpSuite(exp); } else if(reader.check(Sym.EOF) || reader.check(Sym.CONCAT) || reader.check(Sym.LPAR)) { return debut; } else { throw new Exception("Expected FIRST of operateur or FOLLOW of expSuite"); } } public Expression nontermOperateur(Expression debut) throws Exception { if (reader.check(Sym.PLUS)) { reader.eat(Sym.PLUS); return new Sum(debut,nontermExpr()); } else if (reader.check(Sym.MINUS)) { reader.eat(Sym.MINUS); return new Difference(debut,nontermExpr()); } else if (reader.check(Sym.TIMES)) { reader.eat(Sym.TIMES); return new Product(debut,nontermExpr()); } else if (reader.check(Sym.DIV)) { reader.check(Sym.DIV); return new Division(debut,nontermExpr()); } else { throw new Exception("Expected PLUS/MINUS/TIMES/DIV"); } }}


La classe Fenetre :

class Fenetre extends JFrame {
private Container cont;
private Panneau pan;

class Panneau extends JPanel {
int x1, y1, x2, y2;
private Color color;

public Panneau() {
this.x1 = 0;
this.y1 = 400;
this.x2 = this.x1;
this.y2 = this.y1;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(this.x1,this.y1,this.x2,this.y2);
System.out.println("PAINT");
//g.setColor(this.color);
}

public int getX1(){return this.x1;}
public int getX2(){return this.x2;}
public int getY1(){return this.y1;}
public int getY2(){return this.y2;}
public void setX1(int x){this.x1=x;}
public void setX2(int x){this.x2=x;}
public void setY1(int y){this.y1=y;}
public void setY2(int y){this.y2=y;}
public void changeColor(Color c){this.color=c;}
}

public Fenetre() {
super("Interpréteur");
setMinimumSize(new Dimension(500,500));
setMaximumSize(new Dimension(500,500));
cont = getContentPane();
cont.setLayout(new GridLayout());
pan = new Panneau();
cont.add(pan);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public Panneau getPan() {
return pan;
}
}
0