This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes getting class file in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "getting class file in java" Watch "getting class file in java" New topic
Author

getting class file in java

C.R.Muthukumar
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
//import src.javax.imageio.*;
//import src.javax.imageio.stream.*;

public class Graph
{
static private final int border =25;
private BufferedImage image;
private double data[];
private int width ,height;
static private final int gridWidth=12,gridHeight=10;
static private final String months[]={
"Jan","Feb","March","Apr","ay","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
public Graph(double data[], int width,int height)
{
this.data=data;
this.width=width;
this.height=height;
}
private void generateImage()
{
image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2= image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint paint= new GradientPaint(0,0,Color.white,width,height,Color.lightGray);
g2.setPaint(paint);
g2.fillRect(0,0,width-1,height-1);
g2.setPaint(Color.black);
BasicStroke veryThin= new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
BasicStroke thin=new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
BasicStroke thick = new BasicStroke(7,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(thin);
g2.setPaint(new Color(155,155,155));
for(int i=0;i<gridWidth;++i)
{
int x= border+(i*(width-2*border))/(gridWidth-1);
g2.drawLine(x,border,x, height-border);
}
for(int i=0; i<gridHeight;++i)
{
int y=border+(i*(width-2*border))/(gridHeight-1);
g2.drawLine(border,y,width-border,y);
}
g2.setStroke(thick);
g2.setPaint(Color.black);
for(int i=1; i<data.length;++i)
{
int x0= border +
(int)(((width-2*border)*(i-1))/(gridWidth-1));

int y0= border+
(int)(((width-2*border)*data[i-1])/(gridWidth-1));
int x1 = border+
(int)(((height-2*border)*i)/(gridWidth-1));
int y1=border+(int)(((height-2*border)*data[i])/(gridHeight-1));
y0=height-1-y0;
y1=height-1-y1;
g2.drawLine(x0,y0,x1,y1);
}
g2.setStroke(veryThin);
g2.setPaint(Color.darkGray);
Font font = new Font("Courier", Font.BOLD,12);
g2.setFont(font);
for(int i=0;i<12;++i)
{
String month=months[i];
FontMetrics fontMetrics = g2.getFontMetrics();
int stringWidth =fontMetrics.stringWidth(month);
int stringHeight=fontMetrics.getAscent();
int x=border+
(int)(((width +2*border)*(i))/(gridWidth-1))-stringWidth/2;
int y = height-border+stringHeight;
g2.drawString(month,x,y);
}
}
public void write(String filename)throws IOException{
if(image==null)
generateImage();
String suffix=filename.substring(filename.lastIndexOf('.')+1);
Iterator ImageWriters=ImageIO.getImageWritersBySuffix(suffix);
ImageWriter imageWriter=(ImageWriter)imageWriters.next();
if(imageWriter==null)
throw new RuntimeException("Format for"+filename+"not supported");
File file = new File(filename);
ImageOutputStream ios=ImageIO.createImageOutputStream(file);
imageWriter.setOutput(ios);
imageWriter.write(image);
}
static public void main(String args[])throws IOException{
double data0[]={4.2,4.3,5.3,6.5,9.0,8.5,0.2,0.4,1.3,4.3,2.6,7.8};
Graph graph0 = new Graph(data0,400,250);
graph0.write("graph0.png");
double data1[]={0,3.3,1.2,6.6,2.5,8.3,4.7,5,4.7,3.9,2.6,1.1};
Graph graph1= new Graph(data1,400,250);
graph1.write("graph1.png");
}
}

The above code does not produce class file on debugging/compilation? can you pl help
to make it executable.Thanks
C.R.Muthukumar
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

Two comments on the compilation:
1) The commented imports are required (preferable without the "src.")
2) Java is case sensitive. imageWriters is not the same as ImageWriters

In the future, it is helpful to include the compiler error messages with your code. And welcome to JavaRanch!


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
C.R.Muthukumar
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
Originally posted by Jeanne Boyarsky:
Two comments on the compilation:
1) The commented imports are required (preferable without the "src.")
2) Java is case sensitive. imageWriters is not the same as ImageWriters

In the future, it is helpful to include the compiler error messages with your code. And welcome to JavaRanch!


C:\jdk1.3\bin\Graph.java:83: cannot resolve symbol
symbol : variable ImageIO
location: class Graph
Iterator ImageWriters=ImageIO.getImageWritersBySuffix(suffix);
^
C:\jdk1.3\bin\Graph.java:84: cannot resolve symbol
symbol : class ImageWriter
location: class Graph
ImageWriter imageWriter=(ImageWriter)ImageWriters.next();
^
C:\jdk1.3\bin\Graph.java:84: cannot resolve symbol
symbol : class ImageWriter
location: class Graph
ImageWriter imageWriter=(ImageWriter)ImageWriters.next(); ^
C:\jdk1.3\bin\Graph.java:88: cannot resolve symbol
symbol : class ImageOutputStream
location: class Graph
ImageOutputStream ios=ImageIO.createImageOutputStream(file);
^
C:\jdk1.3\bin\Graph.java:88: cannot resolve symbol
symbol : variable ImageIO
location: class Graph
ImageOutputStream ios=ImageIO.createImageOutputStream(file);
^
5 errors

Tool completed with exit code 1
As per try , pl find the result of the compilation for ur perusal and needful
Thanks a lot if a solution is forwarded for these errors.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

The Java Tutorial has a chapter on how to create and use packages.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting class file in java