Results 1 to 19 of 19
  1. #1
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Question How to transfer my code to NetBeans IDE

    Peace be unto you.

    I have my working code, typed in notepad, now i want to have the same function in my NetBean's Swing GUI. I tried a lot and its giving me funny errors.


    Java Code:
    // it draw circle when user click mouse
    
        import java.awt.Color;
        import java.awt.Container;
        import java.awt.Dimension;
        import java.awt.Graphics;
        import java.awt.Point;
        import java.awt.event.MouseEvent;
        import java.awt.event.MouseListener;
        import java.util.ArrayList;
        import javax.swing.JComponent;
        import javax.swing.JFrame;
    
        public class ClickComponent extends JComponent implements MouseListener {
    
           private ArrayList<Point> centers;
    
           public ClickComponent() {
               centers = new ArrayList<Point>();
               addMouseListener(this);
               setPreferredSize(new Dimension(150, 150));
           }
    
           public void paintComponent(Graphics g) {
               g.setColor(Color.WHITE);
               g.fillRect(0, 0, getWidth(), getHeight());
               g.setColor(new Color(225, 0, 0));
               for(int i = 0; i < centers.size(); i++) {
                   Point p = centers.get(i);
                   int px = (int) Math.round(p.getX());
                   int py = (int) Math.round(p.getY());
                   g.fillOval(px - 10, py - 10, 20, 20);
               }
           }
    
           public void mousePressed(MouseEvent e) {
               centers.add(new Point(e.getX(), e.getY()));
               repaint();
           }
    
           public void mouseReleased(MouseEvent e) { }
           public void mouseEntered(MouseEvent e) { }
           public void mouseExited(MouseEvent e) { }
           public void mouseClicked(MouseEvent e) { }
    
           public static void main(String[] args) {
               JFrame frame = new JFrame("Click Component");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               Container contents = frame.getContentPane();
               contents.add(new ClickComponent());
               frame.pack();
               frame.setVisible(true);
           }
       }

    I am completly new to IDE programming. Thanks for your help
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

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

    Default

    I tried a lot and its giving me funny errors.
    We don't have much to go on here since "funny errors" tells us nothing. What precise errors are you seeing?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    And how did you added the code into NetBeans? Those information are must, otherwise it's difficult to comment on your question.

  4. #4
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    I see some issues with the code...i believe your paint component method may not work right. usually you have to obtain an instance of graphics g.getGraphics() ....

  5. #5
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    i am wrong i just checked the API

  6. #6
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    OKay... I compiled your code independantly and it works....then i moved it over to netbeans and IT WORKS...But..you have to divide your programe into modules and all of the modules will be found because they are packaged... I am going to show you how to divide the program, or how I did it...if you want the project file I can email it to you once the site allows me to give my email address... meanwhile I will post up the classes

  7. #7
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    I named the project "component and it has 3 classes in 3 seperate files....
    The first class is your ClickComponent class and this is how it looks

    Java Code:
    
    package component;
    
    /**
     *
     * @author Aaron
     */
    
    import java.awt.Color;
       
        import java.awt.Dimension;
        import java.awt.Graphics;
        import java.awt.Point;
        import java.awt.event.MouseEvent;
        import java.awt.event.MouseListener;
        import java.util.ArrayList;
        import javax.swing.JComponent;
        
    
    public class ClickComponent extends JComponent implements MouseListener{
    
      private ArrayList<Point> centers;
    
           public ClickComponent() {
               centers = new ArrayList<Point>();
               addMouseListener(this);
               setPreferredSize(new Dimension(150, 150));
           }
    
           public void paintComponent(Graphics g) {
               g.setColor(Color.WHITE);
               g.fillRect(0, 0, getWidth(), getHeight());
               g.setColor(new Color(225, 0, 0));
               for(int i = 0; i < centers.size(); i++) {
                   Point p = centers.get(i);
                   int px = (int) Math.round(p.getX());
                   int py = (int) Math.round(p.getY());
                   g.fillOval(px - 10, py - 10, 20, 20);
               }
           }
    
           public void mousePressed(MouseEvent e) {
               centers.add(new Point(e.getX(), e.getY()));
               repaint();
           }
    
           public void mouseReleased(MouseEvent e) { }
           public void mouseEntered(MouseEvent e) { }
           public void mouseExited(MouseEvent e) { }
           public void mouseClicked(MouseEvent e) { }
    }

  8. #8
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    the second class I made is frameBuild.java and it looks like this

    Java Code:
     
    package component;
    import javax.swing.JFrame;
     import java.awt.Container;
    /**
     *
     * @author Aaron
     */
    public class frameBuild {ClickComponent c;
        
        frameBuild(){ c= new ClickComponent();
              JFrame frame = new JFrame("Click Component");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               Container contents = frame.getContentPane();
               contents.add(c);
               frame.pack();
               frame.setVisible(true);}
    
    }
    It is important to see here that when I called on the ClickComponent object I didn't have to import it because it is a class within the project so the compiler knows where to find it
    Last edited by aaroncarpet; 11-13-2009 at 11:48 PM. Reason: showing how the object can be called from a different file without importing

  9. #9
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    And finally here is the Main class which is the first one the ide gives you notice the constructor i made

    Java Code:
    package component;
    
    
    /**
     *
     * @author Aaron
     */
    public class Main { frameBuild f;
    Main(){ f = new frameBuild();}//this builds the frame
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Main m = new Main();//this tells the Main to build the frame
              
            // TODO code application logic here
        }
    
    }

  10. #10
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    If you do it like the above way you have to go to file add new file and add a java class
    try this out and it should work

  11. #11
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    Thank AARON,

    I'll give a try and get back to you asap.


    cheers
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  12. #12
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    when your classes are all in different files on the ide , as long as they are in the same project you don't have to import them, that is the beauty of the package

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Hi aaroncarpet, I really appreciate your support given to the original poster. But it's much better if you can guide him to the correct way, rather posting the code you've workout. Anyway thanks for the support given in our community. :)

  14. #14
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    Am right now on the go, trying what Aaron posted. And i gues its a good idea for beginners lyk me to have response lyk that one.

    Am gr8ful to all of you.

    Will let you know my progress ...

    Cheers
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  15. #15
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Red face on the go ...

    sorry guys am still trying ...
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  16. #16
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    Is like all are pure Classes, no JFrame Form.
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  17. #17
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    I understand from the code above, i need to create Blank project, then the classes...
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  18. #18
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    Compiled successfully. but when i run it says Main Class is not found ...what should i do now?


    Thanks
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  19. #19
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Unzip this folder and move it into your netbeans projects folder and then open netbeans and it will show the component project compile it and run it and then use the file tree on the left to look at all the classes
    Attached Files Attached Files

Similar Threads

  1. Replies: 9
    Last Post: 12-21-2010, 02:37 PM
  2. [SOLVED] Turn off NetBeans code blocker
    By xcallmejudasx in forum New To Java
    Replies: 5
    Last Post: 05-04-2009, 05:07 PM
  3. tcp file transfer
    By ddj in forum Networking
    Replies: 2
    Last Post: 03-20-2009, 02:10 PM
  4. whats the code that we need to connect oracle10g with the netbeans
    By katko0ota in forum Enterprise JavaBeans (EJB)
    Replies: 6
    Last Post: 07-15-2008, 05:52 PM
  5. Replies: 9
    Last Post: 12-18-2007, 09:02 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
  •