
正文
java代码画圆圈 java绘制圆环
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java中画圈是哪个函数
使用drawOval来画圆圈,
g.drawOval(x, y, w, h); x,y表示圆所在的位置.w ,h 表示圆的宽高,当宽高不一致是就是椭圆
实例
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class SwingDemo extends JFrame{
public SwingDemo() {
setTitle("窗口");
setSize(280, 180);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new SwingDemo();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.drawOval(150, 100, 30, 20);//画椭圆
g.setColor(Color.BLUE);
g.drawOval(100, 50,60, 60);//画圆
}
}
显示
相关问答
Q1: JAVA画圆
import java.awt.Frame;
import java.awt.Graphics;
public class S extends Frame{
private int x;
private int y;
private boolean drawOval;//为true时绘制
//测试入口函数
public static void main(String []args)
{
new S().print();
}
//构造函数,初始化x、y坐标,设置drawOval变量为false,设置窗体大小
public S()
{
x = 200;
y = 200;
drawOval = false;
this.setSize(400,400);
this.setVisible(true);
}
public void print(){
//在调用S类实例的print方法时,画一个以属性X,Y为起点的宽高为10的圆.
drawOval = true; //设置drawOval变量为true
repaint(); //调用刷新画面方法
}
public void paint(Graphics g)
{
//为true时绘制
if(drawOval)g.fillOval(x,y,10,10);
}
}
Q2: java 如何用两个循环画圆
其实就是遍历每一个点,如果在圆形上,则输出#,不在圆形上,则输出空格。
代码如下:
float x = 14;
float y = 8;
float r = 5;
for (int i = 0; i 20; i++) {
for (int j = 0; j 20; j++) {
float result = (i - x) * (i - x) + (j - y) * (j - y) - r * r; // 圆心公式
if (result 5 result -5) { // 5 为精度范围
System.out.print("#");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
Q3: 急求Java代码,定义一个Circle(圆类型)
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圆心java代码画圆圈:(%f,%f),半径java代码画圆圈:%f,面积:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
Q4: 用JAVA编写圆
代码如下java代码画圆圈:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class TestSw extends JFrame { public static void main(String[] args) { new TestSw(); } public TestSw(){ super("Test"); this.setSize(new Dimension(400,300)); this.setContentPane(new Mypane()); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class Mypane extends JPanel{ public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.setXORMode(Color.white); g.drawArc(20, 20, 100, 100, 0, 360); ///此方法将画一个直径100java代码画圆圈的圆.红色. } } }
Q5: 用java画一个圆
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{
int x,y,r,n;
int x0,y0;
MyCanvas()
{
setSize(100,100);
setBackground(Color.red);
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setR(int r)
{
this.r=r;
}
public void setN(int n)
{
this.n=n;
}
public void paint(Graphics g1)
{
for(int i=0;i=360;i=i+360/n)
{
x0 = (int)(x+r*Math.cos(i));
y0 = (int)(y+r*Math.sin(i));
g1.drawString("*",x0,y0);}
}
}
public class e1 extends Applet implements ActionListener
{
MyCanvas canvas;
TextField inputR,inputX,inputY,inputN;
Label label1,label2,label3;
Button b1,b2;
public void init()
{
canvas = new MyCanvas();
inputR = new TextField(6);
inputX = new TextField(6);
inputY = new TextField(6);
inputN = new TextField(6);
b1 = new Button("确定");
b1.addActionListener(this);
label1 = new Label("输入位置坐标:");
label2 = new Label("输入半径:");
label3 = new Label("输入要打印的*数:");
add(label1);
add(inputX);
add(inputY);
add(label2);
add(inputR);
add(label3);
add(inputN);
add(b1);
add(canvas);
}
public void actionPerformed(ActionEvent e)
{
int x=0,y=0,n=0,r=0;
try
{
x=Integer.valueOf(inputX.getText()).intValue();
y=Integer.valueOf(inputY.getText()).intValue();
n=Integer.valueOf(inputN.getText()).intValue();
r=Integer.valueOf(inputR.getText()).intValue();
canvas.setX(x);
canvas.setY(y);
canvas.setR(r);
canvas.setN(n);
canvas.repaint();
}
catch(NumberFormatException ee)
{
x = 0;
y = 0;
r = 0;
n = 0;
}
}
}
public void draw(Graphics2D g) {
g.setColor(color);//设置颜色
g.setStroke(stroke);//宽度
int x, y, w, h;
if (startX endX) {//以下的startx 、endx都是由鼠标拖 动事件得到
x = endX;
w = startX - endX;
} else {
x = startX;
w = endX - startX;
}
if (startY endY) {
y = endY;
h = startY - endY;
} else {
y = startY;
h = endY - startY;
}
g.drawOval(x, y, w, h);
}
关于java代码画圆圈和java绘制圆环的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







