0

if I set (e.g. g.setOval) by direct way I get one Oval on window but If I set Object with creating g.setOval inside I sometimes get more than one Oval on window (2,3 or 4), look on code.

-Why do this can explain me? -How I can create Graphics inside my Object and get one Oval after one call Object. (no 2,3 or 4)

public class Obj {

    Random rand;
    private int x, y;

    public void paintc (Graphics g,int x, int y) {
        g.fillOval(x,y,20,20);
        System.out.println(x + " : " +y);
    }
}


public class Main extends JPanel{

    private Obj obj;
    Random rand;

        public Main () {
            obj = new Obj();
        }

        public void paint(Graphics g) {
            rand = new Random();
            int x = rand.nextInt(300 - 20);
            int y = rand.nextInt(200 - 20);

            obj.paintc(g,x,y);              //by this way I sometimes get more than one Oval why????
            g.fillOval(x,y,20,20);          //by this way I get one oval It is alright
        }

        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(300,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.add(new Main());
        }
    }
  • You are forgetting to call the `super.paint(g);` method in your paint method override. Oh and you shouldn't even be overriding paint. Override paintComponent instead. – Hovercraft Full Of Eels Aug 22 '17 at 01:01
  • Thx but it is not a problem. I tried change it but behavior of program is same. – ShowMeYourCode Aug 22 '17 at 20:38
  • And.. This isn' t duplicate of "My overriden paint method is not getting called" and "Concerns about the function of JPanel: paintcomponent() " please uncheck it as duplicate. – ShowMeYourCode Aug 22 '17 at 20:55

0 Answers0