1

I want to make program load line from database.I've been setting at database every coordinat using double .but im getting error when i try to load from database and want to drawline in jpanel it's didn't appear in my jpanel.how to fix it? this is my code.

    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");

    }}

and this is my paint component method

 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);
    }
}
FRIDY
  • 61
  • 10

1 Answers1

3

This should throw a NullPointerException. Where do you initialize your g2d variable? Your graphics look to be done incorrectly. You should 1) read the data in within a background thread, and store it into a collection, probably an ArrayList. Once this is done, notify Swing on the event thread so that you can draw the data within a JPanel's paintComponent method. See the painting tutorial for more on Swing graphics.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373