是否只有Java方法在JScrollPane中显示更大的图片?我不想重新发明轮子,我已经在使用JLabel技巧中的ImageIcon显示32768x400图像,因为似乎存在与平台相关的ImageIcon的限制。 Ubuntu 16.10不会显示任何大小为32768x400的ImageIcon,尽管它显示的是较小的。 Win10显示它们全部....并且甚至没有任何类型的错误输出,这很糟糕,因为我只是浪费时间搜索问题。
那么有什么简单的解决方案可以不需要我重新发明轮子吗?
特别是,我想显示波形,即。一系列浮点数,因此实际上根本不需要整体图像。
答案 0 :(得分:1)
我相信这表明了如何做你想做的事。请注意Graph
组件的宽度为65535.这可以通过在滚动时仅绘制图形的可见部分来进一步优化,但它的速度相当快。
import java.awt.*;
import javax.swing.*;
import java.util.function.Function;
class Graph extends JComponent {
private Function<Double, Double> fun;
public Graph(Function<Double, Double> fun) {
this.fun = fun;
setPreferredSize(new Dimension(65535, 300));
}
public void paintComponent(Graphics g) {
// clear background
g.setColor(Color.white);
Rectangle bounds = getBounds();
int w = bounds.width;
int h = bounds.height;
g.fillRect(bounds.x, bounds.y, w, h);
// draw the graph
int prevx = 0;
int prevy = fun.apply((double)prevx).intValue();
g.setColor(Color.black);
for (int i=1; i<w; i++) {
int y = fun.apply((double)i).intValue();
g.drawLine(prevx, prevy, i, y);
prevx = i;
prevy = y;
}
}
}
public class Wf {
public static void main(String[] args) {
JFrame f = new JFrame();
// we're going to draw A sine wave for the width of the
// whole Graph component
Graph graph = new Graph(x -> Math.sin(x/(2*Math.PI))*100+200);
JScrollPane jsp = new JScrollPane(graph);
f.setContentPane(jsp);
f.setSize(800, 600);
f.setVisible(true);
}
}