JAVA中如何用graphics 作圆环
答案:3 悬赏:50 手机版
解决时间 2021-02-28 05:43
- 提问者网友:王者佥
- 2021-02-27 22:27
JAVA中如何用graphics 作圆环,可以设置圆环的内径,外径
最佳答案
- 五星知识达人网友:由着我着迷
- 2021-02-27 22:38
package zhidao;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class Circle extends JApplet {
class Canvas extends JLabel {
public Canvas() {
}
@Override
public void paint(Graphics g) {
super.paint(g);
int w=getWidth();
int h=getHeight();
for (int i = 0; i < 10; i++) {
g.setColor(randColor());
int radius=(int)(Math.random()*51+80);
int x=(int)(Math.random()*(w-radius*2));
int y=(int)(Math.random()*(h-radius*2));
Ring ring = new Ring(x, y, radius,(float)(Math.random()/2+0.5));
ring.paint(g);
}
}
}
private Color randColor(){
int r=(int)(Math.random()*256);
int g=(int)(Math.random()*256);
int b=(int)(Math.random()*256);
return new Color(r,g,b);
}
public Circle() {
}
@Override
public void init() {
super.init();
setSize(500, 500);
getContentPane().add(new Canvas(), BorderLayout.CENTER);
}
}
class Ring {
private int x;
private int y;
// 外径
private int radius;
// 内径因子
private float factor;
public int getX() {
return x;
}
public int getY() {
return y;
}
public Ring(int x, int y, int radius, float factor) {
super();
this.x = x;
this.y = y;
this.radius = radius;
if (factor < 0 || factor > 1)
throw new IllegalArgumentException();
this.factor = factor;
}
public int getInsideRadius() {
return (int) (radius * factor + 0.5f);
}
public int getRadius() {
return radius;
}
public void paint(Graphics g) {
g.fillOval(x, y, radius, radius);
int iWidth = getInsideRadius();
int dif = Math.round((radius - iWidth) / 2f);
Color recent = g.getColor();
g.setColor(new Color(not(recent.getRGB())));
g.fillOval(x + dif, y + dif, iWidth, iWidth);
g.setColor(recent);
}
private int not(int color) {
return (~color) & 0xffffff;
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class Circle extends JApplet {
class Canvas extends JLabel {
public Canvas() {
}
@Override
public void paint(Graphics g) {
super.paint(g);
int w=getWidth();
int h=getHeight();
for (int i = 0; i < 10; i++) {
g.setColor(randColor());
int radius=(int)(Math.random()*51+80);
int x=(int)(Math.random()*(w-radius*2));
int y=(int)(Math.random()*(h-radius*2));
Ring ring = new Ring(x, y, radius,(float)(Math.random()/2+0.5));
ring.paint(g);
}
}
}
private Color randColor(){
int r=(int)(Math.random()*256);
int g=(int)(Math.random()*256);
int b=(int)(Math.random()*256);
return new Color(r,g,b);
}
public Circle() {
}
@Override
public void init() {
super.init();
setSize(500, 500);
getContentPane().add(new Canvas(), BorderLayout.CENTER);
}
}
class Ring {
private int x;
private int y;
// 外径
private int radius;
// 内径因子
private float factor;
public int getX() {
return x;
}
public int getY() {
return y;
}
public Ring(int x, int y, int radius, float factor) {
super();
this.x = x;
this.y = y;
this.radius = radius;
if (factor < 0 || factor > 1)
throw new IllegalArgumentException();
this.factor = factor;
}
public int getInsideRadius() {
return (int) (radius * factor + 0.5f);
}
public int getRadius() {
return radius;
}
public void paint(Graphics g) {
g.fillOval(x, y, radius, radius);
int iWidth = getInsideRadius();
int dif = Math.round((radius - iWidth) / 2f);
Color recent = g.getColor();
g.setColor(new Color(not(recent.getRGB())));
g.fillOval(x + dif, y + dif, iWidth, iWidth);
g.setColor(recent);
}
private int not(int color) {
return (~color) & 0xffffff;
}
}
全部回答
- 1楼网友:青灯有味
- 2021-02-28 00:50
两个圆,圆心一样半径不一样就是了
- 2楼网友:深街酒徒
- 2021-02-28 00:02
试一下这个:
image image=toolkit.getdefaulttoolkit().getimage("图片名.jpg");
g.drawimage(image, 50, 160, 150,182, this);//设定位置
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯