Hi, I would need help, pleas. I would like to portray the curve under the specified angles with the repeat and the number of segments. Will you help me please?

now I have this:

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
    }
}

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

you have this

public void paint(Graphics g) {
                super.paint(g);  
              if (draw) {  
                  g.drawLine(0, 0, 1000, 1000); 
               } else {                } 
           } 
       };

i'm not as familiar with writing methods in the define of the panel like your doing, but if you created a panel class, this is what you'd do ( and it may work with this code the way your doing it:

class mypanel extends JPanel
{

mypanel() {} // default constructor dont know if you need this

  public void  paintComponent(Graphics g) {
                super.paintComponent(g);  
              if (draw) {  
                  g.drawLine(0, 0, 1000, 1000); 
               } else {                } 
           } // end method
}// end class

essentially all you have to do i think is change paint() to paintComponent(). this has worked in my programs and i seem to recall reading this is the way you have to do it. to repaint call repaint();

i.e. if you have mypanel aPanel = new mypanel();
aPanel.repaint() will repaint. you can also call repaint with (x,y,width,height) to specify only repaint that part of the screen. repaint by itself repaints teh whole screen. your paint components method doesnt care how much is repainted, it draws everything, but when it comes time to write to the screen by the java enviorment it will not try to write over thre full screen if you specify an area to repaint. for your program i'd just use repaint, but repaint with a an area defined is good if callign repaint many times and trying to reduce how busy your program is, writing to the whole screen every time gets expensive. You only need to call repaint when there is a reason to update the screen, i. e you made a change. if you want to draw one line and call it a day, you wont need repaint.

Mike

Edited 4 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.