Results 1 to 7 of 7
- 12-20-2008, 12:18 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
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.
-
If you're going to post some code, you might as well post all so that this code will compile and run.
- 12-21-2008, 12:11 AM #3Java 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(); } }
- 12-21-2008, 05:55 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
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.
- 12-21-2008, 06:11 PM #5Java Code:
jname = new JLabel("MY PAINT APPLICATION",JLabel.CENTER);
Java Code:buttonpanel.add(jarc);
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
- 12-21-2008, 07:21 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
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.
- 07-08-2009, 12:06 PM #7
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
How to draw in a JPanel using Netbeans 6.1 GUI maker
By Gatts79 in forum New To JavaReplies: 4Last Post: 08-28-2009, 07:50 PM -
how to draw x-y graph in Jpanel.--not in APPLET.
By vincent2001@gmail.com in forum New To JavaReplies: 2Last Post: 08-24-2008, 06:01 AM -
Draw on JPanel, Help
By carl in forum Java 2DReplies: 1Last Post: 07-31-2007, 07:56 AM
Bookmarks