Results 1 to 7 of 7

Thread: Draw line

  1. #1
    janes is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default Draw line

    Hi, I would need help, pleas. I would like draw the line with the specified angles, with the repeat and the number of segments. Will you help me please?

    now I have this:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.HeadlessException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    /**
    *
    * @author martin.juranek
    */
    public class Main extends JFrame {
    
    private boolean draw = false;
    JPanel paintPanel;
    
    public Main() throws HeadlessException {
    JButton jb = new JButton("Cudl");
    jb.addActionListener(new ActionListener() {
    
    public void actionPerformed(ActionEvent e) {
    draw = true;
    System.out.println("kresli = " + draw);
    paintPanel.repaint();
    
    }
    });
    paintPanel = new JPanel() {
    
    @Override
    public void paint(Graphics g) {
    super.paint(g);
    if (draw) {
    g.drawLine(0, 0, 1000, 1000);
    } else {
    }
    }
    };
    BorderLayout bl = new BorderLayout();
    bl.addLayoutComponent(jb, BorderLayout.NORTH);
    bl.addLayoutComponent(paintPanel, BorderLayout.CENTER);
    getContentPane().add(jb);
    getContentPane().add(paintPanel);
    setLayout(bl);
    paintPanel.setPreferredSize(new Dimension(500, 500));
    jb.setPreferredSize(new Dimension(50, 100));
    
    pack();
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    
    public void run() {
    Main m = new Main();
    m.setVisible(true);
    }
    });
    // TODO code application logic here
    }
    }
    Last edited by Fubarable; 01-24-2010 at 10:21 PM. Reason: code tags added

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MainLine extends JFrame {
        private boolean draw = false;
        JPanel paintPanel;
    
        public MainLine() throws HeadlessException {
            JButton jb = new JButton("Cudl");
            jb.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    draw = true;
                    System.out.println("kresli = " + draw);
                    paintPanel.repaint();
                }
            });
            paintPanel = new JPanel() {
                double theta = Math.toRadians(25);
                int n = 4;
    
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    if (draw) {
                        for(int i = 0; i < n; i++) {
                            int x1 = 50 + i*25;
                            int y1 = 50 + i*25;
                            int x2 = (int)(x1 + 100*Math.cos(theta));
                            int y2 = (int)(y1 + 100*Math.sin(theta));
                            g.drawLine(x1, y1, x2, y2);
                        }
                    }
                }
            };
            System.out.println("Default layout manager for JFrame: " +
                                getLayout().getClass().getName());
            BorderLayout bl = new BorderLayout();
            bl.addLayoutComponent(jb, BorderLayout.NORTH);
            bl.addLayoutComponent(paintPanel, BorderLayout.CENTER);
            getContentPane().add(jb);
            getContentPane().add(paintPanel);
            setLayout(bl);
            paintPanel.setPreferredSize(new Dimension(500, 500));
            jb.setPreferredSize(new Dimension(50, 100));
    
            pack();
        }
    
        public static void main(String[] args) {
            MainLine m = new MainLine();
            m.setVisible(true);
        }
    }

  3. #3
    janes is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    thank you very very much, and how would I do if I wanted the segments stacked one after another. So that one followed after another. Thanks:)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Play with the code inside the if statement block:
    Java Code:
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
                if (draw) {
                    ... here ...
                }
            }
    You have to experiment, imagine, try different things to get what you want.
    Graphics is hands–on, imaginative work.

  5. #5
    janes is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    ok thanks, iŽll try it :)

  6. #6
    abhiram naidu is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default

    what do u mean by contentpane?

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by abhiram naidu View Post
    what do u mean by contentpane?
    I think it is a panel that holds the content.

Similar Threads

  1. Draw more than one line on run time
    By aiman in forum Java Applets
    Replies: 3
    Last Post: 12-10-2009, 02:44 AM
  2. How to: draw line between 2 nodes of 2 trees
    By kholostoi in forum Java 2D
    Replies: 1
    Last Post: 09-03-2009, 07:10 PM
  3. When do we draw the line for loose coupling.
    By h8alfred in forum Advanced Java
    Replies: 0
    Last Post: 03-27-2009, 03:05 AM
  4. How to Draw line in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:08 PM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •