문제

I am trying to run a program that fills the circles with color I am not sure what I am doing incorrectly I am getting a cannot find symbol error for my fillOval command here is my code. Also the fill should be the same color as the drawn circle.

import javax.swing.JApplet;
import java.awt.*;
import java.util.Random;

public class Drawing extends JApplet
{
public void paint(Graphics page)
{
Random generator=new Random();


float r = generator.nextFloat();
float g = generator.nextFloat();
float b = generator.nextFloat();

Color randomColor = new Color(r, g, b);

int random,randomx,randomy;
int x,y;
int width, height;
setBackground(Color.white);

random=generator.nextInt(24)+8;
randomx=generator.nextInt(24);
randomy=generator.nextInt(24);
x=randomx;
y=randomy;
width=random*2;
height=random*2;
page.drawOval(x, y, width, height);
page.setColor(randomColor);
fillOval(x, y,width,height);
}
}
도움이 되었습니까?

해결책

fillOval is a method of Graphics rather than your custom Drawing class.

fillOval(x, y, width, height);    

should be

page.fillOval(x, y, width, height);

다른 팁

Problems:

  1. You're calling fillOval by itself and not on any variable, such as the page variable.
  2. You should not override the paint method of any component except in unusual circumstances.
  3. You should not draw directly in a top-level window such as a JApplet.
  4. You're posting un-formatted all left-justified code, making it somewhat difficult to read and understand.

I suggest:

  1. Create a class that extends JPanel and override its paintComponent(Graphics g) method.
  2. Don't forget to call the super's paintComponent method, super.paintComponent(g), in your paintComponent override method, so that the component does all its housekeeping painting before doing your painting.
  3. Draw with its Graphic object that is passed into the method's parameter.
  4. Add the JPanel to your JApplet to display it.
  5. When posting code here, please format your posted code by giving it proper indentations, usually 3 spaces per block, and making sure that all code on the same block is on the same indentation level. Your cooperation in this would be greatly appreciated and will likely improve your chances of getting a decent and prompt answer. Remember that we are all volunteers and thus you should strive to make helping you as easy as possible.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top