|
|
|
|
Bonjour,
Bonjour
voici ma classe
public class graph extends JPanel{
static List<point> T=new ArrayList<point>();
static double angle;
static void avancerCar() {
try {
carCourant = (char) System.in.read();
} catch (java.io.IOException e) {
}
}
static void lireExpression() throws InterruptedException{
if (carCourant=='f'){avancerCar();lireDeplacement();}
else if (carCourant=='r'){avancerCar();lireRotation();}
else throw new Error (" Carac inattendu : ");
}
static void lireDeplacement() throws InterruptedException{
if (Character.isDigit(carCourant)){T.add(new point(carCourant*Math.cos(angle),carCourant*Math.sin(angle)));avancerCar();}
else throw new Error (" Carac inattendu : ");
}
static void lireRotation() throws InterruptedException{
if(Character.isDigit(carCourant)){angle+=(int)(carCourant);avancerCar();}
else throw new Error (" Carac inattendu : ");
}
public void paint(Graphics g){
g.drawString("bonjour", 100, 100);
if (T.size()>1){
for (int i=0;i<T.size()-1;i++){
g.drawLine(T.get(i).abs, T.get(i).ord, T.get(i+1).abs-T.get(i).abs, T.get(i+1).ord-T.get(i).ord);
}
}
}
public static void main(String[] args) throws InterruptedException {
lex2.avancerCar () ;
avancerCar() ; // initialisation du car courant
T.add(new point(0,0));
while (carCourant != ';'){
lireExpression();
}
for (int i=0;i<T.size();i++){
System.out.println(T.get(i).abs+".."+T.get(i).ord);
}
}
}
class point{
int abs,ord;
public point(double a,double o){abs=(int) (250+a);ord=(int) (250+o);}
}
Configuration: Windows XP Firefox 2.0.0.14
Bonjour,
public static void main(String[] args) throws InterruptedException {
Graph mongraph = new Graph();
...
mongraph.repaint();
...
}
Par contre, tu devras retirer tous les static de ta classe Graph désormais. Bien cordialement ! |
Bonsoir !
public class Graph extends JPanel{
char carCourant;
...
void avancerCar() {
try {
carCourant = (char) System.in.read();
} catch (java.io.IOException e) {
}
}
...
Ainsi, toutes tes méthodes qui utiliseront carCourant pourront être passées en non-static. |
Salut
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
public class test extends JApplet {
static List<point> T=new ArrayList<point>();
double angle=-Math.PI/2;;
char carCourant;
int x,y;
void avancerCar() {
try {
carCourant = (char) System.in.read();
} catch (java.io.IOException e) {
}
}
void lireExpression() throws InterruptedException {
if (carCourant=='f'){avancerCar();
int v=Integer.parseInt(carCourant+"");
x+=10*v*Math.cos(angle);
y+=10*v*Math.sin(angle);
T.add(new point(x,y));
avancerCar();
}
if (carCourant=='r'){avancerCar();
int v=Integer.parseInt(carCourant+"");
angle+=10*v*Math.PI/180;
avancerCar();
}
}
/*
static void lireExpression() throws InterruptedException{
if (carCourant=='f'){avancerCar();lireDeplacement();}
if (carCourant=='r'){avancerCar();lireRotation();}
//else throw new Error (" Carac inattendu : ");
}
static void lireDeplacement() throws InterruptedException{
if (Character.isDigit(carCourant)){
x+=10*Integer.parseInt(carCourant+"")*Math.cos(angle);
y+=10*Integer.parseInt(carCourant+"")*Math.sin(angle);
T.add(new point(x,y));
avancerCar();}
//else throw new Error (" Carac inattendu : ");
}
static void lireRotation() throws InterruptedException{
if(Character.isDigit(carCourant)){angle+=10*Integer.parseInt(carCourant+"")*Math.PI/180;avancerCar();}
//else throw new Error (" Carac inattendu : ");
}
*/
public void init(){
avancerCar() ; // initialisation du car courant
T.add(new point(0,0));x=y=0;
while (carCourant != ';'){
try {
lireExpression();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i=0;i<T.size();i++){
System.out.println(T.get(i).abs+".."+T.get(i).ord);
}
}
public void paint(Graphics g){
g.drawString("bonjour", 100, 100);
if (T.size()>1){g.setColor(Color.blue);
for (int i=0;i<T.size()-1;i++){
g.drawLine(T.get(i).abs, T.get(i).ord, T.get(i+1).abs, T.get(i+1).ord);
}
}
}
public static void main(String args[]) {
new test();
}
}
class point{
int abs,ord;
public point(double a,double o){abs=(int) (100+a);ord=(int) (150+o);}
}
|
Re,
|
Tu lances le programme en Applet
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
public class test extends JApplet {
static List<point> T=new ArrayList<point>();
double angle=-Math.PI/2;;
char carCourant;
int x,y;
void avancerCar() {
try {
carCourant = (char) System.in.read();
} catch (java.io.IOException e) {
}
}
void lireExpression() throws InterruptedException {
if (carCourant=='f'){avancerCar();
int v=Integer.parseInt(carCourant+"");
x+=10*v*Math.cos(angle);
y+=10*v*Math.sin(angle);
T.add(new point(x,y));
avancerCar();repaint();
}
if (carCourant=='r'){avancerCar();
int v=Integer.parseInt(carCourant+"");
angle+=10*v*Math.PI/180;
avancerCar();
}
}
/*
static void lireExpression() throws InterruptedException{
if (carCourant=='f'){avancerCar();lireDeplacement();}
if (carCourant=='r'){avancerCar();lireRotation();}
//else throw new Error (" Carac inattendu : ");
}
static void lireDeplacement() throws InterruptedException{
if (Character.isDigit(carCourant)){
x+=10*Integer.parseInt(carCourant+"")*Math.cos(angle);
y+=10*Integer.parseInt(carCourant+"")*Math.sin(angle);
T.add(new point(x,y));
avancerCar();}
//else throw new Error (" Carac inattendu : ");
}
static void lireRotation() throws InterruptedException{
if(Character.isDigit(carCourant)){angle+=10*Integer.parseInt(carCourant+"")*Math.PI/180;avancerCar();}
//else throw new Error (" Carac inattendu : ");
}
*/
public void init(){
avancerCar() ; // initialisation du car courant
T.add(new point(0,0));x=y=0;
while (carCourant != ';'){
try {
lireExpression();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i=0;i<T.size();i++){
System.out.println(T.get(i).abs+".."+T.get(i).ord);
}
}
public void paint(Graphics g){
g.drawString("bonjour", 100, 100);
if (T.size()>1){g.setColor(Color.blue);
for (int i=0;i<T.size()-1;i++){
g.drawLine(T.get(i).abs, T.get(i).ord, T.get(i+1).abs, T.get(i+1).ord);
}
}
}
public static void main(String args[]) {
new test();
}
}
class point{
int abs,ord;
public point(double a,double o){abs=(int) (100+a);ord=(int) (150+o);}
}
|
Est ce qu'il est possible que la classe principale ne soit pas extens JAplplet
class palette extends JPanel{
String s;
public palette(String st){
s=st;
setVisible(true);
setSize(200,300);
}
public void paint(Graphics g){
g.drawString(s, 10, 10);
}
}
|
Voici ton code un peu modifié (notamment avec plus de classes).
Lanceur.java:
public class Lanceur {
public static void main(String args[]) {
LigneBriseeAffichage ligne = new LigneBriseeAffichage();
ligne.setVisible(true);
}
}
LigneBriseeAffichage.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class LigneBriseeAffichage extends JFrame {
private static final long serialVersionUID = 8474338316960569460L;
private LigneBrisee ligne = new LigneBrisee(this);
public LigneBriseeAffichage(){
setTitle("Générateur de lignes brisées");
setSize(320,320); //taille de la fenêtre
setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
setResizable(false); //On interdit la redimensionnement de la fenêtre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix
this.getContentPane().setBackground(Color.BLACK);
}
public void paint(Graphics g) {
if (ligne.getT().size() > 1) {
g.setColor(Color.blue);
for (int i = 0; i < ligne.getT().size() - 1; i++) {
g.drawLine(ligne.getT().get(i).abs, ligne.getT().get(i).ord,
ligne.getT().get(i + 1).abs,
ligne.getT().get(i + 1).ord);
}
}
}
}
LigneBrisee.java
import java.util.ArrayList;
import java.util.List;
public class LigneBrisee {
private LigneBriseeAffichage lba;
List<MonPoint> t = new ArrayList<MonPoint>();
double angle = -Math.PI / 2;;
char carCourant;
int x, y;
public LigneBrisee(LigneBriseeAffichage lba){
this.lba = lba;
new EcouteurConsole().start();
}
public class EcouteurConsole extends Thread {
public void run() {
try{
t.add(new MonPoint(0, 0));
x = y = 0;
while(true){
avancerCar();
if(carCourant != '#' ){
lireExpression();
}
else
break;
avancerCar();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
void lireExpression() throws InterruptedException {
while(carCourant != '\r'){
if (carCourant == 'f') {
avancerCar();
int v = Integer.parseInt(carCourant + "");
x += 10 * v * Math.cos(angle);
y += 10 * v * Math.sin(angle);
t.add(new MonPoint(x, y));
avancerCar();
}
if (carCourant == 'r') {
avancerCar();
int v = Integer.parseInt(carCourant + "");
angle += 10 * v * Math.PI / 180;
avancerCar();
}
lba.repaint();
}
}
void avancerCar() {
try {
carCourant = (char) System.in.read();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
public List<MonPoint> getT() {
return t;
}
public void setT(List<MonPoint> t) {
this.t = t;
}
}
MonPoint.java
public class MonPoint {
int abs, ord;
public MonPoint(double a, double o) {
abs = (int) (100 + a);
ord = (int) (150 + o);
}
}
Ca te va ? |
Salut
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JTextArea;
import java.awt.Dimension;
import javax.swing.JScrollPane;
public class rr extends JPanel {
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setPreferredSize(new Dimension(220, 169));
jTextArea.setSize(new Dimension(220, 169));
}
return jTextArea;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setPreferredSize(new Dimension(200, 164));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
char carCourant;
private JTextArea jTextArea = null;
private JScrollPane jScrollPane = null;
void avancerCar() {
try {
carCourant = (char) System.in.read();
} catch (java.io.IOException e) {
}
}
void lireExpression() throws InterruptedException {
if (carCourant=='f'){
jTextArea.append("forward");
avancerCar();repaint();
}
if (carCourant=='r'){
jTextArea.append("right");
avancerCar();
}
}
public rr() throws InterruptedException {
super();
initialize();
}
private void initialize() throws InterruptedException {
this.setSize(300, 200);
this.setLayout(new FlowLayout());
this.setVisible(true);
this.add(getJScrollPane(), null);
avancerCar() ; // initialisation du car courant
while (carCourant!=';'){
lireExpression();//repaint();
}
}
}
ce code ecrit des right et des forwards dans la frame si on ecrit des rffrf; dans la console in, suivi de entrée mais pourquoi la frame n'apparait pas des le lancement de l'app, et seulement apres que je tape entrée si je supprime cette portion de code (la lecture depuis la console), la frame appparait direct quand je lance l'app
while (carCourant!=';'){
lireExpression();//repaint();
}
enfin si je mets un while(true) import java.awt.Dimension; import javax.swing.JScrollPane; public class rr extends JPanel { private JTextArea getJTextArea() { if (jTextArea == null) { jTextArea = new JTextArea(); jTextArea.setPreferredSize(new Dimension(220, 169)); jTextArea.setSize(new Dimension(220, 169)); } return jTextArea; } private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(); jScrollPane.setPreferredSize(new Dimension(200, 164)); jScrollPane.setViewportView(getJTextArea()); } return jScrollPane; } public static void main(String[] args) { // TODO Auto-generated method stub } char carCourant; private JTextArea jTextArea = null; private JScrollPane jScrollPane = null; void avancerCar() { try { carCourant = (char) System.in.read(); } catch (java.io.IOException e) { } } void lireExpression() throws InterruptedException { if (carCourant=='f'){ jTextArea.append("forward"); avancerCar();repaint(); } if (carCourant=='r'){ jTextArea.append("right"); avancerCar(); } } public rr() throws InterruptedException { super(); initialize(); } private void initialize() throws InterruptedException { this.setSize(300, 200); this.setLayout(new FlowLayout()); this.setVisible(true); this.add(getJScrollPane(), null); avancerCar() ; // initialisation du car courant while (carCourant!=';'){ lireExpression();//repaint(); } } } </code> ce code ecrit des right et des forwards dans la frame si on ecrit des rffrf; dans la console in, suivi de entrée mais pourquoi la frame n'apparait pas des le lancement de l'app, et seulement apres que je tape entrée si je supprime cette portion de code (la lecture depuis la console), la frame appparait direct quand je lance l'app la frame n'apparait jamais
while (true){
lireExpression();//repaint();
}
|
Hello,
|