Results 1 to 8 of 8
Thread: Painting in jPanels
- 05-24-2011, 02:19 AM #1
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Painting in jPanels
Hello, i'm working on a computer science project and was looking for some help on some problems ive ran into. My goal is to make a very basic paint app using swing for a gui (just started using netbeans).
My Main class
my JavaPaintUI class (minus all the generated stuff from netbeans)Java Code:public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { JavaPaintUI UI = new JavaPaintUI(); UI.setVisible(true); } }
so i have got this far to make it store 2 sets of coordinates for me to draw a line between yet i'm unsure of how to actually draw inside jpanel2. Is using two points and drawing a line between an effective way to draw or should i just store the current position and draw a circle in its place. I'm used to graphics2d and drawing yet am confused on how to structure it like this.Java Code:package javapaint; import java.awt.*; public class JavaPaintUI extends javax.swing.JFrame { int currentX, currentY, oldX, oldY; private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) { currentX = evt.getX(); currentY = evt.getY(); oldX = currentX; oldY = currentY; System.out.println(currentX + " " + currentY); } private void jPanel2MousePressed(java.awt.event.MouseEvent evt) { oldX = evt.getX(); oldY = evt.getY(); System.out.println(oldX + " " + oldY); }
can anyone point me in the right direction of what i need to do?
Im making a pen tool and also a line tool, which i plan to make work by calling the coordinates on release of the mouse.
-
You'll want to go to the Oracle Java tutorials and read up on how to do graphics with Java and how to draw in Swing.
- 05-24-2011, 03:53 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
See Trail: 2D Graphics (The Java™ Tutorials) Best of luck and post back if you have problems.
- 05-24-2011, 04:24 AM #4
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Thanks guys, reading all of the links and tutorials. However i'm still lost (java is frustrating!), so i need to create a paint method in the JavaPaintUI class? and in that method i need to call the variables in my events (x/y cord)? i still put repaint inside the event thought?
I'm confused with where to put what, as i want it to draw inside the jpanel, yet the only place i know of to place the method is inside the jframe class or main class.
sorry, your help is greatly appreciated.
(still reading all your links in hopes of an answer)
-
I'm sure that you're smart, but there's no way on earth that you could read all in the tutorials about graphics and digest it in this short period of time. No one is that smart. Keep at it.
The tutorials on Swing will tell you that you should paint in a paintComponent method of a component that derives from JComponent such as a JPanel. Yes, you call repaint() in your event handler once you've set the appropriate class fields.... so i need to create a paint method in the JavaPaintUI class? and in that method i need to call the variables in my events (x/y cord)? i still put repaint inside the event thought?
There are many examples of this and similar programs in this forum. You might want to search for them and look them over.I'm confused with where to put what, as i want it to draw inside the jpanel, yet the only place i know of to place the method is inside the jframe class or main class.
Luck!
- 05-25-2011, 01:36 AM #6
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
So i feel like i'm kinda understanding it but confused with how to integrate it to my generated code.
I have a feeling this is terribly wrong but i'm not sure where to continue.Java Code:package javapaint; import java.awt.*; import javax.swing.*; public class JavaPaintUI extends javax.swing.JFrame { /** Creates new form JavaPaintUI */ public JavaPaintUI() { initComponents(); } private void initComponents() { jPanel2 = new javax.swing.JPanel(); pack(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JavaPaintUI().setVisible(true); } }); } public javax.swing.JPanel jPanel2; class jPanel2 extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("BLAH", 20, 20); g.drawRect(200, 200, 200, 200); } } }
In most examples they create a jpanel by calling a class like that (but theirs makes the jpanel inside it). How can i use paintComponent on a already made jpanel (as did by netbeans)? jPanel2
I just saw a post by you about not using generated gui's until fluent in them and agree. Thinking of restarting, since i don't really get the way it generated it.
Thanks
- 05-25-2011, 08:47 AM #7
Easy way: ditch the NetBeans visual designer, which is not a beginners' tool, and learn to hand-code your GUIs.How can i use paintComponent on a already made jpanel (as did by netbeans)?
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
Tough way: read the extensive NetBeans help. Learn the quirks of the visual designer. Learn the meaning of 'Custom creation code' and many other terms.
db
- 05-25-2011, 01:36 PM #8
Member
- Join Date
- Nov 2010
- Location
- VT, USA
- Posts
- 31
- Rep Power
- 0
Extending/subclassing a JPanel gives you the ability to add to it.
On another note, here's pseudocode for using a different class...
class main {
MyClass foo = new foo(this);
void eventHandler(event e) {
foo.passItAlong(e);
}
}
Edit-add: Or register foo as a listener for events.Last edited by Markgm; 05-25-2011 at 04:02 PM.
Similar Threads
-
Switching JPanels with a button inside one of the JPanels
By Beastly in forum AWT / SwingReplies: 2Last Post: 04-26-2011, 02:50 PM -
Grid Painting?
By iCon09 in forum New To JavaReplies: 2Last Post: 02-26-2011, 07:04 AM -
Painting in SWT
By jionnet in forum SWT / JFaceReplies: 9Last Post: 09-24-2010, 06:52 AM -
Painting
By xael in forum New To JavaReplies: 6Last Post: 09-06-2010, 05:10 AM -
Problems regarding JPanels in JPanels
By ColtonPhillips in forum AWT / SwingReplies: 2Last Post: 07-19-2010, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks