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

2D Java Discussion :

Suivre les dimensions de l'écran


Sujet :

2D Java

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2015
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 45
    Par défaut Suivre les dimensions de l'écran
    Bonjour,

    J'ai suivi ce très bon tuto pour m'initier au dessin avec java : http://duj.developpez.com/tutoriels/java/dessin/intro/

    J'ai réussi à tracer mes rectangles comme vous pouvez le voir sur la photo : Voir la photo ci-joint.

    Ces rectangles sont de dimension fixe. Je cherche à ce q'ils suivent dynamiquement la résolution de ma fenêtre (modification de leur taille au redimensionnement de la fenêtre).

    Voici tout le code de mon projet :

    MonMain1.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
     
    package b_deuxiemeRectangleJava;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.io.File;
    import java.io.IOException;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
    public class MonMain1 {
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		JCanvas jc = new JCanvas();
    		jc.setBackground(Color.WHITE);
     
    		jc.setPreferredSize(new Dimension(400,450));
    		Dimension dim  = new Dimension(100,100);
     
    		//***********************************XML*********************************
     
    		/*
    		 * Etape 1 : récupération d'une instance de la classe "DocumentBuilderFactory"
    		 */
    		final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    		try {
    			/*
    			 * Etape 2 : création d'un parseur
    			 */
    			final DocumentBuilder builder = factory.newDocumentBuilder();
     
    			/*
    			 * Etape 3 : création d'un Document
    			 */
    			final Document document= builder.parse(new File("b_deuxiemeRectangleJava/repertoire.xml"));
     
    			//Affiche du prologue
    			System.out.println("*************PROLOGUE************");
    			System.out.println("version : " + document.getXmlVersion());
    			System.out.println("encodage : " + document.getXmlEncoding());		
    			System.out.println("standalone : " + document.getXmlStandalone());
     
    			/*
    			 * Etape 4 : récupération de l'Element racine
    			 */
    			final Element racine = document.getDocumentElement();
     
    			//Affichage de l'élément racine
    			System.out.println("\n*************RACINE************");
    			System.out.println(racine.getNodeName());
     
    			/*
    			 * Etape 5 : récupération des personnes
    			 */
    			final NodeList racineNoeuds = racine.getChildNodes();
    			final int nbRacineNoeuds = racineNoeuds.getLength();
     
    			for (int i = 0; i<nbRacineNoeuds; i++) {
    				if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
    					final Element graphique = (Element) racineNoeuds.item(i);
     
    					//Affichage d'une personne
    					System.out.println("\n*************GRAPHIQUE************");
    					System.out.println("type : " + graphique.getAttribute("type"));
     
    					/*
    					 * Etape 6 : récupération du nom, du height, du width
    					 */
    					final Element nom = (Element) graphique.getElementsByTagName("nom").item(0);
    					final Element height = (Element) graphique.getElementsByTagName("height").item(0);
    					final Element width = (Element) graphique.getElementsByTagName("width").item(0);
     
    					//Affichage du nom, du height, du width
    					System.out.println("nom : " + nom.getTextContent());
    					System.out.println("height: " + height.getTextContent());
    					System.out.println("width: " + width.getTextContent());
     
    					//Création des graphiques
    					int hauteur = Integer.parseInt(height.getTextContent());
    					int largueur = Integer.parseInt(width.getTextContent());
    					jc.addDrawable(new RectangleDrawable(Color.BLACK,new Point(hauteur,largueur),dim));
    				}				
    			}			
    		}
    		catch (final ParserConfigurationException e) {
    			e.printStackTrace();
    		}
    		catch (final SAXException e) {
    			e.printStackTrace();
    		}
    		catch (final IOException e) {
    			e.printStackTrace();
    		}
    		//********************************************************************************
     
     
    		GUIHelper.showOnFrame(jc,"Mes graphiques");
     
    	}
     
    }
    FormDrawable.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
     
    package b_deuxiemeRectangleJava;
     
    import java.awt.*;
     
     
    public abstract class FormDrawable implements IDrawable {
     
    	protected Rectangle rect ;
    	protected Color color;
     
    	public FormDrawable(Color color, Point pos, Dimension dim){
    		this.color=color;
    		this.rect = new Rectangle(pos,dim);
    	}
     
    	public abstract void draw(Graphics g) ;
     
    	public Rectangle getRectangle(){
    		return (Rectangle) rect.clone();
    	}
     
    }
    GUIHelper.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
     
    package b_deuxiemeRectangleJava;
     
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
     
    public class GUIHelper {
     
    	public static void showOnFrame(JComponent component, String frameName) {
    		JFrame frame = new JFrame(frameName);
    		WindowAdapter wa = new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		};
    		frame.addWindowListener(wa);
    		frame.getContentPane().add(component);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    }
    jCanvas.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
     
    package b_deuxiemeRectangleJava;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
     
    import javax.swing.JPanel;
     
    public class JCanvas extends JPanel {
     
    private List drawables = new LinkedList();
     
    	public void paint(Graphics g) {
    		super.paint(g);
    		for (Iterator iter = drawables.iterator(); iter.hasNext();) {
    			IDrawable d = (IDrawable) iter.next();
    			d.draw(g);	
    		}
    	}
     
    	public void addDrawable(IDrawable d) {
    		drawables.add(d);
    		repaint();
    	}
     
    	public void removeDrawable(IDrawable d) {
    		drawables.remove(d);
    		repaint();
    	}
     
    	public void clear() {
    		drawables.clear();
    		repaint();
    	}
     
    }
    IDrawable.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    package b_deuxiemeRectangleJava;
     
    import java.awt.Graphics;
    import java.awt.Rectangle;
     
    public interface IDrawable {
     
    	public  void draw(Graphics g);
     
    	public Rectangle getRectangle();
     
    }
    RectangleDrawable.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 b_deuxiemeRectangleJava;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
     
    public class RectangleDrawable extends FormDrawable {
     
     
    	public RectangleDrawable(Color color, Point pos, Dimension dim) {
    		super(color, pos, dim);
     
    	}
     
    	public void draw(Graphics g) {
    		Color c = g.getColor();
    		g.setColor(color);
    		//g.fillRect(rect.x,rect.y,rect.height,rect.width); //Rectangle plein
    		g.drawRect(rect.x,rect.y,rect.height,rect.width);
    		g.setColor(c);
    	}
    }
    Le fichier Xml où je viens piocher les dimensions de mes rectangles
    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
     
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <repertoire>
     
        <graphique type="linéaire">
            <nom>Graph1</nom>
    		<height>10</height>
    		<width>10</width>	
        </graphique>
     
    	<graphique type="barchart">
    		<nom>Graph2</nom>
    		<height>150</height>
    		<width>10</width>	
    	</graphique>
     
    	<graphique type="piechart">
    		<nom>Graph3</nom>
    		<height>290</height>
    		<width>10</width>	
    	</graphique>
     
    	<graphique type="linéaire">
            <nom>Graph4</nom>
    		<height>10</height>
    		<width>150</width>	
        </graphique>
     
    	<graphique type="barchart">
    		<nom>Graph5</nom>
    		<height>150</height>
    		<width>150</width>	
    	</graphique>
     
    	<graphique type="piechart">
    		<nom>Graph6</nom>
    		<height>290</height>
    		<width>150</width>	
    	</graphique>
     
    	<graphique type="linéaire">
            <nom>Graph7</nom>
    		<height>10</height>
    		<width>300</width>	
        </graphique>
     
    	<graphique type="barchart">
    		<nom>Graph8</nom>
    		<height>150</height>
    		<width>300</width>	
    	</graphique>
     
    	<graphique type="piechart">
    		<nom>Graph9</nom>
    		<height>290</height>
    		<width>300</width>	
    	</graphique>
     
    </repertoire>
    Merci d'avance pour votre aide !
    Images attachées Images attachées  

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 14/10/2013, 10h24
  2. [Lazarus] Récupérer les dimensions d'une fenêtre en plein écran
    Par Modulpro dans le forum Lazarus
    Réponses: 6
    Dernier message: 03/05/2011, 16h05
  3. prendre les memes dimensions que l'écran
    Par M E H D I dans le forum JBuilder
    Réponses: 2
    Dernier message: 02/06/2008, 00h34
  4. Récupérer les dimensions de mon image.
    Par Trilby dans le forum ASP
    Réponses: 3
    Dernier message: 10/02/2005, 09h52
  5. [Sprite] Comment les afficher directement à l'écran
    Par fror dans le forum C++Builder
    Réponses: 3
    Dernier message: 05/03/2004, 16h20

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