내 컴퓨터 과학 수업에서 우리는 자바를 배우고 있으며 우리는 객체 지향과 그것이 유용하다는 것을 배우는 과정에서 핵심으로 왔습니다. 며칠 전에 프로젝트를 시작했는데, 기본 객체 지향에 대한 임무 대신에, 선생님은 실제로 우리를 가르치지 않고 그래픽을 바로 만들어서 저와 다른 몇몇 사람들에게 도전하기로 결정했습니다.JPanel 객체 지향 그래픽
우리는 정수를 받아 배열을 만들 수있는 "분자"프로그램을 만들고 배열의 각 사용 가능한 섹션에 대해 임의의 x 및 y 좌표와 반경의 크기를 작성해야했습니다. 이 변수를 사용하면 타원이 생성됩니다. 또한 선생님은 JFrame 또는 JPanel을 사용하여이 작업을 수행해야한다고 말했고 JFrame을 선택했습니다.
아래 코드에서 내 시도를 볼 수 있으며 어디서 붙어 있습니다. 나는이 경우 중요하지 않기 때문에 다른 색상을 설정하는 부분을 주석 처리했습니다. 누군가가 나에게 도움이되지만 내가 잘못 가고, 어떻게 미래에이 문제를 해결하는 곳도 나에게 설명 할 수 있다면
/**
* Name: Dylan Eisen
* Date: May 1, 2017
* Project: Object Oriented
* Program: Molecules.java
*/
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Molecules extends JFrame
{
static Graphics g;
public Molecules(Graphics g, int x, int y, int size)
{
super.paint(g);
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int num = in.nextInt();
int x = 20, y = 20, size = 20;
object elements[] = new object[num];
for(int i = 0; i < elements.length; i++)
{
x = (int)(Math.random()*1600);
y = (int)(Math.random()*900);
size = (int)(Math.random()*100);
elements[i] = new object(g, x, y, size);
}
Molecules f = new Molecules(g, x, y, size);
f.setSize(1600, 900);
f.setVisible(true);
//f.getContentPane().setBackground(Color.BLACK);
f.setTitle("Molecules Program - Dylan Eisen");
}
}
class object
{
Graphics g;
int x, y, size;
public object(Graphics g, int x, int y, int size)
{
this.x = x;
this.y = y;
this.size = size;
}
void paint(Graphics g)
{
//g.setColor(Color.WHITE);
g.fillOval(x, y, size, size);
}
}
, 정말 도움이 될 것입니다!