写一个简单的java程序
写一个程序:画20个圆,用(半径,每一个圆的起始点(x,y)半径跟起始点)(都为随机数),然后把每个圆的描述存到一个array中,如果有一个或者一个以上的圆互相交叉,就用...
写一个程序: 画20个圆,用(半径,每一个圆的起始点(x, y)半径跟起始点)(都为随机数),然后把每个圆的描述存到一个array中,如果有一个或者一个以上的圆互相交叉,就用蓝色画圆,如果一个园没有跟任何圆相碰,就用黑色画~
请给出可运行的程序 展开
请给出可运行的程序 展开
6个回答
展开全部
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CircleFrame {
private Circle[] circles = null;
private JFrame frame = null;
final int circleNumber = 20;
final int maxCircleDiameter = 50;
private final Color contactColor = Color.BLACK;
private final Color apartColor = Color.BLUE;
public CircleFrame() {
initFrame();
initCircle();
drawColor();
showCircle();
}
private void showCircle() {
JPanel panel = new MyPanel();
panel.setBounds(0, 0, 800, 600);
Container container = frame.getContentPane();
container.setLayout(null);
container.add(panel);
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics graphics) {
for (int i = circles.length - 1; i >= 0; i--) {
graphics.setColor(circles[i].color);
final int diameter = circles[i].diameter;
graphics.drawOval(circles[i].x - diameter, circles[i].y - diameter, diameter * 2, diameter * 2);
}
}
}
private void drawColor() {
int delX, delY, sumDiameter;
for (int i = circles.length - 1; i > 0; i--) {
for (int j = i - 1; j >= 0; j--) {
delX = circles[i].x - circles[j].x;
delY = circles[i].y - circles[j].y;
sumDiameter = circles[i].diameter + circles[j].diameter;
if (delX * delX + delY * delY < sumDiameter * sumDiameter) {
circles[i].color = contactColor;
circles[j].color = contactColor;
}
}
}
}
private void initCircle() {
circles = new Circle[circleNumber];
int x, y, diameter;
for (int i = 0; i < circles.length; i++) {
x = (int)(Math.random() * frame.getWidth());
y = (int)(Math.random() * frame.getHeight());
diameter = (int)(Math.random() * maxCircleDiameter + 1);
circles[i] = new Circle(x, y, diameter);
circles[i].color = apartColor;
}
}
private void initFrame() {
frame = new JFrame("Draw Circle");
frame.setSize(800, 600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void show() {
frame.setVisible(true);
}
public static void main(String[] args) {
CircleFrame tenCircle = new CircleFrame();
tenCircle.show();
}
}
class Circle {
int x, y, diameter;
Color color = null;
public Circle(int x, int y, int diameter) {
this.x = x;
this.y = y;
this.diameter = diameter;
}
public String toString() {
return "[x=" + x + ", y=" + y + ", diameter=" + diameter + "], Color:" + color;
}
}
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CircleFrame {
private Circle[] circles = null;
private JFrame frame = null;
final int circleNumber = 20;
final int maxCircleDiameter = 50;
private final Color contactColor = Color.BLACK;
private final Color apartColor = Color.BLUE;
public CircleFrame() {
initFrame();
initCircle();
drawColor();
showCircle();
}
private void showCircle() {
JPanel panel = new MyPanel();
panel.setBounds(0, 0, 800, 600);
Container container = frame.getContentPane();
container.setLayout(null);
container.add(panel);
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics graphics) {
for (int i = circles.length - 1; i >= 0; i--) {
graphics.setColor(circles[i].color);
final int diameter = circles[i].diameter;
graphics.drawOval(circles[i].x - diameter, circles[i].y - diameter, diameter * 2, diameter * 2);
}
}
}
private void drawColor() {
int delX, delY, sumDiameter;
for (int i = circles.length - 1; i > 0; i--) {
for (int j = i - 1; j >= 0; j--) {
delX = circles[i].x - circles[j].x;
delY = circles[i].y - circles[j].y;
sumDiameter = circles[i].diameter + circles[j].diameter;
if (delX * delX + delY * delY < sumDiameter * sumDiameter) {
circles[i].color = contactColor;
circles[j].color = contactColor;
}
}
}
}
private void initCircle() {
circles = new Circle[circleNumber];
int x, y, diameter;
for (int i = 0; i < circles.length; i++) {
x = (int)(Math.random() * frame.getWidth());
y = (int)(Math.random() * frame.getHeight());
diameter = (int)(Math.random() * maxCircleDiameter + 1);
circles[i] = new Circle(x, y, diameter);
circles[i].color = apartColor;
}
}
private void initFrame() {
frame = new JFrame("Draw Circle");
frame.setSize(800, 600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void show() {
frame.setVisible(true);
}
public static void main(String[] args) {
CircleFrame tenCircle = new CircleFrame();
tenCircle.show();
}
}
class Circle {
int x, y, diameter;
Color color = null;
public Circle(int x, int y, int diameter) {
this.x = x;
this.y = y;
this.diameter = diameter;
}
public String toString() {
return "[x=" + x + ", y=" + y + ", diameter=" + diameter + "], Color:" + color;
}
}
展开全部
很想帮你,可惜...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个用C实现比java要好~可惜我很久没碰这个图形了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
从计算器开始吧。。+
-
,*
/、
-
,*
/、
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不是那么容易的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public
class
Test
{
public
static
void
main(String[]
args)
{
System.out.println("Hello
world!");
}
}呵呵,一个最简单的helloworld
class
Test
{
public
static
void
main(String[]
args)
{
System.out.println("Hello
world!");
}
}呵呵,一个最简单的helloworld
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询