Débutant JFrame

Résolu
monstor_rox Messages postés 18 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 15 janvier 2007 - 12 nov. 2005 à 11:38
bastet1978 Messages postés 54 Date d'inscription lundi 29 septembre 2003 Statut Membre Dernière intervention 4 décembre 2005 - 14 nov. 2005 à 23:49
Bonjour

J'ai besoin de votre aide car je n'ai jamais fait d'interfaces
graphiques en java et là j'ai un exercice qui va vous paraitre tout
bête mais je n'arrive pa a le faire.

Le voici : Créer une application
TestGraphic
, héritant de
JFrame
. Créer
un composant
DrawComponent
, inscrit dans
TestGraphic
,
qui affiche 3 cercles et un rectangle, dont voici les caractéristiques
:


Circle1 x 200,y 200,r = 100 ;


Circle2 x 160,y 150,r = 20 ;


Circle3 x 240,y 150,r = 20


et le Rectangle x 150,y 220,w = 100, h = 40. (voir 3.0)





Merci de votre aide

4 réponses

bastet1978 Messages postés 54 Date d'inscription lundi 29 septembre 2003 Statut Membre Dernière intervention 4 décembre 2005
14 nov. 2005 à 23:49
Je comprends pas.

Je viens de tester et ça marche:

/*

* TestGraphics.java

*

* Created on 14 novembre 2005, 23:40

*

*/

import java.awt.Container;

import java.awt.BorderLayout;

import java.awt.Toolkit;



/**

*

* @author ANDRE Alain

*/

public class TestGraphics extends javax.swing.JFrame {



/**

* TestGraphics

*/

public TestGraphics() {

super("TestGraphics"); // un titre

DrawComponent dc = new DrawComponent(); // un panel pour dessiner.

Container c = getContentPane(); // de quoi ajouter le panel

c.setLayout(new BorderLayout()); // ajoute un layout (nors, sud, est, ouest, centre)

c.add(dc, BorderLayout.CENTER); // ajout du panel au JFrame au centre



// visualisation

int width = 800;

int height = 600;

int w = (int) Toolkit.getDefaultToolkit().getScreenSize().width;

int h = (int) Toolkit.getDefaultToolkit().getScreenSize().height;

setSize(width, height);

setLocation((w/2)-(width/2), (h/2)-(height/2));

setVisible(true);

}

public static void main(String[] args) {

TestGraphics tg = new TestGraphics();

tg.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

}



}



/*

* DrawComponent.java

*

* Created on 14 novembre 2005, 23:39

*

*/



import java.awt.Graphics;

import java.awt.Dimension;



/**

*

* @author ANDRE Alain

*/

public class DrawComponent extends javax.swing.JPanel{



/**

* DrawComponent

*/

public DrawComponent() {

}



// partie pour dessiner

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// cercle

g.drawArc(200,200, 100, 100, 0, 360);

}

public Dimension getMinimumSize(){

return new Dimension(800, 600);

}

public Dimension getPreferredSize(){

return getMinimumSize();

}

public Dimension getMaximumSize(){

return getMinimumSize();

}

}


----
OS: Ubuntu 5.10(linux)
jdk : 1.5.0
MysqlConnector/J: 3.2
mysql Ver 12.22 Distrib 4.0.24, for pc-linux-gnu (i486)
----
3
bastet1978 Messages postés 54 Date d'inscription lundi 29 septembre 2003 Statut Membre Dernière intervention 4 décembre 2005
12 nov. 2005 à 15:53
Bonjour,

Les JFrame sont simples voici le minimum à faire:

JFrame placée au centre de l'écran de taille 800,600 nommée TestGraphics :
public class TestGraphics extends javax.swing.JFrame {

public CreerSculpture() {

super("TestGraphics"); // un titre

DrawComponent dc = new DrawComponent; // un panel pour dessiner.

Container c = getContentPane(); // de quoi ajouter le panel

c.setLayout(new BorderLayout()); // ajoute un layout (nors, sud, est, ouest, centre)

c.add(dc, BorderLayout.CENTER); // ajout du panel au JFrame au centre



// visualisation

int width = 800;

int height = 600;

int w = (int) Toolkit.getDefaultToolkit().getScreenSize().width;

int h = (int) Toolkit.getDefaultToolkit().getScreenSize().height;

setLocation((w/2)-(width/2), (h/2)-(height/2));

setVisible(true);

}

public static void main(String[] args) {

TestGraphics tg = new TestGraphics();

}

}



Class DrawComponent (un JPanel overwrited pour dessiner) voici le minimum:

import java.awt.Graphics;

import java.awt.Dimension;

public class DrawComponent extends javax.swing.JPanel {

public DrawComponent(){

}



// partie pour dessiner

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// cercle

g.drawArc(200,200, 100, 100, 0, 360);

}

public Dimension getMinimumSize(){

return new Dimension(800, 600);

}

public Dimension getPreferredSize(){

return getMinimumSize();

}

public Dimension getMaximumSize(){

return getMinimumSize();

}

}



A toi de trouver le reste, ça devrait aller vite.
----
OS: Ubuntu 5.10(linux)
jdk : 1.5.0
MysqlConnector/J: 3.2
mysql Ver 12.22 Distrib 4.0.24, for pc-linux-gnu (i486)
----
0
bastet1978 Messages postés 54 Date d'inscription lundi 29 septembre 2003 Statut Membre Dernière intervention 4 décembre 2005
13 nov. 2005 à 15:59
PS: j'ai oublie de mettre 'setSize(width, height);' avant la fonction 'setLocation()' dans la classe TestGraphics.

----
OS: Ubuntu 5.10(linux)
jdk : 1.5.0
MysqlConnector/J: 3.2
mysql Ver 12.22 Distrib 4.0.24, for pc-linux-gnu (i486)
----
0
monstor_rox Messages postés 18 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 15 janvier 2007
13 nov. 2005 à 16:13
merci beaucoup mais j'ai 3 warning :
- The serializable class DrawComponent does not declare a static final serialVersionUID field of type long DrawComponent.
- The serializable class TestGraphic does not declare a static final serialVersionUID field of type long TestGraphic.java
- The local variable tg is never read TestGraphic.java
0
Rejoignez-nous