Results 1 to 17 of 17
Like Tree4Likes
  • 1 Post By JosAH
  • 1 Post By Norm
  • 1 Post By pbrockway2
  • 1 Post By Fubarable

Thread: Trying to place my graphics at the center of a panel.

  1. #1
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Trying to place my graphics at the center of a panel.

    Good day.

    I'm actually not very new to Java, but I'm not extremely advanced either. Been out of touch with it for almost 2 years.

    Anyway, to get straight to the point, I'm having problems finding a way on how to put my graphics (circle) at the center of my panel.

    Java Code:
    package yr11.m12.a;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GsGui {
    
       private CdPanel cdPanel;
       private int height = 50;
       private int width = 50;
    
       public void go() {
          JFrame newFrame = new JFrame("Frame");
          newFrame.setLayout(new BorderLayout());
          JButton eastButton = new JButton("Click me to grow circle!");
          newFrame.add(eastButton, BorderLayout.EAST);
    
          cdPanel = new CdPanel();
          newFrame.add(cdPanel, BorderLayout.CENTER);
    
          JButton westButton = new JButton("Click me to shrink circle!");
          newFrame.add(westButton, BorderLayout.WEST);
    
          ActionListener doGrow = new Grow();
          eastButton.addActionListener(doGrow);
    
          newFrame.add(cdPanel, BorderLayout.CENTER);
          System.out.println("Width: " + cdPanel.getWidth());
          System.out.println("Height: " + cdPanel.getHeight());
    
          newFrame.getContentPane().setPreferredSize(new Dimension(500, 500));
          newFrame.pack();
    
          newFrame.setVisible(true);
          newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    
       private class CdPanel extends JPanel {
          public void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.setColor(Color.BLUE);
             g.fillOval(55, 225, height, width);
          }
       }
    
       private class Grow implements ActionListener {
    
          @Override
          public void actionPerformed(ActionEvent arg0) {
             if (width < 500) {
                cdPanel.repaint(55, 225, width += 2, height += 2);
                System.out.println(width);
             } else {
             }
          }
       }
    
       public static void main(String[] args) {
          GsGui gsGui = new GsGui();
          gsGui.go();
       }
    }
    This is my code. I have bold the part where I am confused.

    I am tasked to adjust the coordinates to ensure that the circle is at the center. However, there is one problem that I'm facing.

    I am using a Borderlayout. So this panel is placed at the center, which I have completely no idea what the size is. As of now, it looks somewhat center, but I am not 100% sure, and I have asked my lecturer, and she mentioned that there is a way to place it in the center portion. Can anyone enlighten me on how I am suppose to go about doing it? I don't need the codes, I just want some place where I can start to crack my brain at. Thank you!
    Last edited by Fubarable; 12-07-2011 at 09:45 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Trying to place my graphics at the center of a panel.

    This is a paper and pencil exercise.
    Draw some concentric circles on a piece of paper and mark the x,y location where each circle should be drawn. Look at the radius of the circle and the locations of the bounds for where the circle is to be drawn.

  3. #3
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Quote Originally Posted by Norm View Post
    This is a paper and pencil exercise.
    Draw some concentric circles on a piece of paper and mark the x,y location where each circle should be drawn. Look at the radius of the circle and the locations of the bounds for where the circle is to be drawn.
    Hello. Thank you for the quick reply. I did ask my teacher about this. I told her if I am supposed to just slowly adjust the coordinates 1 by 1 until it is definitely center, but she told me no, that there is a way to ensure that it is center. Which is why I am confused.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trying to place my graphics at the center of a panel.

    A circle has a bounding rectangle (a square actually) with sides 2*r where r is the radius of the circle. If (w, h) are the width and height of the panel, the top left of the bounding square is at (w/2-r,h/2-r).

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Quote Originally Posted by JosAH View Post
    A circle has a bounding rectangle (a square actually) with sides 2*r where r is the radius of the circle. If (w, h) are the width and height of the panel, the top left of the bounding square is at (w/2-r,h/2-r).

    kind regards,

    Jos
    Hello. Thanks for your amazingly quick reply too.

    I understand about the w and h of the panel, and what you mean.

    But right now, my main problem is that I am unable to find the size of the center portion for my Borderlayout. If it is 500/3, it'll give me 166.666666... So am I supposed to use this number as the width of my panel?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Trying to place my graphics at the center of a panel.

    Components have methods you can call to get their postions and sizes.
    Look in the API doc for the many choices. Try several (print out what they return) to see which ones give you the data you are looking for.
    Fubarable likes this.

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

    Default Re: Trying to place my graphics at the center of a panel.

    A couple of suggestions:

    1) You actually do know the size of the CircleDrawPanel at any time, including and especially when it draws the circle by simply calling getWidth() and getHeight() on this object. I suggest that you call these two methods inside of the CircleDrawPanel's paintComponent() method, and then use the results and the circle's height and width to calculate the best x and y values for the first two parameters of your g.fillOval(x, y,...) method. This will involve nothing more than simple algebra.

    2) You've got your width and height parameters mixed up as the last two parameters in the g.fillOval(...) method. This won't matter now, since they are one and the same, but it will matter in the future when they are different.

    3) For simplicity's sake, I'd change your actionPerformed method and have it draw the entire CircleDrawPanel. You'll need to first increment height and width, and then call the parameterless version of repaint on the panel.

    edit: oops, I didn't see Norm's post which is better than mine in that it tries to gently lead you to a solution rather than give it to you. Still I have left somethings for you to do and calculate.

  8. #8
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Quote Originally Posted by Fubarable View Post
    A couple of suggestions:

    1) You actually do know the size of the CircleDrawPanel at any time, including and especially when it draws the circle by simply calling getWidth() and getHeight() on this object. I suggest that you call these two methods inside of the CircleDrawPanel's paintComponent() method, and then use the results and the circle's height and width to calculate the best x and y values for the first two parameters of your g.fillOval(x, y,...) method. This will involve nothing more than simple algebra.

    2) You've got your width and height parameters mixed up as the last two parameters in the g.fillOval(...) method. This won't matter now, since they are one and the same, but it will matter in the future when they are different.

    3) For simplicity's sake, I'd change your actionPerformed method and have it draw the entire CircleDrawPanel. You'll need to first increment height and width, and then call the parameterless version of repaint on the panel.

    edit: oops, I didn't see Norm's post which is better than mine in that it tries to gently lead you to a solution rather than give it to you. Still I have left somethings for you to do and calculate.
    EXACTLY what I was looking for. I completely forgotten you can call getWidth() and getHeight() within the JPanel to find it's width and height. Thank you! I'll work on it now!

  9. #9
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Can a moderator please help me delete this thread?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Trying to place my graphics at the center of a panel.

    Personally I think it would be much better for the thread to stay. You've given it a good title, and the solution is something I'm sure someone else will have forgotten about.

    If you have concerns about help you get contravening homework conditions (which I'm guessing might be the problem) then the thing to do is state that upfront. I think you did that (in this thread and in the other about the shrinking circle). People here are generally good about not spoonfeeding. In any case it can't change the facts to destroy the evidence...

    The other place that a problem may arise is that, to explain your problem you post code, and you might feel that others doing the assignment might benefit unfairly from that. That's a (yet another) reason for considering the SSCCE beneficial. The best code would contain nothing much from your assignment, just a circle/dot/whatever placed in a frame. Of course others may still benefit to the extent that they have also forgotten about the panel methods, but if they follow the discussion it's quite reasonable that they benefit from it.
    DarrylBurke likes this.

  11. #11
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Hi pbrockway2.

    The reason for my wish to delete this thread is because of the 2nd reason.

    Initially when I created this thread, and after the thread was approved, I tried to google search the class name and it didn't show up, so I thought I was pretty safe. But upon trying it again today, I realise that google does indeed show this thread, and I'm afraid of others copying my code. I would love for my thread to stay, but I'm afraid it is spotted by other class mate who would copy entirely my code.

    If I can edit my codes, it would all be okay. Thank you!

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

    Default Re: Trying to place my graphics at the center of a panel.

    I agree that this thread should not be removed, deleted or edited as this goes against the spirit of this forum.
    DarrylBurke likes this.

  13. #13
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Okay, I understand about not wanting it to be deleted, as I'm sure there are few others who probably face the same question as me. But I do sincerely hope that I can edit out some of my codes as this are my codes and anyone can easily search the class name (Which is the same as the name given from our lecturer) to find this thread, and my code. I wish only to edit out the class name and remove some of the methods that would make no difference to the question please.

    All I'm asking is to change the class's name and delete some of the method's code. Is that really too much to ask? I'm at risk of someone copying my code and being charged for plagiarism here, and I'm panicking.
    Last edited by rhexis; 12-07-2011 at 03:55 PM.

  14. #14
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    Some replies would be really nice....All that I am asking is for me to change my class name and method name so it isn't so easy for people to find it just by googling the class name... Is really the spirit of the forum so important that a simple request like this can't be done? This mistake could cost me my degree.

    I've been on the forum for the past 11 hours, and I'll really appreciate if a moderator can give me a reply..
    Last edited by rhexis; 12-07-2011 at 06:54 PM.

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

    Default Re: Trying to place my graphics at the center of a panel.

    The instructors will likely know if someone borrowed this code or not especially if you EMail them immediately with your code and a reference to this site, I wouldn't worry so much about that, but one critical issue for us is that you have received help from us regarding a coding question, and complete evidence of this must remain visible online including your code, if your instructors need to know that this help has been obtained.

  16. #16
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    I am not deleting any evidence from my post. All I am asking for is to be allowed to at least change my class name and my method name so it will not be so easy to find out about this thread, as well as take out methods that are not related to the question that I am asking.

    You are wrong. They will not know and they will take it as cheating. If someone copies my work and sends it in, both of us will be punished, not only the person who has copied it. This is a worrying factor for me because I am at my final year and I cannot afford to fail.

    I hope you guys can understand because this is really a big problem. I am not hiding anything, and I am very appreciative of all your help. I am asking just a very small favour to change the class name and method name so it is not easily found, that is all.

    I made a mistake by not changing the name of my class and the method name before submitting it to the forum. Surely I should at least be allowed to change that? I am not one bit hiding the any evidence. Is that even too much to ask? Just editing the name of the class and method? It's not as if I'm hiding anything. I am begging you guys. Just let me edit my class name and my method names....Is that really even too much? What am I hiding? I admit it is my fault that I failed to change it before submitting, but I didn't expect google to be able to find it.. Please... I've been on for 14 hours trying to get an answer.... And I've never been this desperate before.

    Fubarable, please read your PM. Thank you.
    Last edited by rhexis; 12-07-2011 at 08:32 PM.

  17. #17
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Trying to place my graphics at the center of a panel.

    The problem here is not whether or not my lecturer finds out that I received help from outside, but more of how students might find this thread simply by googling the class name given by the assignment, which in this case is the same as my class name.

    Any student who finds this thread will and my other thread will practically have completed 70%-80% of this assignment, and I would be charged for plagiarism, even though I am the victim, and I will be given an immediate failure for this subject.

    Please, all I am asking is to change the class name and method name, and hide out some methods that are completely irrelevant to my question. I am not at all hiding the fact that I received help, I am not hiding a single evidence.

    It was my mistake that I failed to change the class name before submitting the thread, because I didn't think it would be possible for google to be able to search coded text. When I realised about this, I had already submitted my 2nd thread and question, and was unable to edit it already.

    Please, please help. I am sincerely desperate and have been seeking help for the past 14 hours.
    Last edited by rhexis; 12-07-2011 at 08:46 PM.

Similar Threads

  1. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 09:29 PM
  2. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 09:43 PM
  3. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 06:56 AM
  4. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 12:21 AM
  5. center a form
    By tommy in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:47 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
  •