Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3734 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Comment combiner deux codes ?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Comment combiner deux codes ?

n°1174146
albert95
Posté le 10-08-2005 à 18:05:32  profilanswer
 

Bonsoir à tous et à toutes, forumiens, forumiennes,
 
 ma question va certainement bien faire rire les experts…bon, mais il faut bien que j’arrive à m’en sortir avec ces codes, j’y passe des heures (c’est comme ça qu’on apprend, je crois)
 :pt1cable:  
J’utilise 2 codes qui fonctionnent séparément. Si je les combine, ça ne marche plus…
 
Le premier pour créer une image :  

Citation :


/*
 * StockGraphProducer
 *
 * Copyright (c) 2000 Ken McCrary, All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 *
 * KEN MCCRARY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KEN MCCRARY
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
 
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.FontMetrics;
 
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
 
/**
 *  Draw a simple stock price graph for a one week period
 *  The stock is Sun Microsystems for a week in March, 2000.
 *
 */
public class StockGraphProducer implements ImageProducer
{
  private static int ImageWidth = 300;
  private static int ImageHeight = 300;
 
  private static int VertInset = 25;
  private static int HorzInset = 25;
  private static int HatchLength = 10;
 
  /**
   *  Request the producer create an image
   *
   *  @param stream stream to write image into
   *  @return image type
   */
  public String createImage(OutputStream stream) throws IOException
  {
    plottedPrices = new Point2D.Double[5];
    int prices[] =  {105, 100, 97, 93, 93};
 
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(stream);
 
    BufferedImage bi = new BufferedImage(ImageWidth + 10,  
                                         ImageHeight,  
                                         BufferedImage.TYPE_BYTE_INDEXED);
 
    graphics = bi.createGraphics();
    graphics.setColor(Color.white);
    graphics.fillRect(0, 0, bi.getWidth(), bi.getHeight());
 
    graphics.setColor(Color.red);
 
    createVerticalAxis();
    createHorizontalAxis();
     
    graphics.setColor(Color.green);
     
    plotPrices(prices);
 
    encoder.encode(bi);
 
    return "image/jpg";
  }
 
  /**
   *  Create the vertical axis
   *
   *
   */
  void createVerticalAxis()
  {
 
    vertAxis = new Line2D.Double(HorzInset,  
                                 VertInset,  
                                 HorzInset,  
                                 ImageHeight - VertInset);
    graphics.draw(vertAxis);
 
 
    // Draw the vertical hatch marks
    int verticalLength = ImageHeight - (VertInset * 2);
    int interval = verticalLength/5;
 
    Line2D.Double vertHatch1 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY(),  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY());
 
    graphics.draw(vertHatch1);
 
 
    Line2D.Double vertHatch2 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval);
 
    graphics.draw(vertHatch2);
 
    Line2D.Double vertHatch3 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 2,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 2);
 
    graphics.draw(vertHatch3);
 
 
    Line2D.Double vertHatch4 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 3 ,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 3);
 
    graphics.draw(vertHatch4);
 
    Line2D.Double vertHatch5 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 4,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 4);
 
    graphics.draw(vertHatch5);
 
    verticalAxisTicks = new Line2D.Double[5];
    verticalAxisTicks[0] = vertHatch1;
    verticalAxisTicks[1] = vertHatch2;
    verticalAxisTicks[2] = vertHatch3;
    verticalAxisTicks[3] = vertHatch4;
    verticalAxisTicks[4] = vertHatch5;
 
    verticalYTop = vertHatch1.getP1().getY();
    verticalYBottom = vertHatch5.getP1().getY();
  }
 
  /**  
   *  Create the horizontal axis
   *
   *
   */
  void createHorizontalAxis()
  {
    horAxis = new Line2D.Double(HorzInset,  
                                ImageHeight - VertInset,  
                                ImageWidth -  HorzInset,  
                                ImageHeight - VertInset);
    graphics.draw(horAxis);
 
    int horLength = ImageWidth - (HorzInset * 2);
    int horInterval = horLength/5;
 
    assignVerticalRange(90, 110, 5);
 
    // Draw the horizontal hatches
     
    Line2D.Double horHatch1 = new Line2D.Double(horAxis.getP1().getX() + horInterval,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch1);
 
    decorateVerticalLine(graphics, horHatch1, "M" );
 
    Line2D.Double horHatch2 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 2,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 2,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch2);
 
    Line2D.Double horHatch3 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 3,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 3,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch3);
 
    Line2D.Double horHatch4 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 4,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 4,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch4);
 
    Line2D.Double horHatch5 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 5,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 5,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    horizontalAxisTicks =  new double[5];
    horizontalAxisTicks[0] = horHatch1.getX1();
    horizontalAxisTicks[1] = horHatch2.getX1();
    horizontalAxisTicks[2] = horHatch3.getX1();
    horizontalAxisTicks[3] = horHatch4.getX1();
    horizontalAxisTicks[4] = horHatch5.getX1();
 
    graphics.draw(horHatch5);
 
    // Add text to hatches
    decorateVerticalLine(graphics, horHatch1, "M" );
    decorateVerticalLine(graphics, horHatch2, "T" );
    decorateVerticalLine(graphics, horHatch3, "W" );
    decorateVerticalLine(graphics, horHatch4, "T" );
    decorateVerticalLine(graphics, horHatch5, "F" );
 
  }
 
  /**
   *  Plot the five closing stock prices
   *
   *
   */
  void plotPrices(int[] prices)
  {
    //***************************************************************
    // calculatePriceRatio will determine the percentage of the
    // Y axis the price is, then multiply by the Y axis length.
    //
    //***************************************************************
    double yAxisLength = verticalYBottom - verticalYTop;
 
    double mondayPrice = calculatePriceRatio(prices[0]) * yAxisLength + VertInset;
    double tuesdayPrice = calculatePriceRatio(prices[1]) * yAxisLength + VertInset;
    double wednsdayPrice = calculatePriceRatio(prices[2]) * yAxisLength + VertInset;
    double thursdayPrice = calculatePriceRatio(prices[3]) * yAxisLength + VertInset;
    double fridayPrice = calculatePriceRatio(prices[4]) * yAxisLength + VertInset;
 
    Point2D.Double day1 = new Point2D.Double(horizontalAxisTicks[0], mondayPrice);
    Point2D.Double day2 = new Point2D.Double(horizontalAxisTicks[1], tuesdayPrice);
    Point2D.Double day3 = new Point2D.Double(horizontalAxisTicks[2], wednsdayPrice);
    Point2D.Double day4 = new Point2D.Double(horizontalAxisTicks[3], thursdayPrice);
    Point2D.Double day5 = new Point2D.Double(horizontalAxisTicks[4], fridayPrice);
 
    Line2D.Double line1 = new Line2D.Double(day1, day2);
    Line2D.Double line2 = new Line2D.Double(day2, day3);
    Line2D.Double line3 = new Line2D.Double(day3, day4);
    Line2D.Double line4 = new Line2D.Double(day4, day5);
     
    graphics.draw(line1);
    graphics.draw(line2);
    graphics.draw(line3);
    graphics.draw(line4);
 
  }
 
  /**
   *  Determine the location of the price in the range of price data
   *
   */
  double calculatePriceRatio(int price)
  {
    double totalDataRange = highVerticalRange - lowVerticalRange;
    double pointDelta = highVerticalRange - price;
    double ratio = pointDelta/totalDataRange;
 
    return ratio;
  }
 
  /**
   *  Assignes the range for the vertical axis
   *
   */
  void assignVerticalRange( int low, int high, int increment )
  {
    lowVerticalRange = low;
    highVerticalRange = high;
 
    int current = low;
    int hatchCount = verticalAxisTicks.length - 1;
 
    //***************************************************************
    // Label each vertical tick starting with the low value and
    // increasing by increment value
    //***************************************************************
    while ( current <= high )  
    {
      decorateHorizontalLine(graphics,  
                             verticalAxisTicks[hatchCount],  
                             new Integer(current).toString() );
      current += increment;
      hatchCount--;
    }
  }
 
  /**
   *  Adds decorating text to the enpoint of a horizontal line
   *
   */
  void decorateHorizontalLine(Graphics2D graphics, Line2D.Double line, String text)
  {
    double endX = line.getX1();
    double endY = line.getY1();
    double baseX = endX;
    double baseY = endY;
 
    //***************************************************************
    // The text should be slightly to the left of the line
    // and centered
    //***************************************************************
    FontMetrics metrics = graphics.getFontMetrics();
    baseX -= metrics.stringWidth(text);
    baseY += metrics.getAscent()/2;
    graphics.drawString(text,  
                        new Float(baseX).floatValue(),  
                        new Float(baseY).floatValue());
  }
 
  /**
   *  Adds decorating text to the enpoint of a vertical line
   *
   */
  void decorateVerticalLine (Graphics2D graphics, Line2D.Double line, String text)
  {
    double endX = line.getX2();
    double endY = line.getY2();
    double baseX = endX;
    double baseY = endY;
 
    //***************************************************************
    // Center the text over the line
    //***************************************************************
    FontMetrics metrics = graphics.getFontMetrics();
    baseX -= metrics.stringWidth(text)/2;
    baseY += metrics.getAscent();
     
    graphics.drawString(text,  
                        new Float(baseX).floatValue(),  
                        new Float(baseY).floatValue());
 
  }
     
  /**
   *  Test Entrypoint
   *
   */
  public static void main(String[] args)
  {
   
    try
    {
       FileOutputStream f = new FileOutputStream("stockgraph.jpg" );
       StockGraphProducer producer = new StockGraphProducer();
       producer.createImage(f);
       f.close();  
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
 
  private Line2D.Double vertAxis;
  private Line2D.Double horAxis;
  private double[] horizontalAxisTicks;
  private int highVerticalRange;
  private int lowVerticalRange;
  private Graphics2D graphics;
  private Line2D.Double[] verticalAxisTicks;
  private Point2D.Double[] plottedPrices;
  private double verticalYTop;
  private double verticalYBottom;
 
}


 
Le second lit l’image dans une fenêtre :
 

Citation :


import java.awt.*;
import javax.swing.*;
 
class TestFrame extends JFrame {
 
  public TestFrame() {
    super( "Graphics demo" );
    getContentPane().add(new JCanvas());
  }
 
  public static void main( String args[] ) {
    TestFrame mainFrame = new TestFrame();
    mainFrame.pack();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 1.3
    mainFrame.setVisible(true);
  }
}
 
class JCanvas extends JComponent {
 
   
   
  private static ImageIcon m_flight = new ImageIcon("stockgraph.jpg" );
 
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
 
    // fill entire component white
    g.setColor(Color.white);
    g.fillRect(0,0,getWidth(),getHeight());
 
   ;
 
    // paint the icon below blue sqaure
int w = m_flight.getIconWidth();
int h = m_flight.getIconHeight();
 m_flight.paintIcon(this,g,180-(w/2),150-(h/2));
 
    // paint the icon below red sqaure
    //m_flight.paintIcon(this,g,120-(w/2),300-(h/2));
 
   
 
    //g.setColor(Color.black);
 
     
  }
   
  // Some layout managers need this information
  public Dimension getPreferredSize() {
    return new Dimension(400,400);
  }
 
  // Some layout managers need this information
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }


 
ma combinaison crée bien l’image, mais la fenêtre pour lire le graphe ne s’ouvre pas :
 

Citation :


/*
 * StockGraphProducer
 *
 * Copyright (c) 2000 Ken McCrary, All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 *
 * KEN MCCRARY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KEN MCCRARY
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
 
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.FontMetrics;
 
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
//---code de lecture
import java.awt.*;
import javax.swing.*;
 
/**
 *  Draw a simple stock price graph for a one week period
 *  The stock is Sun Microsystems for a week in March, 2000.
 *
 */
 class StockGraphProducer implements ImageProducer
{
  private static int ImageWidth = 300;
  private static int ImageHeight = 300;
 
  private static int VertInset = 25;
  private static int HorzInset = 25;
  private static int HatchLength = 10;
 
  /**
   *  Request the producer create an image
   *
   *  @param stream stream to write image into
   *  @return image type
   */
  public String createImage(OutputStream stream) throws IOException
  {
    plottedPrices = new Point2D.Double[5];
    int prices[] =  {105, 100, 97, 93, 93};
 
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(stream);
 
    BufferedImage bi = new BufferedImage(ImageWidth + 10,  
                                         ImageHeight,  
                                         BufferedImage.TYPE_BYTE_INDEXED);
 
    graphics = bi.createGraphics();
    graphics.setColor(Color.white);
    graphics.fillRect(0, 0, bi.getWidth(), bi.getHeight());
 
    graphics.setColor(Color.red);
 
    createVerticalAxis();
    createHorizontalAxis();
     
    graphics.setColor(Color.green);
     
    plotPrices(prices);
 
    encoder.encode(bi);
 
    return "image/jpg";
  }
 
  /**
   *  Create the vertical axis
   *
   *
   */
  void createVerticalAxis()
  {
 
    vertAxis = new Line2D.Double(HorzInset,  
                                 VertInset,  
                                 HorzInset,  
                                 ImageHeight - VertInset);
    graphics.draw(vertAxis);
 
 
    // Draw the vertical hatch marks
    int verticalLength = ImageHeight - (VertInset * 2);
    int interval = verticalLength/5;
 
    Line2D.Double vertHatch1 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY(),  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY());
 
    graphics.draw(vertHatch1);
 
 
    Line2D.Double vertHatch2 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval);
 
    graphics.draw(vertHatch2);
 
    Line2D.Double vertHatch3 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 2,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 2);
 
    graphics.draw(vertHatch3);
 
 
    Line2D.Double vertHatch4 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 3 ,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 3);
 
    graphics.draw(vertHatch4);
 
    Line2D.Double vertHatch5 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 4,  
                                                 vertAxis.getP1().getX() + HatchLength/2,  
                                                 vertAxis.getP1().getY() + interval * 4);
 
    graphics.draw(vertHatch5);
 
    verticalAxisTicks = new Line2D.Double[5];
    verticalAxisTicks[0] = vertHatch1;
    verticalAxisTicks[1] = vertHatch2;
    verticalAxisTicks[2] = vertHatch3;
    verticalAxisTicks[3] = vertHatch4;
    verticalAxisTicks[4] = vertHatch5;
 
    verticalYTop = vertHatch1.getP1().getY();
    verticalYBottom = vertHatch5.getP1().getY();
  }
 
  /**  
   *  Create the horizontal axis
   *
   *
   */
  void createHorizontalAxis()
  {
    horAxis = new Line2D.Double(HorzInset,  
                                ImageHeight - VertInset,  
                                ImageWidth -  HorzInset,  
                                ImageHeight - VertInset);
    graphics.draw(horAxis);
 
    int horLength = ImageWidth - (HorzInset * 2);
    int horInterval = horLength/5;
 
    assignVerticalRange(90, 110, 5);
 
    // Draw the horizontal hatches
     
    Line2D.Double horHatch1 = new Line2D.Double(horAxis.getP1().getX() + horInterval,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch1);
 
    decorateVerticalLine(graphics, horHatch1, "M" );
 
    Line2D.Double horHatch2 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 2,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 2,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch2);
 
    Line2D.Double horHatch3 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 3,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 3,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch3);
 
    Line2D.Double horHatch4 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 4,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 4,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    graphics.draw(horHatch4);
 
    Line2D.Double horHatch5 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 5,  
                                                horAxis.getP1().getY() - HatchLength/ 2,  
                                                horAxis.getP1().getX() + horInterval * 5,  
                                                horAxis.getP1().getY() + HatchLength/2);
 
    horizontalAxisTicks =  new double[5];
    horizontalAxisTicks[0] = horHatch1.getX1();
    horizontalAxisTicks[1] = horHatch2.getX1();
    horizontalAxisTicks[2] = horHatch3.getX1();
    horizontalAxisTicks[3] = horHatch4.getX1();
    horizontalAxisTicks[4] = horHatch5.getX1();
 
    graphics.draw(horHatch5);
 
    // Add text to hatches
    decorateVerticalLine(graphics, horHatch1, "M" );
    decorateVerticalLine(graphics, horHatch2, "T" );
    decorateVerticalLine(graphics, horHatch3, "W" );
    decorateVerticalLine(graphics, horHatch4, "T" );
    decorateVerticalLine(graphics, horHatch5, "F" );
 
  }
 
  /**
   *  Plot the five closing stock prices
   *
   *
   */
  void plotPrices(int[] prices)
  {
    //***************************************************************
    // calculatePriceRatio will determine the percentage of the
    // Y axis the price is, then multiply by the Y axis length.
    //
    //***************************************************************
    double yAxisLength = verticalYBottom - verticalYTop;
 
    double mondayPrice = calculatePriceRatio(prices[0]) * yAxisLength + VertInset;
    double tuesdayPrice = calculatePriceRatio(prices[1]) * yAxisLength + VertInset;
    double wednsdayPrice = calculatePriceRatio(prices[2]) * yAxisLength + VertInset;
    double thursdayPrice = calculatePriceRatio(prices[3]) * yAxisLength + VertInset;
    double fridayPrice = calculatePriceRatio(prices[4]) * yAxisLength + VertInset;
 
    Point2D.Double day1 = new Point2D.Double(horizontalAxisTicks[0], mondayPrice);
    Point2D.Double day2 = new Point2D.Double(horizontalAxisTicks[1], tuesdayPrice);
    Point2D.Double day3 = new Point2D.Double(horizontalAxisTicks[2], wednsdayPrice);
    Point2D.Double day4 = new Point2D.Double(horizontalAxisTicks[3], thursdayPrice);
    Point2D.Double day5 = new Point2D.Double(horizontalAxisTicks[4], fridayPrice);
 
    Line2D.Double line1 = new Line2D.Double(day1, day2);
    Line2D.Double line2 = new Line2D.Double(day2, day3);
    Line2D.Double line3 = new Line2D.Double(day3, day4);
    Line2D.Double line4 = new Line2D.Double(day4, day5);
     
    graphics.draw(line1);
    graphics.draw(line2);
    graphics.draw(line3);
    graphics.draw(line4);
 
  }
 
  /**
   *  Determine the location of the price in the range of price data
   *
   */
  double calculatePriceRatio(int price)
  {
    double totalDataRange = highVerticalRange - lowVerticalRange;
    double pointDelta = highVerticalRange - price;
    double ratio = pointDelta/totalDataRange;
 
    return ratio;
  }
 
  /**
   *  Assignes the range for the vertical axis
   *
   */
  void assignVerticalRange( int low, int high, int increment )
  {
    lowVerticalRange = low;
    highVerticalRange = high;
 
    int current = low;
    int hatchCount = verticalAxisTicks.length - 1;
 
    //***************************************************************
    // Label each vertical tick starting with the low value and
    // increasing by increment value
    //***************************************************************
    while ( current <= high )  
    {
      decorateHorizontalLine(graphics,  
                             verticalAxisTicks[hatchCount],  
                             new Integer(current).toString() );
      current += increment;
      hatchCount--;
    }
  }
 
  /**
   *  Adds decorating text to the enpoint of a horizontal line
   *
   */
  void decorateHorizontalLine(Graphics2D graphics, Line2D.Double line, String text)
  {
    double endX = line.getX1();
    double endY = line.getY1();
    double baseX = endX;
    double baseY = endY;
 
    //***************************************************************
    // The text should be slightly to the left of the line
    // and centered
    //***************************************************************
    FontMetrics metrics = graphics.getFontMetrics();
    baseX -= metrics.stringWidth(text);
    baseY += metrics.getAscent()/2;
    graphics.drawString(text,  
                        new Float(baseX).floatValue(),  
                        new Float(baseY).floatValue());
  }
 
  /**
   *  Adds decorating text to the enpoint of a vertical line
   *
   */
  void decorateVerticalLine (Graphics2D graphics, Line2D.Double line, String text)
  {
    double endX = line.getX2();
    double endY = line.getY2();
    double baseX = endX;
    double baseY = endY;
 
    //***************************************************************
    // Center the text over the line
    //***************************************************************
    FontMetrics metrics = graphics.getFontMetrics();
    baseX -= metrics.stringWidth(text)/2;
    baseY += metrics.getAscent();
     
    graphics.drawString(text,  
                        new Float(baseX).floatValue(),  
                        new Float(baseY).floatValue());
 
  }
     
  /**
   *  Test Entrypoint
   *
   */
  public static void main(String[] args)
  {
   
    try
    {
       FileOutputStream f = new FileOutputStream("stockgraph.jpg" );
       StockGraphProducer producer = new StockGraphProducer();
       producer.createImage(f);
       f.close();  
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
 
  private Line2D.Double vertAxis;
  private Line2D.Double horAxis;
  private double[] horizontalAxisTicks;
  private int highVerticalRange;
  private int lowVerticalRange;
  private Graphics2D graphics;
  private Line2D.Double[] verticalAxisTicks;
  private Point2D.Double[] plottedPrices;
  private double verticalYTop;
  private double verticalYBottom;
 
 
class TestFrame extends JFrame {
 
  public TestFrame() {
    super( "Graphics demo" );
    getContentPane().add(new JCanvas());
  }
 
  public  void main( String args[] ) {
    TestFrame mainFrame = new TestFrame();
    mainFrame.pack();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 1.3
    mainFrame.setVisible(true);
  }
}
 
class JCanvas extends JComponent {
 
   
   
  private  ImageIcon m_flight = new ImageIcon("stockgraph.jpg" );
 
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
 
    // fill entire component white
    g.setColor(Color.white);
    g.fillRect(0,0,getWidth(),getHeight());
 
   ;
 
    // paint the icon below blue sqaure
int w = m_flight.getIconWidth();
int h = m_flight.getIconHeight();
 m_flight.paintIcon(this,g,180-(w/2),150-(h/2));
 
    // paint the icon below red sqaure
    //m_flight.paintIcon(this,g,120-(w/2),300-(h/2));
 
   
 
    //g.setColor(Color.black);
 
     
  }
   
  // Some layout managers need this information
  public Dimension getPreferredSize() {
    return new Dimension(400,400);
  }
 
  // Some layout managers need this information
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
}
 
}


 
puis-je avoir une aide pour combiner ces deux codes ??
merci d'avance
 
albert
 
mes références :
création d'image
http://www.javaworld.com/javaworld [...] #resources
récupération dans une fenêtre
http://javafaq.nu/java/free-swing- [...] ter2.shtml

mood
Publicité
Posté le 10-08-2005 à 18:05:32  profilanswer
 

n°1174427
victorus_e​cl
PIBCAK
Posté le 11-08-2005 à 11:37:28  profilanswer
 

Des petites choses pour t'aider:
 
1) ça ne sert à rien de mettre TOUT ton code ici, personne ne le lira jamais :)
2) En java, on fait de la POO... ce n'est pas du C. Sauf cas exceptionnel il n'y a pas d'interet à réunir plusieurs fichiers de classes dans un même fichier...
3) Pour faire fonctionner les 2 ensembles, basiquement on appelle une nouvelle instance de l'objet décrit dans le fichier a à l'intérieur du fichier b, et on l'utilise ensuite comme on veut...  
 
en bref, trouves un bon tuto sur la POO et java, et commence avec un peu plus simple que ce que tu essaies de faire...

n°1174548
albert95
Posté le 11-08-2005 à 13:40:59  profilanswer
 

Merci victorus_e cl,
 
Je pensais naïvement qu’il était nécessaire de faire des lignes de programmes pour faire du java. Il semble que je me suis trompé.
 :??:  

Citation :


3) Pour faire fonctionner les 2 ensembles, basiquement on appelle une nouvelle instance de l'objet décrit dans le fichier a à l'intérieur du fichier b, et on l'utilise ensuite comme on veut...  


 
C’est encore une sombre histoire de CLASSPATH ??(mon cauchemar)
http://imagesdeti.free.fr/GifAnimesSmiley/smiley_369.gif
 
tu peux me dire par quel procédé on appelle une nouvelle instance ? je suppose qu’il faut utiliser la procédure  
« import », mais comment faire interagir les packages ?

Citation :


 
en bref, trouves un bon tuto sur la POO et java, et commence avec un peu plus simple que ce que tu essaies de faire...


 
J’étais assez content des codes trouvés dans les 2 tutos cités dans mon post…ils ne sont donc pas de bons  pour débuter ?
Où trouve-t-on de bon tuto sur la POO et java ?? tu as des pistes à m’indiquer ?
 J’en ai travaillé un grand nombre, ils traitaient peut-être ces sujets, mais je ne m’en suis pas aperçu.
 
cordialement
albert
 

n°1174595
nerisson
Pic-pic
Posté le 11-08-2005 à 14:38:31  profilanswer
 
n°1174612
albert95
Posté le 11-08-2005 à 14:55:56  profilanswer
 

merci nerisson,
je vais revisiter tous ces tutoriaux, ça ne peut pas faire de mal
 
cordialement
albert

n°1174616
victorus_e​cl
PIBCAK
Posté le 11-08-2005 à 15:05:33  profilanswer
 

Citation :

Où trouve-t-on de bon tuto sur la POO et java ??


à ton niveau je commencerai par ça http://tima.imag.fr/rms/perso/boun [...] ndex1.html
 
ça devrait te permettre de comprendre pourquoi ce que tu as essayé de faire ne marche pas dès la fin de la page 2.


Message édité par victorus_ecl le 11-08-2005 à 15:07:15
n°1174693
albert95
Posté le 11-08-2005 à 16:15:45  profilanswer
 

Merci victorus_e cl,
 
J’avais déjà parcouru ce tutorial d’Ahcène BOUNCEUR, sans trop lui prêter attention. (d’où l’intérêt des progressions pédagogiques universitaires pour éviter de se disperser, progressions qu’on ne peut pas construire lorsqu’on est autodidacte)
C’est en effet une petite merveille, je vais y consacrer quelques heures.
 
Une question, encore, si ce n’est pas abuser, pour l’affichage des résultats, jusqu’à présent j'utilisais des fenêtres JFrame, je n’ai pas réussi à afficher des applets…faut-il installer une plateforme particulière pour les visionner ?? ou bien faut-il les envoyer sur un serveur ??
 
cordialement
 
albert

n°1179529
albert95
Posté le 19-08-2005 à 13:00:39  profilanswer
 

En complément du tutorial d’Ahcène BOUNCEUR,  pour rendre service à ceux qui, comme moi , parcourent le web à la recherche d’exemples qui fonctionnent bien avec JCreator, j’indique l’adresse d’exemples sur POO, à télécharger :
http://www.scs.carleton.ca/~courses/COMP1005/notes/COMP1405_4/housebuilder.gif
 
 
http://www.scs.carleton.ca/~course [...] ankAccount
 
Il faut télécharger et ranger les deux exemples dans le même dossier, compiler et exécuter
http://www.scs.carleton.ca/~course [...] count.java
http://www.scs.carleton.ca/~course [...] ester.java
 
(running in progress)
 :)  
albert

n°1180482
Profil sup​primé
Posté le 21-08-2005 à 03:04:57  answer
 

albert95 a écrit :

En complément du tutorial d’Ahcène BOUNCEUR,  pour rendre service à ceux qui, comme moi , parcourent le web à la recherche d’exemples qui fonctionnent bien avec JCreator, j’indique l’adresse d’exemples sur POO, à télécharger :
http://www.scs.carleton.ca/~course [...] uilder.gif
 
 
http://www.scs.carleton.ca/~course [...] ankAccount
 
Il faut télécharger et ranger les deux exemples dans le même dossier, compiler et exécuter
http://www.scs.carleton.ca/~course [...] count.java
http://www.scs.carleton.ca/~course [...] ester.java
 
(running in progress)
 :)  
albert


merci pour ces liens, le tutoriel objet est énorme  :o


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Comment combiner deux codes ?

 

Sujets relatifs
Pouvez vous m'expliquer ces 5 lignes de codes en JSrécupération des codes erreurs sur ftp
besoin de codeslister des zip codes qui ne sont pas présents dans un des 2 fichiers
traitement des codes à barresLecteur codes barres, comment on intègre ça?
[Access] Gestion des codes postauxsite vbfrance et codes-sources inaccessible!
Comment récupérer les codes de statut HTTP en ASP ?Existe-t-il une liste de tout les codes erreurs générés par MySQL?
Plus de sujets relatifs à : Comment combiner deux codes ?


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)