I'm working with the 1.8.1 release of biojava on Netbeans, and using the ChromatogramGraphic Class to try to create an image of a chromatogram.
(http://www.biojava.org/docs/api1.8/)
I've made a file chooser to access chromatograms, and I use the ChromatogramFactory (from biojava) Class to create a Chromatogram Object from the file.
Apparently, this:
http://biojava.org/pipermail/biojava-l/2003-June/003896.html
code can accomplish what I want. I don't understand what it does, and I don't think I can use similar syntax to draw the image on my JFrame.
Any help would be greatly appreciated.
[What I have so far. I don't know what most of it does.]
private void renderTrace() throws IOException, UnsupportedChromatogramFormatException {
ABIFChromatogram abiChrom = new ABIFChromatogram();
File abi = new File(textarea.getText());
ABITrace abiTrace = new ABITrace(abi);
ABIFParser abiParse = new ABIFParser(abi);
ChromatogramFactory chromFactory = new ChromatogramFactory();
Chromatogram chrom = ChromatogramFactory.create(abi);
ChromatogramGraphic gfx = new ChromatogramGraphic(chrom);
gfx.setHeight(240);
gfx.setHorizontalScale(2.0f);
// set some options that affect the output
// turn off filled-in "callboxes"
gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_A,
Boolean.FALSE);
gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_C,
Boolean.FALSE);
gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_G,
Boolean.FALSE);
gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_T,
Boolean.FALSE);
gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_OTHER,
Boolean.FALSE);
// this option controls whether each trace/callbox/etc is scaled/positioned
// individually, or whether the scaling is done on all shapes at the level
// of the graphics context
// enabling this option is recommended for higher-quality output
gfx.setOption(ChromatogramGraphic.Option.USE_PER_SHAPE_TRANSFORM,
Boolean.TRUE);
BufferedImage bi = new BufferedImage(
gfx.getWidth(),
gfx.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setBackground(Color.white);
g2.clearRect(0, 0, bi.getWidth(), bi.getHeight());
if (g2.getClip() == null) {
g2.setClip(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// the main event
gfx.drawTo(g2);
// work-around an OS X bug where sometimes the last Shape drawn
// doesn't show up in the output
g2.draw(new java.awt.Rectangle(-10, -10, 5, 5));
gfx.drawTo();
}
If this works then you can draw the image using the paint() method of JFrame. First you need to make sure that this
gfx.drawTo(g2);
works, from the gfx side. Try to save the image into a file and see if its there
try {
ImageIO.write(bi, "png", new File("gfx-image.png"));
} catch (IOException ex) { ex.printStackTrace(); }
with import javax.imageio.*; in your import statements.
If that works and you can see the image then at the JFrame you need to have something like
public void paint(Graphics g) {
g.drawImage(bi, 0, 0, this);
}
Related
I'm trying to create GRAL graphs and export them as JPGs.
Not sure why, but the color of the 'Inset' areas (outside the border) seems to be defaulting to black. This is particularly confusing, as the docs seem to indicate that insets default to being white.
How can I change the color of the Inset?
Here is how I'm exporting the jpg
private void getJpg(BarPlot plot) {
BufferedImage bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) bImage.getGraphics();
DrawingContext context = new DrawingContext(g2d);
plot.draw(context);
DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/jpeg");
try {
wr.write(plot, new FileOutputStream("/path/out.jpg"), 800, 600);
} catch (IOException err) {
System.out.println(err);
}
}
Here is my current result (using white text on black bg for now, but want black text on white bg). current output
I figured it out! (with the help of a co-worker)
The Inset area defaults to being transparent black (RGBA(0,0,0,0)). Because of this, when the graph is exported as JPEG (which does not support transparency) it drops the alpha value and renders it all as black. By exporting as a png, I was able to preserve transparency and the graphs now look good when embedded into a document with a white background.
private static void getPng(XYPlot plot, String fileName) {
BufferedImage bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bImage.getGraphics();
DrawingContext context = new DrawingContext(g2d);
plot.draw(context);
DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/png");
try {
wr.write(plot, new FileOutputStream(fileName + ".png"), 800, 600);
} catch (IOException err) {
System.out.println(err);
}
}
This is my code please help and explain what I did wrong thank you very much.
Also I am a bit confuse about Thread whether I did correctly way.
public class Fade extends JPanel implements Runnable {
static Image image;
private float alpha = 0f;
static JFrame frame;
public static void main(String[] args) throws IOException {
image = new ImageIcon(ImageIO.read(new File("gummybear.jpg")))
.getImage();
frame = new JFrame("fade frame");
frame.add(new Fade());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(image.getWidth(frame), image.getHeight(frame));
// set picture in the center of screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
ExecutorService executor = Executors.newFixedThreadPool(1);
Runnable fade = new Fade();
executor.execute(fade);
// executor.shutdown();
// while (!executor.isTerminated()) {
// }
// System.out.println("Finished fade in / fade out threads");
}
public void run() {
while (alpha < 1) {
try {
System.out.println(alpha);
alpha += 0.1f;
this.repaint();
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Fader.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// SRC_OVER: If pixels in the source and the destination overlap, only
// the source
// pixels outside of the overlapping area are rendered. The pixels in
// the overlapping area are not changed.
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alpha));
g2d.drawImage(image, 0, 0, null);
// Color c = new Color(255, 255, 255, alpha);
// g2d.setColor(c);
// g2d.fillRect(0, 0, image.getWidth(frame), image.getHeight(frame));
System.out.println("repaint");
}
}
...............
Image Stuff
It looks to me that you want to add transparency to an image that was loaded from a JPG file. JPG files don't have an alpha channel (that is used for transparency), so this will not work.
You find a good example for working with transparency (and loading images from JPG) in the one of the Java tutorials: Drawing an Image
There you find a nice example with code that does nearly what you want: change the transparency of an image loaded from a JPG file. The difference is that the opacity value is taken from a slider control instead of a timed variable.
Threading Stuff
Edit: I just realized, that you are using Swing, an API that is not designed to be thread safe.
I just have to point you to the Java Tutorials How to Use Swing Timers and Concurrency in Swing then.
The problem is that you are not changing the alpha value. At least, not the alpha value of the Fade instance that you are showing:
// Here you are adding a "Fade" instance to the frame.
frame.add(new Fade());
...
// Here you are creating a NEW "Fade" instance. Only in
// this instance, the alpha value will be affected
Runnable fade = new Fade();
executor.execute(fade);
Change this to
// Create a Fade instance and add it to the frame
Fade fade = new Fade();
frame.add(fade);
...
// Submit the SAME Fade instance to the executor
executor.execute(fade);
You'll also have to verify that the alpha value remains in [0,1], but this can be done with something like
alpha += 0.1f;
alpha = Math.min(1.0f, alpha);
I am using SVGSalamander.
My code loads a svg image and sets it as background of a JDesktopPane.
File f = new File("awesome_tiger.svg");
SVGUniverse svgUniverse = new SVGUniverse();
try {
SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURL()));
try {
diagram.render(g);
}
catch(Exception ex) {System.out.println(ex);}}
catch (Exception ex2) {System.out.println(ex2);}
How can I achieve, that the image fills the window/frame completely and resizes with it?
Seems SVGSalamander has the method isScaleToFit() but how can I use it?
I use for antiAlias this: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); (this is how it is written in the SVGIcon and SVGPanel classes)
Edit: solved it
AffineTransform at = new AffineTransform();
at.setToScale(jdpPane.getWidth()/diagram.getWidth(), jdpPane.getWidth()/diagram.getWidth());
g.transform(at);
diagram.render(g);
scales it proportionally
We can use an AffineTransform and the setToScale method
File f = new File("awesome_tiger.svg");
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
SVGUniverse svgUniverse = new SVGUniverse();
try {
SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURI().toURL()));
try {
AffineTransform at = new AffineTransform();
at.setToScale(jdpPane.getWidth()/diagram.getWidth(), jdpPane.getWidth()/diagram.getWidth());
g.transform(at);
diagram.render(g);
}
catch(Exception e2) {System.out.println(e2);}}
catch (Exception ex) {System.out.println(ex);}
this is the code with which I am overriding the paint() method
I am using Graphics2D itext feature and draw image with g2.drawImage(x, y, null). If i load this image using ImageIO.read() image looks black and white in generated PDF. But Toolkit.getDefaultToolkit().createImage() works fine. Here is the code:
public static final String filename = "dummy.pdf";
public static void main(String[] args) {
try {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(filename));
writer.setPdfVersion(PdfWriter.VERSION_1_5);
doc.open();
PdfContentByte cb = writer.getDirectContent();
Graphics2D g2 = cb.createGraphics(1654, 1168);
draw(g2);
doc.close();
System.out.println("Done!");
} catch(Exception e) {
e.printStackTrace();
}
}
private static void draw(Graphics2D g2) throws Exception {
g2.setColor(Color.red);
g2.fill(new Rectangle2D.Double(0, 0, 100, 100));
BufferedImage img = ImageIO.read(new File("Speedy2BigClr.gif"));
BufferedImage bi = toBufferedImage(img);
Image i = makeColorTransparentAndBW(bi, Color.WHITE);
Image iii = Toolkit.getDefaultToolkit().createImage("Speedy2BigClr.gif");
g2.translate(0, 300);
g2.scale(0.3, 0.3);
g2.drawImage(img, 0, 0, null);
}
}
Unfortunately for another gif the ImageIO works fine, and Toolkit does not work. But all images look fine on screen. Why the way i load image affects the result in PDF?
P.S. For the clear reasons i am using LGPL iText (2.0.4, 2.1.7, 4.2.0).
UPD: works fine on iText-5.3.5, that i can not use :-(
I am making a project for Controlling a PC from any other PC on a Network.
but when i send the image from server to client, it does not changed, just the first image is displayed.
So i am sending the captured images of server to client using GZIP compression.
Here is the Server's Code:
out = new DataOutputStream(s.getOutputStream());
zipout = new GZIPOutputStream(out);
while(true)
{
img = conn.getScreenImg(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//Here conn is a Object of Robot Class
ImageIO.write(img, "jpeg", zipout);
Thread.sleep(200);
System.out.println("Success");
}
Client Code: displaying the Images sent by Server.
while(true)
{
try
{
img = ImageIO.read(zipin);
Graphics g = this.getGraphics();
g.drawImage(img, 0, 0, this);
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
}
I need Help with this. The image doesn't change on client.
and i want to know that if it is good to use GZIP here for compression for images to send over network , will it accelerate the process. or should i use some other method.
The way you are displaying the images on the client doesn't seem right to me. It would be a lot better to write a custom JPanel that would override the paintComponent method and that would handle the image rendering. Something like below:
class ImagePanel extends JPanel {
private BufferedImage image = null;
private void setImage(BufferedImage img) {
image = img;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (image != null) {
g2.drawImage(image, 0, 0, null);
}
}
}
Inside your while loop you would call the setImage method on the panel. The panel in of course previously instantiated and added to a container.