Comment combiner deux codes ? - Java - Programmation
Marsh Posté le 11-08-2005 à 11:37:28
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...
Marsh Posté le 11-08-2005 à 13:40:59
Merci victorus_e cl,
Je pensais naïvement quil était nécessaire de faire des lignes de programmes pour faire du java. Il semble que je me suis trompé.
Citation : |
Cest encore une sombre histoire de CLASSPATH ??(mon cauchemar)
tu peux me dire par quel procédé on appelle une nouvelle instance ? je suppose quil faut utiliser la procédure
« import », mais comment faire interagir les packages ?
Citation : |
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 à mindiquer ?
Jen ai travaillé un grand nombre, ils traitaient peut-être ces sujets, mais je ne men suis pas aperçu.
cordialement
albert
Marsh Posté le 11-08-2005 à 14:38:31
ReplyMarsh Posté le 11-08-2005 à 14:55:56
merci nerisson,
je vais revisiter tous ces tutoriaux, ça ne peut pas faire de mal
cordialement
albert
Marsh Posté le 11-08-2005 à 15:05:33
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.
Marsh Posté le 11-08-2005 à 16:15:45
Merci victorus_e cl,
Javais déjà parcouru ce tutorial dAhcène BOUNCEUR, sans trop lui prêter attention. (doù lintérêt des progressions pédagogiques universitaires pour éviter de se disperser, progressions quon ne peut pas construire lorsquon est autodidacte)
Cest en effet une petite merveille, je vais y consacrer quelques heures.
Une question, encore, si ce nest pas abuser, pour laffichage des résultats, jusquà présent j'utilisais des fenêtres JFrame, je nai 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
Marsh Posté le 19-08-2005 à 13:00:39
En complément du tutorial dAhcène BOUNCEUR, pour rendre service à ceux qui, comme moi , parcourent le web à la recherche dexemples qui fonctionnent bien avec JCreator, jindique ladresse dexemples sur POO, à télécharger :
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
Marsh Posté le 21-08-2005 à 03:04:57
albert95 a écrit : En complément du tutorial dAhcène BOUNCEUR, pour rendre service à ceux qui, comme moi , parcourent le web à la recherche dexemples qui fonctionnent bien avec JCreator, jindique ladresse dexemples sur POO, à télécharger : |
merci pour ces liens, le tutoriel objet est énorme
Marsh Posté le 10-08-2005 à 18:05:32
Bonsoir à tous et à toutes, forumiens, forumiennes,
ma question va certainement bien faire rire les experts bon, mais il faut bien que jarrive à men sortir avec ces codes, jy passe des heures (cest comme ça quon apprend, je crois)
Jutilise 2 codes qui fonctionnent séparément. Si je les combine, ça ne marche plus
Le premier pour créer une image :
/*
* 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 limage dans une fenêtre :
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 limage, mais la fenêtre pour lire le graphe ne souvre pas :
/*
* 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