-1

I've been trying this for hours now with no avail.

I'm trying to use the paintComponent method in my Game.java class, but I'm not sure exactly how to do this.

I've tried calling the function directly but of course it does not work as it needs to return something.

The method I need to use is in this "Circles.java" class:


package testgame;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.JPanel;

public class Circles extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Bubbles(g); }

    private void Bubbles(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints rh
                = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING, 
                RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHints(rh);
        int x, y, size;
        x = (int) (Math.random() * 500) + 15;
        y = (int) (Math.random() * 450) + 15;
        size = (int) (Math.random() * 30) + 10;
        g2d.setColor(Color.green);
        g2d.drawOval(x, y, size, size); } 
}

This is the class that needs the paintComponent method (Game.java):


package testgame;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class Game extends JFrame {

    public static void LoadUI() {
        JFrame frame = new JFrame("Just a test!");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(550, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); }

public static void main(String[] args) {
     frame.add(new (Circles()));  }
}

The error I get is at the:

frame.add(new (Circles()));

The error being:

identifier expected

cannot find symbol
symbol:  method Circles()
location: class Game

cannot find symbol
symbol:  variable frame
location: class Game
jrtapsell
  • 6,719
  • 1
  • 26
  • 49
  • Possible duplicate of [paint() and repaint() in Java](https://stackoverflow.com/questions/10768619/paint-and-repaint-in-java) – Joe C Oct 14 '17 at 18:12
  • instead of frame.add(new (Circles())); write frame.add(new Circles()); – keuleJ Oct 14 '17 at 19:01

2 Answers2

0

First of all, It should be new Circle () instead of new (Circle ())

Second thing as new and Circle() both are in different bracket ,java treat Circle() as method of Class Game hence you are getting error like that.

Manish Jaiswal
  • 442
  • 5
  • 19
0

What you are trying to do here frame.add(new (Circles())); inside your main method is accessing a variable that is only available in your LoadUI() method.

To be able to access that variable you would need to declare it outside your LoadUI() method, something like this:

    ...
    public class Game extends JFrame {

    JFrame frame;

    public static void LoadUI()....

Secondly, as Manish Jaiswal mentioned above, your placement of brackets is wrong, meaning your way of initializing the Circles object is wrong.

To make this code work, you could do something like this in your main method:

LoadGUI();
frame.add(new Circles());

Though I would recommend using a separate class / object for your GUI / JFrame and not making the LoadUI() method static. You could also indent your code in an easier to read way, just to make it easier for yourself as well :)

  • Ayy thanks mate, it worked! I declared the variable like this: public static JFrame frame = new JFrame("Just a test!"); and Java got it right! By the way do you think I should delegate each class to do a specific function (like drawing the circle, loading the UI) and then execute all of that in the main? – Gianfranco Stercoforti Oct 15 '17 at 16:55
  • @GianfrancoStercoforti Well, in general Java is an object-oriented language, meaning that yes, you could use a separate class for each function. But what you should try to do is model classes (just partly) after some kind of object. That's why there is JFrame. It is supposed to represent a window, broadly speaking. You could do the same thing with the File class, which represents a file. So I would recommend you use separate methods for loading the UI and so on, but maybe keep it in the same class and work with objects / classes for things like file management instead (separate purpose). –  Oct 17 '17 at 19:45