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

Composants Java Discussion :

Sismographe dans un JScrollePane


Sujet :

Composants Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2014
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2014
    Messages : 12
    Points : 9
    Points
    9
    Par défaut Sismographe dans un JScrollePane
    Bonjour,

    Je dois réaliser une sorte de sismographe en Java. Je reçois des informations, valeurs numériques, et je dois dessiner des lignes horizontales.

    Je dessine ces lignes avec g.drawLine dans un JPanel qui lui est dans un JScrollePane.

    Un ascenseur dois apparaitre lorsque j'ai un grand nombre de lignes ce qui dois me permettre de visualiser les précédents relevé, or impossible de déclencher l'ascenseur.

    Ci dessous mon code qui me permet de dessiner mes courbes (qui ressemble plus a un histogramme j'en convient)

    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
    public class ScreenPrinter extends JPanel implements Scrollable{
     
    	private int tabValue[] = new int[2048];
     
    	private int maxUnitIncrement = 1;
     
    	private ArrayList<Integer> list = new ArrayList< Integer>();
     
    	/**
             * Constructeur par defaut
             */
    	public ScreenPrinter(int inMaxUnitIncrement){
     
    		setSize(300, 960);
     
    		for(int i = 0; i < 2048; i++)
    		{
    			double doubleVal = Math.random();
    			int    intVal    = (int) (doubleVal * 2048) ;
    			tabValue[i] = intVal ; 
    		}
     
    		maxUnitIncrement = inMaxUnitIncrement;
    	}
     
    	public void AddLine(){
     
    		System.out.println("add a line");
     
    		double doubleVal = Math.random();
    		int    intVal    = (int) (doubleVal * 2048) ;
     
    		list.add(intVal);
     
    		this.repaint();
    	}
     
    	public void paintComponent(Graphics g){
     
    		int widthJPanel  = this.getWidth() ;
    		int heightJPanel = this.getHeight() ;
     
    		g.setColor(new Color(1.0f, 0.0f, 0.0f)); // blanc
    	    g.fillRect(0, 0, widthJPanel, heightJPanel);
    	    g.setColor(new Color(0.0f, 0.0f, 0.0f)); // noir
     
    	    if(list.size() == 0)
    	    	return ;
     
    	    int cpt = 0 ;
     
    	    // on doit inverser la descente du schema pour avoir l'effet "sismographe"
    	    ArrayList<Integer> reverseList = new ArrayList< Integer>();
     
    	    for(int i=list.size()-1; i>=0; i--)
    	    	reverseList.add(list.get(i));
     
    	    for(Integer oneLine : reverseList)
    	    {
    	    	int x1 = 0 ;
    	    	int y1 = 0 ;
    	    	int x2 = widthJPanel * (2048 - oneLine) / 2048 ;
    	    	int y2 = 0 ;
     
    	    	g.drawLine(0, cpt, x2, cpt);
     
    	    	cpt++;
    	    }
    	}
     
     
    	public Dimension getPreferredSize() {
                return super.getPreferredSize();
        }
     
    	//*/
    	@Override
    	public Dimension getPreferredScrollableViewportSize() {
    		// TODO Auto-generated method stub
    		return getPreferredSize();
    	}
     
    	@Override
    	public int getScrollableBlockIncrement(Rectangle visibleRect,
    			                               int orientation, 
    			                               int direction) {
     
    		if (orientation == SwingConstants.HORIZONTAL) {
                return visibleRect.width - maxUnitIncrement;
            } else {
                return visibleRect.height - maxUnitIncrement;
            }
     
    	}
     
    	@Override
    	public boolean getScrollableTracksViewportHeight() {
    		// TODO Auto-generated method stub
    		return false;
    	}
     
    	@Override
    	public boolean getScrollableTracksViewportWidth() {
    		// TODO Auto-generated method stub
    		return false;
    	}
     
    	@Override
    	public int getScrollableUnitIncrement(Rectangle visibleRect,
    			                              int orientation, 
    			                              int direction) {
    		//Get the current position.
            int currentPosition = 0;
            if (orientation == SwingConstants.HORIZONTAL) {
                currentPosition = visibleRect.x;
            } else {
                currentPosition = visibleRect.y;
            }
     
            //Return the number of pixels between currentPosition
            //and the nearest tick mark in the indicated direction.
            if (direction < 0) {
                int newPosition = currentPosition -
                                 (currentPosition / maxUnitIncrement)
                                  * maxUnitIncrement;
                return (newPosition == 0) ? maxUnitIncrement : newPosition;
            } else {
                return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
            }
    	}
    	//*/
    }

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2014
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2014
    Messages : 12
    Points : 9
    Points
    9
    Par défaut
    J'ai mis à jour ma classe, l'ascenseur apparait mais uniquement si ma JFrame est redimensionné. Je ne vois pas comment faire apparaitre l'ascenseur, dois je forcer le repaint d'un élément ? De la JFrame, Du JScrollePane, Du JPanel ou du JLabel dans lequel je dessine ?

    Le code modifié :

    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
    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
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    public class ScreenPrinter3 extends JLabel implements Scrollable, MouseMotionListener{
     
    	private int tabValue[] = new int[2048];
     
    	private int maxUnitIncrement = 1;
     
    	//private ArrayList<int[]> list = new ArrayList< int[]>();
    	private ArrayList<Integer> list = new ArrayList< Integer>();
     
    	/**
             * Constructeur par defaut
             */
    	public ScreenPrinter3(int inMaxUnitIncrement){
     
    		maxUnitIncrement = inMaxUnitIncrement;
    	}
     
    	/**
             * 
             */
    	public void AddLine(){
     
    		double doubleVal = Math.random();
    		int    intVal    = (int) (doubleVal * 2048) ;
     
    		list.add(intVal);
     
    		this.repaint();
    	}
     
    	/**
             * 
             */
    	public void paintComponent(Graphics g){
     
    		// largeur
    		int widthJPanel  = this.getWidth() ;
     
    		// hauteur
    		//int heightJPanel = this.getHeight() ;
    		int heightJPanel = list.size() ;
     
    		Dimension dim = new Dimension(widthJPanel, heightJPanel);
    		setMinimumSize(dim);
    		setPreferredSize(dim);
    		setMaximumSize(dim);
     
    		//g.setColor(new Color(1.0f, 1.0f, 1.0f)); // blanc
    		g.setColor(new Color(1.0f, 0.0f, 0.0f)); // blanc
    	    g.fillRect(0, 0, widthJPanel, heightJPanel);
    	    g.setColor(new Color(0.0f, 0.0f, 0.0f)); // noir
     
    	    if(list.size() == 0)
    	    	return ;
     
    	    int cpt = 0 ;
     
    	    // on doit inverser la descente du schema pour avoir l'effet "sismographe"
    	    ArrayList<Integer> reverseList = new ArrayList< Integer>();
     
    	    for(int i=list.size()-1; i>=0; i--)
    	    	reverseList.add(list.get(i));
     
    	    for(Integer oneLine : reverseList)
    	    {
    	    	int x1 = 0 ;
    	    	int y1 = 0 ;
    	    	int x2 = widthJPanel * (2048 - oneLine) / 2048 ;
    	    	int y2 = 0 ;
     
    	    	g.drawLine(0, cpt, x2, cpt);
     
    	    	cpt++;
    	    }
     
    	    Print(reverseList);
    	}
     
     
    	public Dimension getPreferredSize() {
     
    		return new Dimension(super.getWidth(), list.size());
    		//return super.getPreferredSize();
        }
     
    	//*/
    	@Override
    	public Dimension getPreferredScrollableViewportSize() {
    		// TODO Auto-generated method stub
    		return getPreferredSize();
    	}
     
    	@Override
    	public int getScrollableBlockIncrement(Rectangle visibleRect,
    			                               int orientation, 
    			                               int direction) {
     
    		if (orientation == SwingConstants.HORIZONTAL) {
                return visibleRect.width - maxUnitIncrement;
            } else {
                return visibleRect.height - maxUnitIncrement;
            }
     
    	}
     
    	@Override
    	public boolean getScrollableTracksViewportHeight() {
    		// TODO Auto-generated method stub
    		return false;
    	}
     
    	@Override
    	public boolean getScrollableTracksViewportWidth() {
    		// TODO Auto-generated method stub
    		return false;
    	}
     
    	@Override
    	public int getScrollableUnitIncrement(Rectangle visibleRect,
    			                              int orientation, 
    			                              int direction) {
    		//Get the current position.
            int currentPosition = 0;
            if (orientation == SwingConstants.HORIZONTAL) {
                currentPosition = visibleRect.x;
            } else {
                currentPosition = visibleRect.y;
            }
     
            //Return the number of pixels between currentPosition
            //and the nearest tick mark in the indicated direction.
            if (direction < 0) {
                int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement;
                return (newPosition == 0) ? maxUnitIncrement : newPosition;
            } else {
                return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
            }
    	}
    	//*/
     
     
    	/**
             * 
             */
    	public void Print(ArrayList<Integer> inReverseList){
     
    		System.out.println("fonction test du panel");
     
    		try {
    			Calendar calendar = Calendar.getInstance();
    			int hour   = calendar.get(Calendar.HOUR_OF_DAY);
    			int minute = calendar.get(Calendar.MINUTE);
     
    			int day    = calendar.get(Calendar.DAY_OF_MONTH);
    			int month  = calendar.get(Calendar.MONTH);
    			int year   = calendar.get(Calendar.YEAR);
     
    			// on recupere la taille de l'image, en largeur 2048, en hauteur le nombre de ligne
    			int width  = 2048; 
    			int height = inReverseList.size();
     
    			// Create a buffered image with transparency
    		    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
     
    		    // Draw the image on to the buffered image
    		    Graphics2D bGr = bimage.createGraphics();
     
    		    int cpt = 0 ;
    		    for(Integer oneLine : inReverseList)
    		    {
     
    				bGr.drawLine(oneLine, cpt, 0, cpt);
     
     
    				cpt++;
    		    }
     
    		    bGr.dispose();
     
    		    StringBuffer strBffr = new StringBuffer();
    		    strBffr.append(year).append(month).append(day).append("_").append(hour).append("_").append(minute).append(".png");
     
    			ImageIO.write(bimage,"PNG",new File(strBffr.toString())); 
     
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
     
    	/**
             * Converts a given Image into a BufferedImage
             *
             * @param img The Image to be converted
             * @return The converted BufferedImage
             */
    	public static BufferedImage toBufferedImage(Image img)
    	{
    	    if (img instanceof BufferedImage)
    	    {
    	        return (BufferedImage) img;
    	    }
     
    	    // Create a buffered image with transparency
    	    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
     
    	    // Draw the image on to the buffered image
    	    Graphics2D bGr = bimage.createGraphics();
    	    bGr.drawImage(img, 0, 0, null);
    	    bGr.dispose();
     
    	    // Return the buffered image
    	    return bimage;
    	}
    }

Discussions similaires

  1. DBLookupComboBox dans DBGrid
    Par KThrax dans le forum Bases de données
    Réponses: 7
    Dernier message: 24/08/2004, 15h18
  2. gérer les jpg dans une fenetre directdraw???
    Par Anonymous dans le forum DirectX
    Réponses: 1
    Dernier message: 14/06/2002, 13h39
  3. enregistrer dans un fichier avec une appli mdi
    Par ferrari dans le forum C++Builder
    Réponses: 4
    Dernier message: 05/05/2002, 15h17
  4. faire un selection dans une image aves les APIs
    Par merahyazid dans le forum C++Builder
    Réponses: 3
    Dernier message: 30/04/2002, 10h44
  5. Documentation DirectX dans C++Builder 3
    Par srvremi dans le forum DirectX
    Réponses: 1
    Dernier message: 26/04/2002, 09h59

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