IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Problème d'affichage de Balle(s) à l'écran sur la fenêtre d'une appli java sous NetBeans


Sujet :

AWT/Swing Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    62
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 62
    Points : 34
    Points
    34
    Par défaut Problème d'affichage de Balle(s) à l'écran sur la fenêtre d'une appli java sous NetBeans
    Bonjour,

    Je souhaite afficher la vue d'une balle blanche sur un écran noir sur ma fenêtre de mon application java sous NetBeans au clic de la souris.

    J'ai 3 fichiers :

    Main.java :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package balles;
     
    import javax.swing.*;
     
    public class Main {
     
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Balles");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
     
     
    		Balles ballCanvas = new Balles();
     
    		frame.getContentPane().add(ballCanvas);
    		frame.pack();
    		frame.setVisible(true);
     
    		ballCanvas.start();
     
    	}
     
    }
    Balles.java :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package balles;
     
    import java.awt.Dimension;
    import java.awt.Canvas;
    import java.awt.RenderingHints;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferStrategy;
    import java.util.Random;
     
    /**
     *
     * @author
     */
    public class Balles extends Canvas {
     
        // Rendering / Buffer objects
    	private BufferStrategy strategy;
    	private Graphics2D g2;
     
        // Ball objects
        private Ball[] balls = new Ball[5000];
        private Ball tempBall;
        private Ball currentBall;
        private int ballCount = 0;
     
        public Balles() {
     
        setPreferredSize(new Dimension(800, 600));
    	setIgnoreRepaint(true);
     
            // Evénement(s) clic
    		MouseHandler mouseHandler = new MouseHandler();
    		addMouseMotionListener(mouseHandler);
    		addMouseListener(mouseHandler); 
                }
     
        	public void start()
    	{
    		mainStart();
    	}
     
            	public void mainStart()
    	{
                    lance();
     
            }
     
        	public void generateBalls(int numBalls)
    	{
    		Random rand = new Random();
    		for (int i = 0; i < numBalls; i++)
    		{
    		   Ball tempBall = new Ball(rand.nextInt(10) + getWidth()/2, rand.nextInt(10) + getHeight()/2, 10);
    		   balls[ballCount] = tempBall;
    		   ballCount++;
    		}
    	}
     
              	public void lance()
    	{
     
    		if (strategy == null || strategy.contentsLost())
    		{
    			// Creation BufferStrategy pour affichage
    			createBufferStrategy(2);
    			strategy = getBufferStrategy();
    			Graphics g = strategy.getDrawGraphics();
    			this.g2 = (Graphics2D) g;
    		}
     
     
    		this.g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
     
     
    		// Affichage arrière plan
    		this.g2.setColor(Color.BLACK);
    		this.g2.fillRect(0, 0, getWidth(), getHeight());
     
    		// Affichage des objets
    		for(int i = 0; i < ballCount; i++)
    		{
    			balls[i].draw(this.g2);
    		}
     
    		Ball tempBall = currentBall;
    		if (tempBall != null) tempBall.draw(this.g2);
     
     
      		if (!strategy.contentsLost()) strategy.show();
     
    	}
     
        	private class MouseHandler extends MouseAdapter implements MouseMotionListener
    	{
    		public void mousePressed(MouseEvent e)
                    {
     
                    Ball tempBall = new Ball(e.getX(), e.getY(), 15);         
     
                    }
     
                    public void mouseReleased(MouseEvent e)
                    {
                    balls[ballCount] = tempBall;
    		ballCount++;
                    }
            }
     
    public class Ball implements Comparable<Ball>{
     
    	public Vector2d position;
    	private float radius;
     
    	public Ball(float x, float y, float radius)
    	{
    		this.position = new Vector2d(x, y);
    		this.setRadius(radius);
    	}
     
     	public void draw(Graphics2D g2)
    	{
    		g2.setColor(new Color(255, 255, 255));
    		g2.fillOval( (int) (position.getX() - getRadius()), (int) (position.getY() - getRadius()), (int) (2 * getRadius()) , (int) (2 * getRadius()) );
     
    	}
     
    	public void setRadius(float radius) {
    		this.radius = radius;
    	}
     
    	public float getRadius() {
    		return radius;
    	}
     
            	public int compareTo(Ball ball) {
     
                        	if (this.position.getX() - this.getRadius() > ball.position.getX() - ball.getRadius())
    		{
    			return 1;
    		}
    		else if (this.position.getX() - this.getRadius() < ball.position.getX() - ball.getRadius())
    		{
    			return -1;
    		}
    		else
    		{
    			return 0;
    		}
     
                    }
    }
     
    }
    Vector2d.java :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package balles;
     
    public class Vector2d {
     
    	private float x;
    	private float y;
     
    		public Vector2d(float x, float y)
    	{
    		this.setX(x);
    		this.setY(y);
    	}
     
    		public void setX(float x) {
    		this.x = x;
    	}
     
                    public float getX() {
    		return x;
    	}
     
    		public void setY(float y) {
    		this.y = y;
    	}
     
                    public float getY() {
    		return y;
    	}
     
    }
    Le programme ne m'affiche pas d'erreur(s) à l'écran ni sur le terminal de sortie.

    Mais il n'y a pas de balle blanche au clic de la souris.

    Merci pour votre aide

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Déjà, en l’état, vu que tu ne stockes pas ta nouvelle balle, il va rien se passer. Donc au lieu de :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    public void mousePressed(MouseEvent e) {
        Ball tempBall = new Ball(e.getX(), e.getY(), 15);
    }
    Il faudrait faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    public void mousePressed(MouseEvent e) {
        tempBall = new Ball(e.getX(), e.getY(), 15);
    }
    Ensuite si tu avais mis un break point dans la méthode de rendu de la balle et que tu avais daigné faire tourner le débogueur tu aurais vu que cette méthode est jamais invoquée. T'est pas sensé faire une boucle pour rafraîchir le contenu de ton BufferStrategy, comme c'est clairement indiqué dans l'exemple donné dans la doc de la classe ?

    Maintenant, as-tu réellement besoin d'utiliser un BufferStrategy (peut-être, je ne sais pas. Quels sont tes besoins ?) ? Un rendu traditionnel AWT / Swing n’était pas suffisant ?
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    62
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 62
    Points : 34
    Points
    34
    Par défaut
    Merci bouye,

    Grâce à tes indications, le rendu souhaité fonctionne correctement.

    Mon sujet est maintenant résolu.

    Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Turbo Pascal] Problème d'affichage du résultat final à l'écran
    Par DAMIDAMI dans le forum Turbo Pascal
    Réponses: 2
    Dernier message: 18/02/2018, 09h28
  2. Problème d'affichage entre 2 résolutions d'écran
    Par AntoineLep dans le forum VB.NET
    Réponses: 2
    Dernier message: 29/08/2012, 22h04
  3. [Bonita] problème d'affichage de la boite de dialogue sur une étape
    Par Mr_informatique dans le forum Autres Solutions d'entreprise
    Réponses: 1
    Dernier message: 28/07/2010, 14h07
  4. Problème d'affichage d'une applet java sous Linux
    Par Dave Ridic dans le forum Applets
    Réponses: 1
    Dernier message: 29/05/2008, 14h51
  5. Réponses: 10
    Dernier message: 28/01/2007, 17h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo