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
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 :)