【发布时间】:2017-10-14 18:11:23
【问题描述】:
我已经尝试了几个小时了,但无济于事。
我正在尝试在我的 Game.java 类中使用 paintComponent 方法,但我不确定具体如何执行此操作。
我尝试过直接调用该函数,但它当然不起作用,因为它需要返回一些东西。
我需要使用的方法在这个“Circles.java”类中:
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); }
}
这是需要paintComponent方法的类(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())); }
}
我得到的错误是:
frame.add(new (Circles()));
错误是:
identifier expected
cannot find symbol
symbol: method Circles()
location: class Game
cannot find symbol
symbol: variable frame
location: class Game
【问题讨论】:
-
而不是 frame.add(new (Circles()));写 frame.add(new Circles());
标签: java class methods paintcomponent