Rafraichissement graphique d'un robot en simulation
cs_SuperSteph
Messages postés5Date d'inscriptiondimanche 21 juillet 2002StatutMembreDernière intervention23 juin 2003
-
4 juin 2003 à 16:49
kazary
Messages postés2Date d'inscriptionjeudi 29 décembre 2005StatutMembreDernière intervention10 décembre 2007
-
10 déc. 2007 à 22:07
On est en train de faire un projet qui simule un robot dynamiquement.
On a reussi à faire evoluer le robot par contre on a une "trainéé". On aimerait voir le robot se deplacer sans visualiser ses positions precedentes.
On vous envoie un bout de code esperant que qq'un pourra nous aider.
Cette classe est la classe qui nous permet d'afficher le robot
package ihm;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import Graphics.Robot.RobotGraphic;
import Graphics.Robot.RobotModel;
import Graphics.swing.SwingRenderer;
import Graphics.swing.SwingRobotAttributes;
import Graphics.swing.SwingRobotRenderer;
import Robots.SimulatorEvent;
import Robots.SimulatorListener;
public class RobotView extends View implements SimulatorListener
{
RobotGraphic robotGraphic;
public RobotView(JFrame owner, RobotModel model)
{
super(owner, model);
setupGraphics((RobotModel) model);
}
public RobotView(RobotModel model)
{
super(model);
setupGraphics((RobotModel) model);
}
public RobotModel getRobotModel()
{
return ((RobotModel) getModel());
}
public void setModel(RobotModel model)
{
super.setModel(model);
setupGraphics((RobotModel) model);
}
protected void setupGraphics(RobotModel model)
{
SwingRobotAttributes attributes = new SwingRobotAttributes();
attributes.setColor(Color.green);
attributes.filled();
Rectangle drawArea = new Rectangle(this.getPreferredSize().width,this.getPreferredSize().height);
robotGraphic = new RobotGraphic(drawArea, getRobotModel());
robotGraphic.setSize(30);
SwingRobotRenderer renderer = new SwingRobotRenderer(attributes, robotGraphic);
robotGraphic.setRenderer(renderer);
}
public void paintComponent(Graphics graphics)
{
((SwingRenderer)robotGraphic.getRenderer()).getAttributes().setGraphics(graphics);
robotGraphic.draw();
System.out.println(robotGraphic.getModel());
}
public void update(SimulatorEvent evt)
{
this.paintImmediately(this.getBounds());
}
}
Cette classe est la classe principale
package VectorTest;
import ihm.RobotView;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import Graphics.Robot.DefaultRobotModel;
import Robots.RobotPoint;
import Robots.Simulator;
import Value.BoundedValue;
public class GraphicTest extends JFrame
{
private DefaultRobotModel robotModel;
private RobotView robotView;
private Simulator simulator ;
public GraphicTest(String title)
{
super(title);
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent evt)
{
System.exit(0);
}
});
//setup robot model
BoundedValue x = new BoundedValue(50, 0, 500);
BoundedValue y = new BoundedValue(50, 0, 500);
robotModel = new DefaultRobotModel(x, y, 2);
RobotPoint robotPoint = new RobotPoint(x.getValue(), y.getValue(), 0);
robotModel.setRobot(robotPoint);
//setup robot view
robotView = new RobotView(robotModel);
robotView.setPreferredSize(new Dimension(500,500));
getContentPane().add(robotView, BorderLayout.NORTH);
//setup and start simulator
simulator = new Simulator(robotPoint);
simulator.addListener(robotView);
}
public void simule()
{
//simulator.simuleCartesian(1, 5, 2, 100, 100);
}
public static void main(String[] args)
{
GraphicTest g = new GraphicTest("robot");
g.show();
g.pack();
g.simule();
}
}
mrshardax
Messages postés47Date d'inscriptionjeudi 6 février 2003StatutMembreDernière intervention22 mars 20062 4 juin 2003 à 19:35
alors, je ne sais pas si cette methode est cree ou heritee, j ai pas pu trouver bcp de javadoc la ou je suis.
public void paintComponent(Graphics graphics)
Si elle est heritee, verifie la syntaxe ( le plus souvent, c Graphics g) de une, mais surtout, et c valable pour tout les methodes graphiques heritees, n oublie pas d appeler les methodes parentes en tout debut de fonction par un :