-1

I'm trying to use the method paint() inside a class which extends JFrame class to paint a dice which rolls randomly. When trying to execute this code an error is displayed and nothing appears as output.

Can someone show me how to implement this method paint() in the right way inside JFrame?

This is my code:

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

public class Background extends JFrame { 
    private Random ran;
    private int value;
    private JButton b;

    public Background () {
        super("the title");
        setLayout(new FlowLayout());
        ran = new Random();
        value = nextValue() ;
        b=new JButton("ROLL THE DICES");

        b.setForeground(Color.WHITE);//ndryshon ngjyren e shkrimit
        b.setBackground(Color.YELLOW);
        b.setBounds(20, 30, 20, 70);
        add("South",b);

        thehandler hand=new thehandler();
        b.addActionListener(hand);
    }

    private int nextValue() {
        return Math.abs(ran.nextInt()) % 6 + 1 ;
    }

    public void roll() {
        value = nextValue();
        repaint();
    }

    public int getValue() {
        return value;
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.draw3DRect(40, 40, 60, 60,true);
        /*
        g.drawLine(40,40,40,100);
        g.drawLine(40,100,100,100);
        g.drawLine(100,100,100,40);
        g.drawLine(100,40,40,40);
        */
        g.setColor(Color.RED);
        if (value == 1 || value == 3 || value == 5) g.fillOval(68,68,4,4);
        if (value != 1) { g.fillOval(53,83,4,4) ; g.fillOval(83,53,4,4) ;}
        if (value == 4 || value == 5 || value == 6)
        { g.fillOval(53,53,4,4) ; g.fillOval(83,83,4,4) ;}
        if (value == 6) { g.fillOval(68,53,4,4) ;g.fillOval(68,83,4,4) ;}
    }

    private class thehandler implements ActionListener{
        private Background m;

        thehandler(Background thisone){
            m=thisone;
        }

        public void actionPerformed(ActionEvent event) {
            m.roll();
        }
    }

    public static void main(String[] args) {
        ("Center",d);
        // f.add("South",b = new Button("Roll"));
        // b.addActionListener(new Doit(d));
        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d.getContentPane().setBackground(Color.GREEN);
        f.setSize(400,300);
        f.pack() ;
        f.setVisible(true); Frame f = new Frame("Dice");
        // Button b;
        Background  d = new Background();
        // f.addWindowListener(new MyListener());
        //f.add 
    }
}
Doggy
  • 63
  • 2
  • 10

1 Answers1

2

So, you're basic problem is here...

thehandler hand = new thehandler();

The constructor of thehandler requires an instance of Background...

private class thehandler implements ActionListener {
    //...
    thehandler(Background thisone) {

so, it should actually be...

thehandler hand = new thehandler(this);

That gets rid of the compiler error.

The cause of it not painting anything is because you've broken the paint chain by overriding paint of JFrame and the failing to call super.paint.

See Painting in AWT and Swing and Performing Custom Painting for more details of how painting works

The general answer, is don't do this, there are lots of reasons why you shouldn't override paint of a JFrame:

and this is just another one of them.

Instead, create a separate component (extending from JPanel for example) and override it's paintComponent method and perform your custom painting (calling super.paintComponent first)

Have a look at JFrame shows up grey, blank for a more detailed example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366