如何在 XYImageAnnotation 中调整图像位置
How to adjust Image position in XYImageAnnotation
我在 JFreeChart 中有一个图标:
BufferedImage torreIcon = null;
try {
torreIcon = ImageIO.read(getClass().getClassLoader().getResource(
"Resources/Imagenes/torre_control_2.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
并且我已将此图标添加到 XYPlot
:
XYAnnotation xyannotation = new XYImageAnnotation(0, 1900, torreIcon);
this.xyplot.addAnnotation(xyannotation);
图表中的结果是:
问题是当我放大时:
我希望图标始终位于 x 轴旁边,而不是 space,并且当然具有相同的大小。可能吗?
If I put (0, 0) coordinates, the icon center is put in 0, 0 and the image is not above x axis.
正确;除非另有说明,否则 XYImageAnnotation
使用 RectangleAnchor.CENTER
.
在指定数据 space 坐标处 居中
I put manually the coordinates that makes the icon put above the x axis initially, and as we can see the y coordinate that make this is 1900.
而是在构造 XYImageAnnotation
时指定所需的 RectangleAnchor
:
new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM)
下面的示例将蓝色框添加到方便的图像中,以查看图像相对于锚点的绘制方式。原点的 RectangleAnchor.BOTTOM
如下图所示。当您平移和缩放时,图像保持锚定到原点。尝试其他 RectangleAnchor
个实例以查看效果。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see
*/
public class XYImageTest {
private static final int N = 1;
private XYDataset createDataset() {
XYSeries series = new XYSeries("Series");
for (int i = -N; i <= N; i++) {
series.add(i, i);
}
return new XYSeriesCollection(series);
}
private JFreeChart createChart(final XYDataset dataset, Image image) {
JFreeChart chart = ChartFactory.createXYLineChart("Test", "X", "Y", dataset);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.addAnnotation(new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM));
return chart;
}
private void display() {
Icon icon = UIManager.getIcon("OptionPane.informationIcon");
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.setColor(Color.blue);
g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
icon.paintIcon(null, g, 0, 0);
g.dispose();
JFrame f = new JFrame("XYImageTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ChartPanel(createChart(createDataset(), image)) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new XYImageTest()::display);
}
}
我在 JFreeChart 中有一个图标:
BufferedImage torreIcon = null;
try {
torreIcon = ImageIO.read(getClass().getClassLoader().getResource(
"Resources/Imagenes/torre_control_2.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
并且我已将此图标添加到 XYPlot
:
XYAnnotation xyannotation = new XYImageAnnotation(0, 1900, torreIcon);
this.xyplot.addAnnotation(xyannotation);
图表中的结果是:
问题是当我放大时:
我希望图标始终位于 x 轴旁边,而不是 space,并且当然具有相同的大小。可能吗?
If I put (0, 0) coordinates, the icon center is put in 0, 0 and the image is not above x axis.
正确;除非另有说明,否则 XYImageAnnotation
使用 RectangleAnchor.CENTER
.
I put manually the coordinates that makes the icon put above the x axis initially, and as we can see the y coordinate that make this is 1900.
而是在构造 XYImageAnnotation
时指定所需的 RectangleAnchor
:
new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM)
下面的示例将蓝色框添加到方便的图像中,以查看图像相对于锚点的绘制方式。原点的 RectangleAnchor.BOTTOM
如下图所示。当您平移和缩放时,图像保持锚定到原点。尝试其他 RectangleAnchor
个实例以查看效果。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see
*/
public class XYImageTest {
private static final int N = 1;
private XYDataset createDataset() {
XYSeries series = new XYSeries("Series");
for (int i = -N; i <= N; i++) {
series.add(i, i);
}
return new XYSeriesCollection(series);
}
private JFreeChart createChart(final XYDataset dataset, Image image) {
JFreeChart chart = ChartFactory.createXYLineChart("Test", "X", "Y", dataset);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.addAnnotation(new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM));
return chart;
}
private void display() {
Icon icon = UIManager.getIcon("OptionPane.informationIcon");
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.setColor(Color.blue);
g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
icon.paintIcon(null, g, 0, 0);
g.dispose();
JFrame f = new JFrame("XYImageTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ChartPanel(createChart(createDataset(), image)) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new XYImageTest()::display);
}
}