-1

I am very new to the Graphics portion of Java. I have created a frame and on it I have added a panel whose color has been set to Green. Now on clicking that panel I want to draw a circle using a test class's object called Mypanel. But it does not happen. Please guide !

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

class Mypanel extends JPanel
{
    @Override
    public void paintComponent(Graphics g)
    {
        g.drawOval(15, 15, 5, 5);
    }        
}
public class algo extends javax.swing.JFrame {

    public algo() {
        initComponents();
        jPanel1.setBackground(Color.GREEN);
    }
    Mypanel p = new Mypanel() ;

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        p.repaint();
    }                                    

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new algo().setVisible(true);
            }
        });
    }

}

If I were to guess I would say that I am not supposed to use the repaint method, but I was told that this was to be used.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • In my opinion you will need repaint when you are changing structure, i.e Oval to square or similarly. In swing most of the time component changes gets reflected automatically. when you make call to setter method of the specific attribute. – Acewin Oct 30 '15 at 17:54
  • 1
    It's impossible to be sure, since you haven't included your `initComponents()` method in your question, but a likely reason is that your `Mypanel` instance has a width of zero and height of zero. You can fix this by overriding [getPreferredSize()](http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#getPreferredSize--) in your Mypanel class. Also, the first line in an overridden paintComponent method should always be `super.paintComponent(g);`. If you don't do this, you will see strange behavior. – VGR Oct 30 '15 at 18:13
  • Based on your code, I believe you are a C# Programmer, right? In Java, only creating a Method with name of your object and the sufix `MouseClicked` doesn't add an event to your object at all. I sugest you to study more about Java, Events, Naming convetions before and then rewrite your code =) – Gabriel Câmara Oct 30 '15 at 19:23

2 Answers2

1

That code as supplied would not compile. For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Mypanel extends JPanel {

    boolean clicked = false;

    Mypanel() {
        setBackground(Color.GREEN);
        MouseListener mouseListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                clicked = true;
                repaint();
            }
        };
        this.addMouseListener(mouseListener);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 100);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (clicked) {
            g.drawOval(15, 15, 50, 50);
        }
    }
}

public class algo extends JFrame {

    public algo() {
        initComponents();
        pack();
        //jPanel1.setBackground(Color.GREEN); ?!?
    }

    protected final void initComponents() {
        add(new Mypanel());
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new algo().setVisible(true);
            }
        });
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-2

There are a few things to correct in your example...

When you create the frame (i.e. in the constructor) you'll want to call super(). This is the first thing the constructor has to do. Then, you'll probably want to set an initial width/height, and set the background color of the frame green.

You need to add a mouse listener so that the mouseClicked method is actually called. Then have it add the 'MyPanel' object to the frame, and call repaint.

I think that's roughly what you're going for.

  • 1
    *"I think that's roughly what you're going for."* I think you should test ideas before writing them in public. *"you'll probably want to set an initial width/height"* No, the components (including custom components like `Mypanel` should override `getPreferredSize` to return a sensible value, then the frame should be packed to that size. *"Then have it add the 'MyPanel' object to the frame, and call repaint."* Better to add the panel to the frame at start-up, then simply change its state (and call `repaint()` when the button is clicked. – Andrew Thompson Oct 30 '15 at 18:04
  • 1
    There is no reason to write `super()`. It is always called automatically, if no call to a different constructor is present. – VGR Oct 30 '15 at 18:09