Results 1 to 7 of 7
  1. #1
    New to JAVA is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Question draw in jpanel using values input in textbox

    Hello!

    I am learning core JAVA and I am doing a project on "My Paint Application".
    I am facing trouble while using the values from a textbox to draw using paintComponent. the paint page has buttons for "line", "arc" and other shapes, and has a jpanel which is defined in another class.

    when the user clicks on "line" button, for example, a dialog opens, where the user inputs the coordinates required for drawing a line. I have accepted the input, but i do not understand how to use this input to draw on the jpanel that is inserted in my paint page.

    Can anyone please help me with this? I'm stuck up with it.

    I have attached some of my code for reference.
    Attached Files Attached Files

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're going to post some code, you might as well post all so that this code will compile and run.

  3. #3
    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 java.util.Random;
    import javax.swing.*;
    
    public class LineExample {
        DrawingComponent drawComponent = new DrawingComponent();
        Random seed = new Random();
    
        private JPanel getUIPanel() {
            JButton button = new JButton("line");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Point[] pts = getRandomData();
                    drawComponent.setLine(pts);
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        /**
         * In place of your dialog for user input.
         * Quick and easy...
         */
        private Point[] getRandomData() {
            int w = drawComponent.getWidth();
            int h = drawComponent.getHeight();
            Point[] pts = new Point[2];
            pts[0] = new Point(next(w), next(h));
            pts[1] = new Point(next(w), next(h));
            return pts;
        }
    
        private int next(int n) {
            return seed.nextInt(n);
        }
    
        public static void main(String[] args) {
            LineExample test = new LineExample();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.drawComponent);
            f.add(test.getUIPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }
    
    class DrawingComponent extends JPanel {
        Point lineStart = new Point();
        Point lineEnd   = new Point();
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawLine(lineStart.x, lineStart.y, lineEnd.x, lineEnd.y);
        }
    
        public void setLine(Point[] pts) {
            lineStart = pts[0];
            lineEnd   = pts[1];
            repaint();
        }
    }

  4. #4
    New to JAVA is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks hardwired, for your reply. But, is there any way to accept input coordinates from the user to use in my code?

    i have tried executing the code, omiting the above part. and i am now facing some other problems.

    when i click on a shape, i am able to draw it, but when i click on another shape, the previous shape is deleted. i think this has something to do with the repaint() method. Please can you tell me where in my code, "MyPaint", attached here, should i call repaint()?

    one more thing i want to know is, what should i do to clear the canvas area?

    Please help.
    Attached Files Attached Files

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java Code:
    jname = new JLabel("MY PAINT APPLICATION",JLabel.CENTER);
    add another field that is a JTextField or some JText someting or other, add as you are in
    Java Code:
    buttonpanel.add(jarc);
    and so on.

    JText classes have a getText() method that will bring to your code what the user entered as a String.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    New to JAVA is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Thank you sir, for your kind reply.
    But i would like to clarify where i am stuck up. Any suggestions would be of great help.

    i have accepted the input from text boxes, using getText method. i have then converted the string into integer and double as per the requirement. i am actually, stuck up at this point, for i am unable to use this input to draw shapes on the jpanel which i am using as a canvas ,embedded in mypaint page.

    to be more clear,

    the user has typed, x1 = 0. x2 = 0, x3 =200, x4 =200.(say). i want to use x1,x2,x3,x4 in drawLine method .As you can see in the code that i have attached above, i.e., "MyPaint", the class extended from jpanel(i.e., my canvas), is an innerclass i the jframe. this innerclass has overridden paintComponent() in which are present drawline, etc. my doubt is, how should i pass the values x1,x2,x3,x4, which will differ according to the user input, to this, drawLine() method.

  7. #7
    phanhuuhoan is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 08-28-2009, 06:50 PM
  2. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 05:01 AM
  3. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 AM

Posting Permissions

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