java - JPanel 问题 - 在正常外观上重新绘制调整大小

标签 java swing svg batik

我在使用 JPanel 时遇到了奇怪的问题。我正在尝试显示 svg 图像( SVG_class 从 batik jar 扩展 JSVGCanvas )。问题是当我启动这个程序时我得到了这个

enter image description here

当我用指针框架稍微调整大小时,我得到这样的正常图片

enter image description here

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas;

public class Main {

static JScrollPane scrollpane;
// The frame.
protected JFrame frame;

public static void main(String[] args) {

final JFrame f = new JFrame("frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700, 500);
f.setBackground(Color.blue);

SVG_class svg = new SVG_class();
JPanel p = new JPanel();
p.setSize(new Dimension(700, 500));
p.add(svg);
p.setBackground(Color.red);
scrollpane = new JScrollPane(p);
scrollpane.setPreferredSize(new Dimension(700, 500));

Container cont = f.getContentPane();
cont.add(scrollpane);

f.setVisible(true);
}

}

public class SVG_class extends JSVGCanvas {
private List<Type> list;
private int rad;
public boolean red_dot(int iX, int iY, String sX, String sY){
boolean b;
int x,y;
x=Math.abs(iX-(int)Double.parseDouble(sX));
y=Math.abs(iY-(getSize().height-(int)Double.parseDouble(sY)));
System.out.println("iX="+iX+" iY="+iY+" sX="+sX+" sY="+sY+" x="+x+" y="+y);
b=(x<=rad) &&(y<=rad);
return b;
}

    public SVG_class(){
    try {
    this.list=CSV.getCSV("map2");
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String fileName = "map2";
    File file = new File("C:\\Users\\Gigabyte\\Desktop\\SVG\\"+fileName+".svg");
    try {
    this.setURI(file.toURL().toString());
                this.addMouseListener(new MouseListener() {
    @Override
    public void mouseReleased(MouseEvent e) {
    //
    }
    @Override
    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    boolean found=false;
    // System.out.println("pritisnuo si x="+e.getX()+" y="+e.getY());
    int x,y;
    for(int i=0; i<list.size() && !found;i++ ){
    found=red_dot(e.getX(), e.getY(), list.get(i).getX(), list.get(i).getY());
    }
    if (found) {
    System.out.println("pritisao!!!!");
    }
    System.out.println(" ");
    }
    });
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

谁能告诉我错误是什么?我尝试使用 p.repaint 但它没有帮助。

最佳答案

发布 SVG_class 的源代码可能会更容易看出问题所在。

实际上,我怀疑问题是因为您没有明确告诉它要使用哪种布局。

如果你想让 svg 占据整个窗口,你可以在创建 p 之后执行 p.setLayout(new GridLayout(1, 1));

关于java - JPanel 问题 - 在正常外观上重新绘制调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6623357/

相关文章:

svg - html2canvas可以在页面中呈现svg吗?

java - Neo4j Lucene索引-使用通配符执行模糊搜索

java - 使用 lambda 表达式从对象列表中选择对象

java - 根据用户选择/输入从集合中过滤对象

java - FPL论坛申请

javascript - 动态更新矩形的宽度和高度

java - 设计模式问题

java - 为什么我在 Swing 的 JButton 中的 html 代码偏离中心?

java - 两个不同 JTable 之间的 TableModelListener

javascript - SVG 使用和 jQuery 上的 Mouseenter/Mouseleave