资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java画个圆代码,java编程画个圆

请写出用java代码画一个圆

靠,楼上的回答那么长啊,只要一个函数,就是

我们提供的服务有:成都网站设计、做网站、成都外贸网站建设公司、微信公众号开发、网站优化、网站认证、滨州ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的滨州网站制作公司

drawOval(int

x,int

y,int

w,int

h);

这是是画椭圆形的函数,但是它也可以画圆形。

比如

drawOval(100,100,50,50);

就在坐标50,50画一个直径100的圆,只要把,最后的2个参数设成一样就是一个圆。要画直径200的话,就把最后2个参数设成200,200

一切OK了

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);

}

}

用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); ///此方法将画一个直径100的圆.红色. } } }

java代码画出一个圆

你先想这道题的思路吧。

首先打印出圆形的话就是在一个60-60这种正方形里面话个圆,你先要知道要画的点的位置对吧。算出所有的点打印出来。就是这个思路。然后是怎么算的问题了

public class SSS {

public static void main(String[] args) {

//半径

int r = 30;

for (int y = 0; y = 2 * r; y += 2) {

long x = Math.round(r - Math.sqrt(2 * r * y - y * y));

long longLength = 2 * (r - x);

for (int i = 0; i = x; i++) {

System.out.print(' ');

}

System.out.print('*');

for (int j = 0; j = longLength; j++) {

System.out.print(' ');

}

System.out.println('*');

}

}

}

用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编程画个圆
网站URL:http://cdkjz.cn/article/phhdoi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220