怎么用java生成10个随机圆,并且把期中最大的一个填充成红色?谢谢!
3个回答
展开全部
看一下这个
--------------------------------------------------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class A extends JFrame {
int width = 800;
int height = 600;
public A() {
getContentPane().add(new MyPanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(width, height);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
class MyPanel extends JPanel implements ActionListener {
public MyPanel() {
JButton btn = new JButton("reset");
btn.addActionListener(this);
add(btn);
}
public void paint(Graphics g) {
super.paint(g);
int len = 10;
// 直径
int[] diameter = new int[len];
Point[] point = new Point[len];
for (int i = 0; i < len; i++) {
point[i] = new Point();
}
int MaxDiameter = 0;
for (int i = 0; i < len; i++) {
int random = (int) (Math.random() * 1000);
diameter[i] = random % 80 + 1;
if (diameter[i] > MaxDiameter) {
MaxDiameter = diameter[i];
}
random = (int) (Math.random() * 1000);
point[i].x = random % 800 + 1;
point[i].y = random % 600 + 1;
if (point[i].x + diameter[i] > width) {
point[i].x -= diameter[i];
}
if (point[i].y + diameter[i] > height) {
point[i].y -= diameter[i];
}
}
for (int i = 0; i < len; i++) {
if (MaxDiameter == diameter[i]) {
Color color = g.getColor();
g.setColor(Color.RED);
g
.fillOval(point[i].x, point[i].y, diameter[i],
diameter[i]);
g.setColor(color);
} else {
g
.drawOval(point[i].x, point[i].y, diameter[i],
diameter[i]);
}
}
}
public void actionPerformed(ActionEvent e) {
repaint();
}
}
public static void main(String[] args) {
new A();
}
}
--------------------------------------------------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class A extends JFrame {
int width = 800;
int height = 600;
public A() {
getContentPane().add(new MyPanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(width, height);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
class MyPanel extends JPanel implements ActionListener {
public MyPanel() {
JButton btn = new JButton("reset");
btn.addActionListener(this);
add(btn);
}
public void paint(Graphics g) {
super.paint(g);
int len = 10;
// 直径
int[] diameter = new int[len];
Point[] point = new Point[len];
for (int i = 0; i < len; i++) {
point[i] = new Point();
}
int MaxDiameter = 0;
for (int i = 0; i < len; i++) {
int random = (int) (Math.random() * 1000);
diameter[i] = random % 80 + 1;
if (diameter[i] > MaxDiameter) {
MaxDiameter = diameter[i];
}
random = (int) (Math.random() * 1000);
point[i].x = random % 800 + 1;
point[i].y = random % 600 + 1;
if (point[i].x + diameter[i] > width) {
point[i].x -= diameter[i];
}
if (point[i].y + diameter[i] > height) {
point[i].y -= diameter[i];
}
}
for (int i = 0; i < len; i++) {
if (MaxDiameter == diameter[i]) {
Color color = g.getColor();
g.setColor(Color.RED);
g
.fillOval(point[i].x, point[i].y, diameter[i],
diameter[i]);
g.setColor(color);
} else {
g
.drawOval(point[i].x, point[i].y, diameter[i],
diameter[i]);
}
}
}
public void actionPerformed(ActionEvent e) {
repaint();
}
}
public static void main(String[] args) {
new A();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询