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
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
A voir également:
- Repaint() qui n'appelle pas paintComponent()
- Appelle masqué - Guide
- Numéro 04 qui appelle et raccroche ✓ - Forum Mobile
- Téléphone qui appelle tout seul - Forum Mobile
- Messenger ne sonne pas quand on m'appelle - Forum Messagerie
- Numéro 01 qui appelle tout le temps - Forum Mobile
1 réponse
KX
Messages postés
16668
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
17 mars 2023
3 005
21 avril 2015 à 18:03
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...
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...
21 avril 2015 à 23:40
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;
}
}