Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Change the random draw line here for a mouseListener draw

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change the random draw line here for a mouseListener draw

    Hi, im working on a "paint" program. So far, i have a GUI with 1 button "Ligne" and one drawable panel. in my class Paint_Dessin, theres a method call TracerLigne(). this method draw line folowing a random patern. What i want to do is put a mouselistener so x1,y1 = click1 and x2,y2 = click 2. this is my code. Thank you (sorry for the french comment)

    **************************************
    //cree une fenetre
    public class QUESTION
    {
    public static void main(String[] args)
    {
    Paint_GUI test2 = new Paint_GUI();
    }
    }
    ***********************************
    ***********************************
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class Paint_GUI extends JFrame
    {
    //Panels contenant tout les bouton de mon interface
    private JPanel panelBtn;

    //Bar d'outil Btn
    private JButton BtnTracerLigne;

    //créer l'objet Paint_Dessin
    private Paint_Dessin espaceDessin = new Paint_Dessin();

    public Paint_GUI()
    {
    final int WINDOW_WIDTH = 650;
    final int WINDOW_HEIGHT = 450;

    setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
    setTitle("Paint v.2.0");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    // Appeler la methode qui construit la barre de BTN.
    buildPanelBtn();
    add(panelBtn, BorderLayout.NORTH);
    add(espaceDessin, BorderLayout.CENTER);

    // Afficher la fenetre.
    setVisible(true);
    }

    private void buildPanelBtn()
    {
    BtnTracerLigne = new JButton("Ligne");
    BtnTracerLigne.addActionListener(new LigneListener());

    // Creer le panel.
    panelBtn = new JPanel();
    // Ajouter les composantes au label
    panelBtn.add(BtnTracerLigne);
    }
    private class LigneListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    espaceDessin.TracerLigne();
    }
    }
    }
    ********************************************
    *******************************************

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.image.*;

    class Paint_Dessin extends JPanel {
    private static final long serialVersionUID = -2110723486099015303L;
    private static final Random RAND = new Random();
    private BufferedImage buffer = null;

    @Override
    public void paintComponent(final Graphics g) {
    final Graphics2D g2 = (Graphics2D) g;
    g2.clearRect(0, 0, getWidth(), getHeight()); // cleanup du composant
    g2.drawImage(getBuffer(), null, 0, 0);
    }

    public void TracerLigne() {
    final Graphics2D g2 = getBuffer().createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.BLACK);
    // dessin la ligne au pif dans l'espace visible
    final int x1 = RAND.nextInt(500); // position en X1
    final int y1 = RAND.nextInt(500); // position en Y1
    final int x2 = RAND.nextInt(500); // position en X2
    final int y2 = RAND.nextInt(500); // position en Y2
    g2.drawLine(x1, y1, x2, y2);
    Line2D.Double line = new Line2D.Double(x1, y1, x2, y2);
    g2.fill(line);
    repaint();
    }

    private BufferedImage getBuffer() {
    if (buffer == null)
    buffer = new BufferedImage(getWidth(), getHeight(),
    BufferedImage.TYPE_INT_ARGB);
    return buffer;
    }
    }
    ***********************************************
    Last edited by Panda23; February 9th, 2011 at 08:40 PM. Reason: had extra code


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Change the random draw line here for a mouseListener draw

    click here
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 06:49 PM
  2. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 07:15 PM
  3. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM
  4. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 21st, 2009, 12:30 AM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 01:14 AM

Tags for this Thread