codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Graph in Gui...  gosi at 15:44 on Friday, November 11, 2005
 

hi, I´m trying to make a xy-graph in Gui but I can¨t find where it is in java.swing. Does anyone know what the class is called.

thanx :)

  Re: Graph in Gui...  crwood at 07:54 on Saturday, November 12, 2005
 

We're pretty much on our own when it comes to this kind of low-level drawing. The main alternative is third-party software such as JFreeChart and SourceNet. The main classes to look at are Graphics, Graphics2D, Font and classes in the java.awt.geom package for drawing; Toolkit and ImageIO for images. The tutorial has pages on custom painting, using ImageIcons, and a specialized trail on 2D Graphics (good reference). Here is an example that shows how to scale (positive) data and draw it in a graph. You can create about anything that you can imagine like this.

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;

public class Graphing extends JPanel
{
double[] xData = { 16.2, 18.7, 21.9, 14.3, 16.7, 22.9, 21.5, 16.9 };
double[] yData = { 22.0, 23.4, 19.8, 24.0, 18.0, 12.3, 16.3, 14.9 };
final int PAD = 30;

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double w = getWidth();
double h = getHeight();
double[] mmX = getRangeBounds(xData);
double[] mmY = getRangeBounds(yData);
double minX = mmX[0];
double maxX = mmX[1];
double minY = mmY[0];
double maxY = mmY[1];
double xScale = (w - 2*PAD) / (maxX - minX);
double yScale = (h - 2*PAD) / (maxY - minY);
int xSteps = (int)(maxX - minX);
int ySteps = (int)(maxY - minY);

// grid
g2.setPaint(new Color(200,220,240));
// vertical grid lines
double x1, y1 = PAD, x2 = w-PAD, y2 = h-PAD;
for(int j = 1; j <= xSteps; j++)
{
x1 = PAD + j * xScale;
g2.draw(new Line2D.Double(x1, y1, x1, y2));
}
// horizontal grid lines
x1 = PAD;
for(int j = 0; j < ySteps; j++)
{
y1 = PAD + j * yScale;
g2.draw(new Line2D.Double(x1, y1, x2, y1));
}

// axes
g2.setPaint(Color.black);
// ordinate
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// abcissa
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));

// labels
double[] vals = { maxY, minY, minX, maxX };
Font font = g2.getFont().deriveFont(14f);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
float sx, sy;
for(int j = 0; j < vals.length; j++)
{
String s = String.valueOf(vals[j]);
float width = (float)font.getStringBounds(s, frc).getWidth();
LineMetrics lm = font.getLineMetrics(s, frc);
float height = lm.getAscent() + lm.getDescent();
if(j < 2) // ordinate
{
sx = (PAD - width)/2;
sy = (float)(PAD + height/2 - lm.getDescent() + j * (h - 2*PAD));
}
else // abcissa
{
sx = (float)(PAD - width/2 + (j == 3 ? (w - 2*PAD) : 0));
sy = (float)(h - (PAD - height)/2 - lm.getDescent());
}
g2.drawString(s, sx, sy);
}

// data
g2.setPaint(Color.red);
for(int j = 0; j < xData.length; j++)
{
double x = PAD + (xData[j] - minX) * xScale;
double y = h-PAD - (yData[j] - minY) * yScale;
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}
}

private double[] getRangeBounds(double[] d)
{
double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
for(int j = 0; j < d.length; j++)
{
if(d[j] < min)
min = d[j];
if(d[j] > max)
max = d[j];
}
return new double[]{ Math.floor(min), Math.ceil(max) };
}

public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new Graphing());
f.setSize(400,400);
f.setLocation(600,325);
f.setVisible(true);
}
}


  Re: Graph in Gui...  gosi at 15:38 on Saturday, November 12, 2005
 

aha, so it´s a little more complicatet than I thought, thanks for the help :)








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005