Custom Painting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    Custom Painting

    Hello guys. I was wondering if it is possible to perform custom painting in a GUI window using C++. In Java you can do this by using the following code:
    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;
    
    public class SwingPaintDemo2 {
       
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());
            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 {
    
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.black));
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(250,200);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
    
            // Draw Text
            g.drawRect(120, 100, 125, 50);
        }  
    }
    This would draw a simple rectangle on the screen. So can you do this in C++?
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    C++ has no concept of a GUI. How you handle a GUI and drawing in C++ depends entirely on the GUI API in question.

    Comment

    Working...