Results 1 to 2 of 2
  1. #1
    diRisig is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default New: Want to understand Drawing...

    Hello,

    I'm trying to get into java coming from C# and C++. And I want to understand paint() events of awt components.

    I have a jFrame which has a Canvas and a jButton. I've put this code in the button's clicked event method but i don't get anything drawn on the canvas. How do I draw to a canvas? please

    Java Code:
            Graphics g = canvas1.getGraphics();
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, canvas1.WIDTH, canvas1.HEIGHT);

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    In Swing use a (non-opaque) JComponent or a(n opaque) JPanel and override the paintComponent method for custom graphics. Although you can use an AWT/heavyweight Canvas you may have problems later see Mixing heavy and light components for more.
    The general idea for custom drawing is to set up the paintComponent method to be ready to draw its enclosing classes state at any time and to adjust/change the state from within event code.
    See the JComponent paint method for perspective about the various painting methods and how they relate. You can override paint and the other two but it is generally for more advanced work.
    Lesson: Performing Custom Painting has basic concepts.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class Drawing extends JPanel implements ActionListener {
        Random seed = new Random();
        Color color = Color.red;
    
        public void actionPerformed(ActionEvent e) {
            color = getColor();
            repaint();
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int cx = getWidth()/2;
            int cy = getHeight()/2;
            int d = 75;
            g2.setPaint(color);
            g2.fillOval(cx-d/2, cy-d/2, 75, 75);
        }
    
        private Color getColor() {
            return new Color(seed.nextInt(0xffffff));
        }
    
        private JPanel getUIPanel() {
            JButton button = new JButton("change");
            button.addActionListener(this);
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            Drawing test = new Drawing();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getUIPanel(), "Last");
            f.setSize(400,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

Similar Threads

  1. help me need to understand queries
    By hossainsadd in forum JDBC
    Replies: 1
    Last Post: 05-26-2008, 01:02 AM
  2. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-02-2008, 12:55 AM
  3. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM
  4. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 11:03 AM
  5. i can't understand using interface as a type
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 11:07 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
  •