Results 1 to 7 of 7
Thread: Draw line
- 01-24-2010, 08:58 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
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
- 01-25-2010, 07:10 AM #2
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); } }
- 01-25-2010, 01:11 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
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:)
- 01-25-2010, 05:06 PM #4
Play with the code inside the if statement block:
You have to experiment, imagine, try different things to get what you want.Java Code:protected void paintComponent(Graphics g) { super.paintComponent(g); if (draw) { ... here ... } }
Graphics is hands–on, imaginative work.
- 01-26-2010, 10:32 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
ok thanks, i´ll try it :)
- 03-22-2010, 03:15 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
what do u mean by contentpane?
- 03-25-2010, 10:48 PM #7
Similar Threads
-
Draw more than one line on run time
By aiman in forum Java AppletsReplies: 3Last Post: 12-10-2009, 02:44 AM -
How to: draw line between 2 nodes of 2 trees
By kholostoi in forum Java 2DReplies: 1Last Post: 09-03-2009, 07:10 PM -
When do we draw the line for loose coupling.
By h8alfred in forum Advanced JavaReplies: 0Last Post: 03-27-2009, 03:05 AM -
How to Draw line in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:08 PM -
How to draw a thick line
By johnt in forum Java 2DReplies: 1Last Post: 05-31-2007, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks