Results 1 to 8 of 8
  1. #1
    ManyProblems is offline Hello
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Repainting rectangles never work for me.

    I started a project with the simple JPanel, which apparently is not as simple as it first seemed :(. My goal was to have the rectangle grow one size larger upon clicking it. However, when I click it my MouseListener seems to work, only my repaint() doesn't.

    I'm not sure what to do, because this is my first time using repaint and I don't think I'm using it correctly.
    One thing I'm not sure about calling the repaint method twice. On the java tutorials, it said the first time ws to fill the current square with the background color, the second to recreate it.


    My Code:


    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    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;


    public class getBigger {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    private static void createAndShowGUI() {

    JFrame f = new JFrame("Disappearing Ball");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);
    }
    }

    class MyPanel extends JPanel {



    public MyPanel() {

    addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    System.out.println("it works");



    repaint(50,50,80,80);


    repaint(50,50,20,20);
    }
    });





    }

    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawString("Click to create a rectangle!",10,20);
    g.setColor(Color.GREEN);
    g.fillRect(50,50,80,80);
    g.setColor(Color.BLACK);
    g.drawRect(50,50,80,80);
    }

    public Dimension getPreferredSize() {
    return new Dimension(250,200);
    }


    }




    If you don't mind telling me where I went wrong.

    Thanks

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

    Default Re: Repainting rectangles never work for me.

    I'm sorry, I'm confused. Where in the code above do you increase the size of your rectangle?

    Edit: I see that this is your second thread in the forum, and that you never replied to help given to you in your first thread. Please note that if you want to encourage folks to take the time to try to help you, it isn't asking too much of you to take a little time to acknowledge that help, is it? I suggest that you reply to all comments in your previous thread. It's usually best to reply to previous threads before starting new threads.
    Last edited by Fubarable; 07-30-2012 at 05:52 AM.

  3. #3
    ManyProblems is offline Hello
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Repainting rectangles never work for me.

    I'm pretty sure it's here:


    repaint(50,50,20,20);



    And by the way, thanks for the advice. I'm pretty new to forums.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,192
    Rep Power
    19

    Default Re: Repainting rectangles never work for me.

    Here's some more advice, for the second time:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Repainting rectangles never work for me.

    Quote Originally Posted by ManyProblems View Post
    I'm pretty sure it's here:

    repaint(50,50,20,20);
    I was wondering if you were thinking this. No, all this does is redraw a portion of the component, the portion that starts at x = 50, y = 50, with a width and height of 20. It does not change any of the images being drawn. To do that you need to change the numbers that you have here:

    Java Code:
    g.setColor(Color.GREEN);
    g.fillRect(50,50,80,80);
    g.setColor(Color.BLACK);
    g.drawRect(50,50,80,80);
    The best way to do that is not to use literal numbers in the code above, but rather class int fields, since these can be changed. Then if you want to change the location of the drawn rectangle, you change the values held by the fields and call repaint().

    And by the way, thanks for the advice. I'm pretty new to forums.
    You're welcome!

  6. #6
    ManyProblems is offline Hello
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Repainting rectangles never work for me.

    Ok, I think I get it.

    Plus, an anonymous person re-wrote a full code that works.

    So thanks, everyone.

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

    Default Re: Repainting rectangles never work for me.

    Quote Originally Posted by ManyProblems View Post
    Plus, an anonymous person re-wrote a full code that works.
    Where?

  8. #8
    ManyProblems is offline Hello
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Repainting rectangles never work for me.

    Quote Originally Posted by Fubarable View Post
    Where?
    private messages

Similar Threads

  1. Problems repainting
    By stuffses in forum AWT / Swing
    Replies: 4
    Last Post: 04-10-2012, 05:44 PM
  2. Repainting on JPanel
    By sunde887 in forum AWT / Swing
    Replies: 8
    Last Post: 09-20-2011, 03:10 PM
  3. Applet not repainting?
    By mjgood91 in forum Java Applets
    Replies: 4
    Last Post: 01-18-2011, 02:27 AM
  4. repainting more efficiently
    By imorio in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2010, 04:24 AM
  5. Repainting From Another Class
    By habester in forum New To Java
    Replies: 1
    Last Post: 11-13-2009, 02:29 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
  •