1

As shown below in code, i'm getting x and y values of line from the database. And then storing them in an array x. After that i'm trying to draw this line on the Frame, but it is not getting drawn. How can i draw line on Frame?

public class TestFrame{
    static JFrame test;
    public static void main(String ar[]){
        test=new JFrame("Test");
        JButton openLine=new JButton(new AbstractAction("Open Line"){

            public void actionPerformed(ActionEvent e) {
                String lineId=JOptionPane.showInputDialog("Enter Line id");
                ImageComponent image=new ImageComponent();
                image.openLine(lineId);
            }

        });
        test.add(openLine, BorderLayout.NORTH);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(600,600);
        test.setVisible(true);

    }
    static class ImageComponent extends JComponent{
        static int[] x=new int[100];
        static ArrayList al=new ArrayList();
        public void openLine(String line_id){

                            try {

                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                Connection con=DriverManager.getConnection("jdbc:odbc:image");
                                Statement pstm=con.createStatement();
                                ResultSet rs=pstm.executeQuery("select * from Line where ID= '"+line_id+"'");
                                while(rs.next()){
                                    x[0]=rs.getInt(3);
                                    x[1]=rs.getInt(4);
                                    x[2]=rs.getInt(5);
                                    x[3]=rs.getInt(6);

                                    al.add(x);

                                }

                                repaint();

                            } catch (Exception ex) {
                                System.out.println("Exception : "+ex);
                            }
                }
                public Graphics2D gd;
                Line2D[] line=new Line2D[100];
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
                        gd=(Graphics2D)g;

                        gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                            for(int i=0;i<al.size();i++){
                                line[i]=new Line2D.Double(x[0], x[1],x[2],x[3]);
                                gd.draw(line[i]);

                            }

                        }
                }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Parth Soni
  • 11,158
  • 4
  • 32
  • 54

4 Answers4

4

Here is one approach that uses a BufferedImage as a rendering surface.

The OP in that question was asking about an applet, but I displayed the image in an option pane. It is just as well suited to display in a frame etc. without any confusion over whether to override paint(Graphics) or paintComponent(Graphics). ;)

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Umm.. substitute 'draw line of frame' with 'draw line on image (and put it on frame if you like)' and there is a working (animated) example. Did you try running the code? – Andrew Thompson Apr 16 '12 at 12:57
1

You can use the Graphics object and override the paint() method like so:

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(Color.gray);
    int x = 5;
    int y = 7;

    g2.draw(new Line2D.Double(x, y, 200, 200));
    g2.drawString("Line2D", x, 250);

  }

Taken from here

npinti
  • 51,780
  • 5
  • 72
  • 96
  • I need to draw line using `paintComponent()` as there's some other methods which is not shown here.. So, how can i do such a thing? – Parth Soni Apr 16 '12 at 10:43
1

You shouldn't directly on a JFrame. All rendering in the main body of JFrame is done in the contentpane. Menu content is done in the JMenuBar

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

One issue is that you create a new ImageComponent every time the button is clicked but it is not added to your frame. Another one is that you fill in an arraylist (al) but don't use it to actually draw your line.

This works for me with dummy values for x[0], x[1], x[2], x[3]:

static JFrame test;
static ImageComponent image = new ImageComponent(); //declared as a class member

public static void main(String ar[]) {
    test = new JFrame("Test");
    JButton openLine = new JButton(new AbstractAction("Open Line") {

        public void actionPerformed(ActionEvent e) {
            String lineId = JOptionPane.showInputDialog("Enter Line id");
            image.openLine(lineId);
        }
    });
    test.add(openLine, BorderLayout.NORTH);
    test.add(image); //ADD THE IMAGE TO THE FRAME
    image.setVisible(true);
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setSize(600, 600);
    test.setVisible(true);

}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • would you please show the whole code that worked for you, I'm trying the same but it's not working for me.. – Parth Soni Apr 19 '12 at 19:02