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
}
}