MolPanel.java
Sample program to use armDraw.jar. Its filename must be MolPanel.java. After compilation, try "java -classpath armDraw.jar\;. MolPanel C00296.mol".
Size 7.5 kB - File type text/x-javaFile contents
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import metabolic.MolFigure;
import canvas.AbstractComponent;
import canvas.DrawPane;
import canvas.LineStroke;
import canvas.View;
import draw2d.MOLformat;
import draw2d.SmilesFormat;
public class MolPanel extends DrawPane
{
MolFigure mf = null;
public MolPanel()
{
super(null, null);
}
/**
* Read a SMILES format into this molecular panel.
*
* @param id
* ... String id for the molecule data. This
* value can be null.
* @param smiles
* ... SMILES description of the molecule
*/
public void read(String id, String smiles)
{
SmilesFormat sf = new SmilesFormat(smiles);
init(id, sf.toMOLformat());
}
/**
* Read a MOL format into this molecular panel.
*
* @param id
* ... String id for the molecule data. This
* value can be null.
* @param smiles
* ... MOL description of the molecule
*/
public void read(String id, MOLformat mol)
{
init(id, mol);
}
private void init(String id, MOLformat mol)
{
mf = new MolFigure(id, mol);
mf
.initialization(this, new Point2D.Float(0, 0),
20);
mf.setRectBound();
newDraw(id, true);
getLayer().addNew(mf, new Point2D.Float(5, 5), 0);
Dimension d = getPictureSize();
setPaperSize(d);
super.repaint();
}
static private void printHelp()
{
System.out
.println("java -jar MolFigure.jar [options] [MOL files (.mol)]");
System.out.println("OPTIONS");
System.out
.println(" -z ... specify size (e.g. -s 200)");
System.out
.println(" -s ... specify scale (5-50, default is 15)");
System.out
.println(" -f ... specify fontsize (default is 7)");
System.out
.println(" -l ... specify line width (1-7, default is 1)");
System.out
.println(" -o ... specify output directory");
System.out
.println(" -p ... output PNG format instead of JPG");
System.out.println(" -h ... this help");
}
/**
* convert from the array of MOL filenames into JPG
* pictures
*
* @param args
*/
static public void convertToJPGformat(String[] args)
{
searchPane.ViewerOptions drawOptions = new searchPane.ViewerOptions();
DrawPane canvas = new DrawPane(null, null);
String ext = "jpg";
// Set molecular scaling. 5 is the smallest, 50 is
// the largest
float scale = 15;
// This String value is the name of output directory
String dir = "";
int size = 0;
int argi = 0;
Font F = null;
LineStroke ls = null;
while ((argi < args.length)
&& args[argi].startsWith("-"))
{
try
{
if (args[argi].length() < 2)
throw new Exception("");
else
{
if (args[argi].equals("-z"))
{// size
size = (int) Float
.parseFloat(args[++argi]);
argi++;
}
else if (args[argi].equals("-p"))
{// output png
ext = "png";
argi++;
}
else if (args[argi].equals("-s"))
{// scale
scale = Float
.parseFloat(args[++argi]);
argi++;
}
else if (args[argi].equals("-o"))
{// scale
dir = args[++argi];
argi++;
}
else if (args[argi].equals("-l"))
{// linestroke
int f = (int) Float
.parseFloat(args[++argi]);
if (f <= 0)
f = 1;
if (f > 7)
f = 7;
ls = LineStroke.getStroke(f, 0);
argi++;
}
else if (args[argi].equals("-f"))
{// output png
int f = (int) Float
.parseFloat(args[++argi]);
F = new Font("Dialog", Font.PLAIN,
f);
argi++;
}
else
throw new Exception("");
}
}
catch (Exception e)
{
printHelp();
System.exit(0);
}
}
AbstractComponent.setDefaultTo(null, null, ls,
null, F);
for (int i = argi; i < args.length; i++)
{
try
{
draw2d.MOLformat mf = new draw2d.MOLformat();
java.io.BufferedReader br = new java.io.BufferedReader(
new FileReader(args[i]));
mf.read(br);
// Generate MolFigure from the filenames
MolFigure mF = new MolFigure(args[i], mf);
// Set the location of this figure at (0,0)
mF.initialization(canvas,
new Point2D.Float(0, 0), scale);
// Set the picture size (different from scale)
if (size != 0)
mF.setRectBound(size, size);
else
mF.setRectBound();
BufferedImage img;
Graphics2D g;
// Determine the image size
Rectangle2D.Float R = mF.getBoundingBox();
if (ext.equals("jpg"))
{
img = new BufferedImage((int) R.width,
(int) R.height,
BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) img.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, (int) R.width,
(int) R.height);
}
else
{
img = new BufferedImage((int) R.width,
(int) R.height,
BufferedImage.TYPE_INT_ARGB);
g = (Graphics2D) img.getGraphics();
}
g.setRenderingHints(drawOptions
.getRenderingHints());
mF.draw(g, false);
// Generate the output filename
int slashPos = Math.max(0, args[i]
.lastIndexOf("/") + 1);
int suffxPos = Math.min(args[i]
.lastIndexOf("."), args[i].length());
String fname = args[i].substring(slashPos,
suffxPos);
// Inscribe filename into the picture
// g.drawString(fname, 10, R.height - 10);
fname = (dir.equals("") ? "" : (dir + "/"))
+ fname + "." + ext;
ImageIO.write(img, ext, new File(fname));
g.dispose();
System.out.println(fname);
}
catch (Exception e)
{
System.out.println("in processing file "
+ args[i]);
e.printStackTrace();
}
}
}
static public void main(String[] args)
{
util.MolMass.init();
try
{
draw2d.MOLformat mf = new draw2d.MOLformat();
java.io.BufferedReader br = new java.io.BufferedReader(
new FileReader(args[0]));
mf.read(br);
View.PreviewFrame f = new
View.PreviewFrame(); f.read("foo", new
MolFigure("foo", mf)); f.pack();
f.setVisible(true);
// If you want to use the standard JPanel to display
// a structure, use the following code instead.
// JFrame jf = new JFrame();
// MolPanel mp = new MolPanel();
// mp.prepareMenusForPopups();
// mp.read("foo", mf);
// jf.getContentPane().add(mp);
// jf.pack();
// jf.setVisible(true);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}