Results 1 to 8 of 8
  1. #1
    nickrrr is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default 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
    Java Code:
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JavaPaintUI UI = new JavaPaintUI();
            UI.setVisible(true);
        }
    
    }
    my JavaPaintUI class (minus all the generated stuff from netbeans)
    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);
        }
    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.

    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.

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

    Default

    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.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    See Trail: 2D Graphics (The Java™ Tutorials) Best of luck and post back if you have problems.

  4. #4
    nickrrr is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    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)

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

    Default

    Quote Originally Posted by nickrrr View Post
    Thanks guys, reading all of the links and tutorials. However i'm still lost (java is frustrating!),
    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.

    ... 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?
    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.

    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.
    There are many examples of this and similar programs in this forum. You might want to search for them and look them over.

    Luck!

  6. #6
    nickrrr is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    So i feel like i'm kinda understanding it but confused with how to integrate it to my generated code.

    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);
            }
        }
    }
    I have a feeling this is terribly wrong but i'm not sure where to continue.
    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

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    How can i use paintComponent on a already made jpanel (as did by netbeans)?
    Easy way: ditch the NetBeans visual designer, which is not a beginners' tool, and learn to hand-code your GUIs.
    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

  8. #8
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by nickrrr View Post
    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.
    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

  1. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  2. Grid Painting?
    By iCon09 in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 07:04 AM
  3. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  4. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  5. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM

Posting Permissions

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