Results 1 to 8 of 8
  1. #1
    SM2010 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default Possible to do JPanel drawing without extending?

    Hi - noob question but I've just read a few authoritative-sounding posts on the net saying how extending JFrame is a bad idea, design-wise, if all you're trying to do is create a frame.

    Does the same apply to JPanel? If all I want to do is set up a panel and do some drawing on it, don't I just need an instance of a JPanel rather than a subclass, if you know what I mean?

    All the examples I've seen of JP drawing extend it, then override paintComponent(). Is that possible using composition, rather than inheritance?

    thanks in advance.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I don't think that its possible through composition without subclassing anyway.

    In anycase, overriding the paintComponent method is the standard way of doing it, and I don't think its that inefficient.
    If you only want to override one instance of a JPanel you can override the paintComponent Method like this:

    Java Code:
    JPanel myPanel = new JPanel () {
        public void paintComponent(Graphics g) {
            // Your code here
        }
    }
    This JPanel can be put into a JFrame like any other component.

    Hope this helps
    Berkeleybross

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by berkeleybross View Post
    I don't think that its possible through composition without subclassing anyway.

    In anycase, overriding the paintComponent method is the standard way of doing it, and I don't think its that inefficient.
    If you only want to override one instance of a JPanel you can override the paintComponent Method like this:

    Java Code:
    JPanel myPanel = new JPanel () {
        public void paintComponent(Graphics g) {
            // Your code here
        }
    }
    This JPanel can be put into a JFrame like any other component.
    Of course you can take this a bit further: a delegating panel that delegates all its painting operations to a Painter:

    Java Code:
    public interface Painter {
       public void pain(Component c, Graphics c);
    }
    and the delegating panel looks like this:

    Java Code:
    public class DelegatingPanel extends JPanel {
       private Painter painter; 
    
       public DelegatingPainter(Painter painter) {
          super();
          this.painter= painter;
       }
    
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (painter != null)
             painter.paint(this, g);
       }
    }
    An external implementation of a Painter does all the painting work and you can stick in whatever you want for that Painter.

    kind regards,

    Jos

  4. #4
    SM2010 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks BB.

    I think the general 'rule' seems to be if you're over-riding method(s), like you would with JPanel, then inheritance is okay, otherwise composition. Makes sense from a design pov I guess. :)

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

    Default

    Quote Originally Posted by JosAH View Post
    O
    Java Code:
    public interface Painter {
       public void [color="red"]pain[/color](Component c, Graphics c);
    }
    You really don't like GUI graphics, do you Jos?

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

    Default

    And yes, you can draw on JPanels without subclassing them, a technique that I believe is called active (as opposed to passive) graphics. I'm no pro at this, but I've seen it used in full-screen apps and sometimes in game programming, but it's not the preferred way to do most routine drawing.

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

    Default

    Quote Originally Posted by Fubarable View Post
    You really don't like GUI graphics, do you Jos?
    Erm, no, does it show? ;-)

    kind regards,

    Jos

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

    Default

    One way of doing custom painting without subclassing any Swing or AWT class (not that I would recommend it for most purposes) is via ImageIcon and JLabel. Overly simplistic example:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.swing.*;
    
    public class CustomPaintedLabel {
    
      BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
      Graphics graphics = image.getGraphics();
      JLabel label = new JLabel(new ImageIcon(image));
      Random random = new Random();
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new CustomPaintedLabel().makeUI();
          }
        });
      }
    
      public void makeUI() {
        repaintImage();
        JButton button = new JButton("Repaint");
        button.addActionListener(new ActionListener() {
    
          @Override
          public void actionPerformed(ActionEvent e) {
            repaintImage();
          }
        });
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(label, BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      private void repaintImage() {
        graphics.setColor(new Color(random.nextInt(0xFFFFFF)));
        graphics.fillOval(2, 2, 196, 196);
        graphics.setColor(new Color(random.nextInt(0xFFFFFF)));
        graphics.fillRect(50, 50, 100, 100);
        label.repaint();
      }
    }
    db

Similar Threads

  1. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 05:46 AM
  2. Drawing with JPanel
    By m00nchile in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 09:12 PM
  3. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 08:42 AM
  4. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 04:47 PM
  5. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 11:47 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
  •