When i am trying to access repaint() method for JPanel form main static method I am getting error: "Cannot make a static reference to the non-static method repaint() from the type Component". When I tried to change main method as "public void main", or remove static – it can not compile. What is the proper approach to solve this problem?
public class mull extends JPanel { ...
....
public static void main(String[] args) {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
ringPoints += 100;
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
squarePoints += 100;
allowedToDrawSquare = false;
}
if(!allowedToDrawSquare && !allowedToDrawRing){
// stop timer
// display points
ringTimer.stop();
squareTimer.stop();
showScores = true;
repaint(); // Cannot make a static reference to the non-static method repaint() from the type Component
}
}
});
f.add(p);
f.add(new mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
I replaced all my code form main and there is one detail I can not see how to deal with. In my main method I created JFrame and added to it times (Swing timers). When I palced all code into constructor it does not work and makes no sense to call constructor itself while inside in (in order to add to JFrame Timers). How to solve it?
Constructor:
public Mull() {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
ringPoints += 100;
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
squarePoints += 100;
allowedToDrawSquare = false;
}
if(!allowedToDrawSquare && !allowedToDrawRing){
// stop timer
// display points
ringTimer.stop();
squareTimer.stop();
showScores = true;
repaint();
}
}
});
f.add(p);
f.add(new Mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
ActionListener actionListenerRing = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(ringCounter < 3){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
while( !(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize )
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
ringCounter++;
repaint();
}
}
};
ActionListener actionListenerSquare = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(squareCounter < 3){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
while( !(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize )
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsSquare(xSquare, ySquare, squareSize);
squareCounter++;
repaint();
}
}
};
ringTimer = new Timer(700, actionListenerRing);
ringTimer.setInitialDelay(1000);
ringTimer.start();
squareTimer = new Timer(500, actionListenerSquare);
squareTimer.setInitialDelay(1000);
squareTimer.start();
}
Probably, I should move Timers methods to another class and then call something like f.add(new setTimers()); ….
Placing all your logic into the main method is a bad practice. It should reside in the object itself. That would also solve your static access problems. Here is how to do that:
Move all that code to another non-static method, for example
public void init() {}. Then, in yourpublic static void mainmethod, call that init method like this:Also, by Java naming conventions Java classes use CamelCase naming. So your class should be renamed to “Mull”.