【发布时间】: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 调用绘制组件?你能给我举个例子,我应该怎么做才能修复它?
-
我现在一直在编辑我的脚本