【问题标题】:ask error load drawline问错误加载画线
【发布时间】:2015-09-18 16:44:47
【问题描述】:

我想从数据库中创建程序加载线。我一直在使用double 设置每个坐标在数据库中。但是当我尝试从数据库加载并想在 jpanel 中绘制它时出现错误,它没有出现在我的 jpanel.how 修复它?这是我的代码。

    ArrayList<Shape> linesList = new ArrayList<Shape>();
    shape line = null;

    public void loadline(){
        while (rs.next()) {
            double x1 = rs.getDouble("coorX");     //getting coordinate
            double y1 = rs.getDouble("coorY");
            double x2 = rs.getDouble("coorX2");
            double y2 = rs.getDouble("coorY2");
           line =new Line2D.Double(x1,y1,x2,y2);
           linesList.add(line);
        repaint();
        revalidate();
        }
    }catch(Exception E){
JOptionPane.showMessageDialog(null, "error load data");
}
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "error sql");

    }}

这是我的绘制组件方法

 protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(birumuda);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for(Shape content : linesList){
        g2d.draw(content);
    }
}

【问题讨论】:

  • 你遇到了什么错误?
  • 你必须将值从 JDBC 加载到 util.List,然后通过 JCOmponent.repaint 调用paintComponent,在paintComponent 内部循环存储在 util.List 中的 vakues
  • shape是什么意思?
  • 我在画线时出错。 @mKorbel 如何通过 jcomponent repaint 调用绘制组件?你能给我举个例子,我应该怎么做才能修复它?
  • 我现在一直在编辑我的脚本

标签: java mysql swing


【解决方案1】:

这应该抛出 NullPointerException。你在哪里初始化你的 g2d 变量?您的图形看起来做得不正确。您应该 1) 在后台线程中读取数据,并将其存储到一个集合中,可能是一个 ArrayList。完成后,在事件线程上通知 Swing,以便您可以在 JPanel 的 paintComponent 方法中绘制数据。有关 Swing 图形的更多信息,请参阅painting tutorial

【讨论】:

  • 你能给我举个例子我应该怎么做吗?
  • @FRIDY:类似于我在回答类似问题here中的代码。
  • 我现在一直在编辑我的脚本。就像这样。但是为什么现在没有出现?
  • @FRIDY:没有可运行的代码很难说。可能是组件自己绘制时linesList集合为空,也可能是组件太小而无法显示线条。此时最好的选择是创建并发布minimal reproducible example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 2022-01-14
  • 1970-01-01
  • 2015-09-08
相关资源
最近更新 更多