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

    Default New: Want to understand Drawing...


    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.fillRect(0, 0, canvas1.WIDTH, canvas1.HEIGHT);

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    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 =;
        public void actionPerformed(ActionEvent e) {
            color = getColor();
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            int cx = getWidth()/2;
            int cy = getHeight()/2;
            int d = 75;
            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");
            JPanel panel = new JPanel();
            return panel;
        public static void main(String[] args) {
            Drawing test = new Drawing();
            JFrame f = new JFrame();
            f.add(test.getUIPanel(), "Last");

Similar Threads

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