rectangles in Swing JPanel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aleplgr
    New Member
    • Aug 2007
    • 19

    rectangles in Swing JPanel

    Hi! I'm trying to draw some filled rectangles in a JPanel, should I define a paintComponent like this?
    public void paintComponent( Graphics g) {
    super.paintComp onent(g);
    g.setColor(Colo r.blue);
    g.drawRect(10, 10, 80, 30);
    g.drawRoundRect (100, 10, 80, 30, 15, 15);

    Or is there any other way to do it? maybe using some Swing component, for example the JTextField is like a rectangle and I could change the background color and if I could set it as a read-only component where the user could not write,that could work as a rectangle..
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by aleplgr
    Hi! I'm trying to draw some filled rectangles in a JPanel, should I define a paintComponent like this?
    public void paintComponent( Graphics g) {
    super.paintComp onent(g);
    g.setColor(Colo r.blue);
    g.drawRect(10, 10, 80, 30);
    g.drawRoundRect (100, 10, 80, 30, 15, 15);

    Or is there any other way to do it? maybe using some Swing component, for example the JTextField is like a rectangle and I could change the background color and if I could set it as a read-only component where the user could not write,that could work as a rectangle..
    Using the Graphics object is the normal way of doing it. There are also some g.fillXX methods there.
    Is there anything wrong with using the Graphics object?

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      r035198x is right. Just use the graphics object.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by aleplgr
        Hi! I'm trying to draw some filled rectangles in a JPanel, should I define a paintComponent like this?
        public void paintComponent( Graphics g) {
        super.paintComp onent(g);
        g.setColor(Colo r.blue);
        g.drawRect(10, 10, 80, 30);
        g.drawRoundRect (100, 10, 80, 30, 15, 15);

        Or is there any other way to do it? maybe using some Swing component, for example the JTextField is like a rectangle and I could change the background color and if I could set it as a read-only component where the user could not write,that could work as a rectangle..
        You should describe what you are trying to do. What is your goal?

        Comment

        Working...