I can not save the figure an image.
import java.awt.*;import javax.swing.*;import gov.noaa.pmel.sgt.ColorMap;import gov.noaa.pmel.sgt.ContourLevels;import gov.noaa.pmel.sgt.GridAttribute;import gov.noaa.pmel.sgt.IndexedColorMap;import gov.noaa.pmel.sgt.LinearTransform;import gov.noaa.pmel.sgt.demo.TestData;import gov.noaa.pmel.sgt.dm.SGTData;import gov.noaa.pmel.sgt.swing.JPlotLayout;import gov.noaa.pmel.util.Dimension2D;import gov.noaa.pmel.util.Range2D;import gov.noaa.pmel.util.Rectangle2D;import java.io.*;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;public class Panel_PINTAR_MAPA extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame("PAINT DEMO"); Panel_PINTAR_MAPA panel = new Panel_PINTAR_MAPA(); panel.setSize(1620, 810); JPlotLayout figure = makeGraph(); panel.add(figure); JButton boton = new JButton("boton"); panel.add(boton); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); panel.saveImage("IMAGE_PANEL", "png"); } public void saveImage(String name, String type) { BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); paint(g2); try { ImageIO.write(image, type, new File(name + "." + type)); } catch (Exception e) { e.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); } private static JPlotLayout makeGraph() { GridAttribute gridAttr_; /* * This example uses a pre-created "Layout" for raster time series to * simplify the construction of a plot. The JPlotLayout can plot a * single grid with a ColorKey, time series with a LineKey, point * collection with a PointCollectionKey, and general X-Y plots with a * LineKey. JPlotLayout supports zooming, object selection, and object * editing. */ SGTData newData; TestData td; JPlotLayout rpl; ContourLevels clevels; /* * Create a test grid with sinasoidal-ramp data. */ Range2D xr = new Range2D(190.0f, 250.0f, 1.0f); Range2D yr = new Range2D(0.0f, 45.0f, 1.0f); td = new TestData(TestData.XY_GRID, xr, yr, TestData.SINE_RAMP, 12.0f, 30.f, 5.0f); newData = td.getSGTData(); /* * Create the layout without a Logo image and with the ColorKey on a * separate Pane object. */ rpl = new JPlotLayout(true, false, false, "test layout", null, true); rpl.setEditClasses(false); /* * Create a GridAttribute for CONTOUR style. */ Range2D datar = new Range2D(-20.0f, 45.0f, 5.0f); clevels = ContourLevels.getDefault(datar); gridAttr_ = new GridAttribute(clevels); /* * Create a ColorMap and change the style to RASTER_CONTOUR. */ ColorMap cmap = createColorMap(datar); gridAttr_.setColorMap(cmap); gridAttr_.setStyle(GridAttribute.RASTER_CONTOUR); /* * Add the grid to the layout and give a label for the ColorKey. */ rpl.addData(newData, gridAttr_, "First Data"); /* * Change the layout's three title lines. */ rpl.setTitles("Raster Plot Demo", "using a JPlotLayout", ""); /* * Resize the graph and place in the "Center" of the frame. */ rpl.setSize(new Dimension(1620, 810)); /* * Resize the key Pane, both the device size and the physical size. Set * the size of the key in physical units and place the key pane at the * "South" of the frame. */ rpl.setKeyLayerSizeP(new Dimension2D(6.0, 1.02)); rpl.setKeyBoundsP(new Rectangle2D.Double(0.01, 1.01, 5.98, 1.0)); return rpl; } private static ColorMap createColorMap(Range2D datar) { int[] red = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 23, 39, 55, 71, 87, 103, 119, 135, 151, 167, 183, 199, 215, 231, 247, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 246, 228, 211, 193, 175, 158, 140 }; int[] green = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 27, 43, 59, 75, 91, 107, 123, 139, 155, 171, 187, 203, 219, 235, 251, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 247, 231, 215, 199, 183, 167, 151, 135, 119, 103, 87, 71, 55, 39, 23, 7, 0, 0, 0, 0, 0, 0, 0 }; int[] blue = { 0, 143, 159, 175, 191, 207, 223, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 247, 231, 215, 199, 183, 167, 151, 135, 119, 103, 87, 71, 55, 39, 23, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; IndexedColorMap cmap = new IndexedColorMap(red, green, blue); cmap.setTransform(new LinearTransform(0.0, (double) red.length, datar.start, datar.end)); return cmap; }}It is save as white, I show this wrong result:
أكثر...
import java.awt.*;import javax.swing.*;import gov.noaa.pmel.sgt.ColorMap;import gov.noaa.pmel.sgt.ContourLevels;import gov.noaa.pmel.sgt.GridAttribute;import gov.noaa.pmel.sgt.IndexedColorMap;import gov.noaa.pmel.sgt.LinearTransform;import gov.noaa.pmel.sgt.demo.TestData;import gov.noaa.pmel.sgt.dm.SGTData;import gov.noaa.pmel.sgt.swing.JPlotLayout;import gov.noaa.pmel.util.Dimension2D;import gov.noaa.pmel.util.Range2D;import gov.noaa.pmel.util.Rectangle2D;import java.io.*;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;public class Panel_PINTAR_MAPA extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame("PAINT DEMO"); Panel_PINTAR_MAPA panel = new Panel_PINTAR_MAPA(); panel.setSize(1620, 810); JPlotLayout figure = makeGraph(); panel.add(figure); JButton boton = new JButton("boton"); panel.add(boton); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); panel.saveImage("IMAGE_PANEL", "png"); } public void saveImage(String name, String type) { BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); paint(g2); try { ImageIO.write(image, type, new File(name + "." + type)); } catch (Exception e) { e.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); } private static JPlotLayout makeGraph() { GridAttribute gridAttr_; /* * This example uses a pre-created "Layout" for raster time series to * simplify the construction of a plot. The JPlotLayout can plot a * single grid with a ColorKey, time series with a LineKey, point * collection with a PointCollectionKey, and general X-Y plots with a * LineKey. JPlotLayout supports zooming, object selection, and object * editing. */ SGTData newData; TestData td; JPlotLayout rpl; ContourLevels clevels; /* * Create a test grid with sinasoidal-ramp data. */ Range2D xr = new Range2D(190.0f, 250.0f, 1.0f); Range2D yr = new Range2D(0.0f, 45.0f, 1.0f); td = new TestData(TestData.XY_GRID, xr, yr, TestData.SINE_RAMP, 12.0f, 30.f, 5.0f); newData = td.getSGTData(); /* * Create the layout without a Logo image and with the ColorKey on a * separate Pane object. */ rpl = new JPlotLayout(true, false, false, "test layout", null, true); rpl.setEditClasses(false); /* * Create a GridAttribute for CONTOUR style. */ Range2D datar = new Range2D(-20.0f, 45.0f, 5.0f); clevels = ContourLevels.getDefault(datar); gridAttr_ = new GridAttribute(clevels); /* * Create a ColorMap and change the style to RASTER_CONTOUR. */ ColorMap cmap = createColorMap(datar); gridAttr_.setColorMap(cmap); gridAttr_.setStyle(GridAttribute.RASTER_CONTOUR); /* * Add the grid to the layout and give a label for the ColorKey. */ rpl.addData(newData, gridAttr_, "First Data"); /* * Change the layout's three title lines. */ rpl.setTitles("Raster Plot Demo", "using a JPlotLayout", ""); /* * Resize the graph and place in the "Center" of the frame. */ rpl.setSize(new Dimension(1620, 810)); /* * Resize the key Pane, both the device size and the physical size. Set * the size of the key in physical units and place the key pane at the * "South" of the frame. */ rpl.setKeyLayerSizeP(new Dimension2D(6.0, 1.02)); rpl.setKeyBoundsP(new Rectangle2D.Double(0.01, 1.01, 5.98, 1.0)); return rpl; } private static ColorMap createColorMap(Range2D datar) { int[] red = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 23, 39, 55, 71, 87, 103, 119, 135, 151, 167, 183, 199, 215, 231, 247, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 246, 228, 211, 193, 175, 158, 140 }; int[] green = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 27, 43, 59, 75, 91, 107, 123, 139, 155, 171, 187, 203, 219, 235, 251, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 247, 231, 215, 199, 183, 167, 151, 135, 119, 103, 87, 71, 55, 39, 23, 7, 0, 0, 0, 0, 0, 0, 0 }; int[] blue = { 0, 143, 159, 175, 191, 207, 223, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 247, 231, 215, 199, 183, 167, 151, 135, 119, 103, 87, 71, 55, 39, 23, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; IndexedColorMap cmap = new IndexedColorMap(red, green, blue); cmap.setTransform(new LinearTransform(0.0, (double) red.length, datar.start, datar.end)); return cmap; }}It is save as white, I show this wrong result:

أكثر...