Repaint() qui n'appelle pas paintComponent()
benjtlv
Messages postés
28
Statut
Membre
-
benjtlv Messages postés 28 Statut Membre -
benjtlv Messages postés 28 Statut Membre -
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.
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.
A voir également:
- Repaint() qui n'appelle pas paintComponent()
- Appelle masqué - Guide
- Numéro 06 qui appelle et raccroche - Guide
- En préparant son diaporama, tom a pris quelques notes qui l'aideront lors de sa présentation. quand il présentera l'âne, il a prévu de raconter une anecdote sur cet animal. comment s'appelle l'âne de son histoire ? - Forum Windows
- Je peux appeler mais pas recevoir d'appel sur mon portable - Forum Téléphones & tablettes Android
- Dans le document à télécharger, trouvez les lettres situées derrière les rectangles pour reconstituer le nom du chat. comment s'appelle-t-il ? - Forum InDesign
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
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;
}
}