Erreur dans un for arrayList
Bonjour ,
Je ne comprend pas pourquoi javac me dit qu'il y a une erreur sur ma boucle for
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
public class TestMaclasse extends JApplet {
private JPanel panelDeBoutons;
private JButton augmenterPierre;
private JButton reduireAngle;
private JButton augmenterAngle;
private JButton deplacerVite;
private JButton deplacer;
private JButton deplacerTresVite;
private JButton DemiTour;
private JButton predateur;
private JPanel canvas;
private JButton diminuerPierre;
private ArrayList<Maclasse> listRectangles;
private ArrayList<Chenille> ListEnnemi;
private Chenille maChenille ;
boolean a;
int tailleRectangle=2;
int k=-tailleRectangle;
private class Canvas extends JPanel {
public Canvas() {
super();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(5,5,Cercle.xMax-5,Cercle.yMax-5);
maChenille.dessiner(g);
for (Chenille ennemi : ListEnnemi) {
ennemi.dessiner(g);}
for (Maclasse pierre : listRectangles) {
pierre.dessiner(g);
for(int i=0;i<360;i++){
g.drawLine((int)(pierre.getX()+pierre.getLargeur()/2 + maChenille.getGrandDiametreTete()*Math.cos(i*Math.PI/180)),(int)(pierre.getY()+pierre.getHauteur()/2+maChenille.getGrandDiametreTete()*Math.sin(i*Math.PI/180)
),(int)(pierre.getX()+pierre.getLargeur()/2 +0.000001 + maChenille.getGrandDiametreTete()*Math.cos(i*Math.PI/180)),(int)(pierre.getY()+pierre.getHauteur()/2+maChenille.getGrandDiametreTete()*Math.sin(i*Math.PI/180)) );
}}
}
}
public TestMaclasse() {
listRectangles = new ArrayList<Maclasse>();
}
public void InitTabBoolean( boolean k []) {for(int i=0;i<k.length;i++){k[i]=false;}}
public void IsRencontre(){
for(Maclasse pierre : listRectangles){System.out.println(maChenille.getXTete()-pierre.getX()+pierre.getLargeur()/2);
System.out.print(maChenille.getYTete()-pierre.getY()+pierre.getHauteur()/2);
{
if((maChenille.getXTete()-pierre.getX()+pierre.getLargeur()/2)*(maChenille.getXTete()-pierre.getX()+pierre.getLargeur()/2)+(maChenille.getYTete()-pierre.getY()+pierre.getHauteur()/2)*(maChenille.getYTete()-pierre.getY()+pierre.getHauteur()/2)
<=maChenille.getGrandDiametreTete()*maChenille.getGrandDiametreTete()
)
{
maChenille.setCapTete(maChenille.getCapTete()+180);System.out.println(true);}
}}}
@Override
public void init() {
setSize(500, 500);
setLayout(new BorderLayout());
panelDeBoutons = new JPanel();
panelDeBoutons.setLayout(new FlowLayout(FlowLayout.CENTER));
maChenille = new Chenille(15,10,45,Color.blue);
augmenterPierre = new JButton("+ Pierre");
augmenterPierre.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Maclasse rect = new Maclasse();
rect.setX((int) (Math.random() * 500));
rect.setY((int) (Math.random() * 500));
rect.setHauteur(2);
rect.setLargeur(2);
rect.setColor(Color.BLACK);
listRectangles.add(rect);
canvas.repaint();}
}
);
panelDeBoutons.add(augmenterPierre);
diminuerPierre = new JButton("- Pierre");
diminuerPierre.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(listRectangles.size()>0){
listRectangles.remove(listRectangles.size()-1);
canvas.repaint();}
}}
);
panelDeBoutons.add(diminuerPierre);
reduireAngle = new JButton("-");
reduireAngle.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {if(maChenille.getAngleDeVision()>0) {maChenille.setAngleDeVision((maChenille.getAngleDeVision())-5) ; canvas.repaint();}}}
);
panelDeBoutons.add(reduireAngle);
augmenterAngle = new JButton("+");
augmenterAngle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (maChenille.getAngleDeVision()<180){maChenille.setAngleDeVision((maChenille.getAngleDeVision())+5) ;
repaint();}
}
});
panelDeBoutons.add(augmenterAngle);
deplacer = new JButton("GO");
deplacer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IsRencontre();
maChenille.deplacer();canvas.repaint();
}});
panelDeBoutons.add(deplacer);
deplacerVite = new JButton("GOO");
deplacerVite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{for(int i=0 ; i<5 ; i++) {IsRencontre();
maChenille.deplacer();{;}canvas.repaint() ;}}
});
panelDeBoutons.add(deplacerVite);
deplacerTresVite = new JButton("GOO");
deplacerTresVite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{for(int i=0 ; i<10 ; i++) {IsRencontre();
maChenille.deplacer();{;}canvas.repaint() ;}
}});
panelDeBoutons.add(deplacerTresVite);
DemiTour = new JButton("Demi tour");
DemiTour.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {maChenille.setCapTete(maChenille.getCapTete()+180);maChenille.deplacer(); canvas.repaint();}}
);
panelDeBoutons.add(DemiTour);
predateur = new JButton("+ predateur");
predateur.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Chenille ennemi = new Chenille(0, 10, 45, Color.RED);
ennemi.setLongeur(0);ennemi.setAngleDeVision(45);
ListEnnemi.add(ennemi);
canvas.repaint();}}
);
panelDeBoutons.add(predateur);
{
canvas = new Canvas();
canvas.setBackground(Color.WHITE);
add(panelDeBoutons, BorderLayout.NORTH);
add(canvas, BorderLayout.CENTER);
invalidate();
validate();
}}
} |
l'erreur est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at TestMaclasse$Canvas.paintComponent(TestMaclasse.java:46)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) |
Merci d'avance