Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Ah yes, as Fubarable said, your mistake is calling "getGraphics()". Every component has its own Graphics object. Setting the color of the Graphics object of the frame is not going to set it on the component's Graphics object. You need to have a Color variable inside ColorScribble and a setter method to set the color to be used to call "g.setColor(color)".

  2. #22
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Please pay attention to Darryl.Burke and Fubarable. I'm not even going to tell you why, because if I do you might continue to hack your design, but you have a serious memory leak as a result of failure to follow their advice. If you don't believe me, bring up a memory monitor and interact with your program for a while.

  3. #23
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default

    Ok so I tried making a Jpanel class instead of an Applet class and got this:
    XML Code:
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics; 
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseMotionAdapter; 
    
    public class SwingPaintDemo3 {
        
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    createAndShowGUI(); 
                }
            }
            );
        }
    
        private static void createAndShowGUI() 
        {
            JFrame f = new JFrame("Swing Paint Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.add(new MyPanel());
            f.pack();
            f.setVisible(true);
        } 
    }
    
    class MyPanel extends JPanel {
    
        private int last_x;
        private int last_y;
        private int x;
        private int y;
        private Graphics g;
        
        public MyPanel() 
        {
    
            setBorder(BorderFactory.createLineBorder(Color.black));
            
            addMouseListener(new MouseAdapter() 
            {
                public void mousePressed(MouseEvent e) 
                {
                    last_x = e.getX();
                    last_y = e.getY();
                }
            }
            );
    
            addMouseMotionListener(new MouseAdapter() 
            {
                public void mouseDragged(MouseEvent e) 
                {
                    x = e.getX();
                    y = e.getY();
                }
            }
            );
            
        }
    
        public Dimension getPreferredSize() 
        {
            return new Dimension(250,200);
        }
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);       
            g.drawString("This is my custom Panel!",10,20);
            g.setColor(Color.BLACK);
            g.drawLine(last_x, last_y, x, y);
            last_x = x;
            last_y = y;
        }  
    }
    Except for the fact that the lines are never drawn....

  4. #24
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The problem now, is that nothing is calling 'repaint'. If you add a 'repaint();' call to the end of the mouseDragged method, it will start drawing lines. Of course, it's only ever going to draw 1 line, because each call to super.paintComponent clears the canvas.

  5. #25
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default

    Well if i remove the repaint(); method then the problem is none of the lines are drawn and the only way I know of to call the paintComponent method is with the repaint. ***Again I'm sorry for my stupidity, I'm way over my head with this program, teacher only taught us the basics, but expects a huge program for out final =(

  6. #26
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    God, I hate incompetent teachers :mad:

    Two things:
    -1. Why do you have 2 different MouseAdapters? You can just have one with both mouseDragged and mousePressed overrided....
    -2. You don't remove "repaint()", you add "repaint()" at the end of both those methods.

  7. #27
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    You *do* need two listeners, because one is a MouseListener and the other is a MouseMotionListener.

    I'm assuming that what you are trying to do is the following:
    - when the mouse is clicked, it anchors a new line on the screen
    - as you drag the mouse, a line is drawn bewteen the most recent anchor and the current mouse position
    - when you release the mouse, the line becomes permanent, and the next time you press the mouse it starts a new line

    Is that right? Is so, here's what you need to do:
    1. Add a call to repaint in your mouseDragged method as previously stated
    2. in paintComponent, remove the lines where you are setting last_x = x and last_y = y.
    3. In your MouseListener, implement the mouseReleased method. When the mouse is released, you will store x, y, last_x, and last_y into some kind of List.
    4. In your paintComponent, paint the entire list of lines every time the method is drawn, in addition to the line that the user is presently defining (if any)

  8. #28
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @toadaly
    Woops, I didn't see the different method names, my bad :D

  9. #29
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    You *do* need two listeners, because one is a MouseListener and the other is a MouseMotionListener.
    No you don't *need* two MouseAdapters. You can construct a single MouseAdapter assigned to a variable reference, and add that both as a MouseListener and as a MouseMotionListener.

    Modified from the OP's code:
    Java Code:
    MouseAdapter listener = new MouseAdapter() {
    
      public void mousePressed(MouseEvent e) {
        last_x = e.getX();
        last_y = e.getY();
      }
      
      public void mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();
      }
    };
    
    addMouseListener(listener);
    addMouseMotionListener(listener);
    Keeps related code closer together and is IMO more maintainable than two separate, anonymous listeners.

    Just my 0.02, db

  10. #30
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @Darryl.Burke
    And less memory used by an extra object :P ;)

  11. #31
    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 ra4king View Post
    @Darryl.Burke
    And less memory used by an extra object :P ;)
    The memory issue is trivial, but the fact that both listeners share the same state is key.

  12. #32
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default

    Ok so I got the class with JPanel to work, and actually got the 2 programs to work together, the frame with 2 sections: bottom being the JPanel with all the drawing instructions and the top half with the actual ComboBox, and I added a getColor method to ComboBox program and I accessed that method in the paintComponent and I got the nullPointerException again
    Java Code:
    protected void paintComponent(Graphics g)
        {
            ColorBox test = new ColorBox();
            g.setColor(test.getColor());
            g.drawString("This is my custom Panel!",10,20);
            g.drawLine(last_x, last_y, x, y);
            last_x = x;
            last_y = y;
        }
    Here is the setter method:
    Java Code:
        public void getColor() 
        {
    	System.out.println(color);
    	g.setColor(color);
        }
    The color variable is still considered null. Am I suppose to make both programs into one or is there still something I'm missing?

  13. #33
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If the method you're calling a 'setter method' is a member of ColorBox, then I don't see how your code even compiles, because you're passing a void into g.setColor.

    My suspicion is that there actually is a 'getColor' method in your ColorBox class that returns null.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Need Help with NullPointerException Error
    By waterisgood5 in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 08:27 PM
  2. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 03:31 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. awt TextField nullPointerException error
    By k2k in forum AWT / Swing
    Replies: 3
    Last Post: 02-24-2009, 05:24 AM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 07:54 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
  •