你先想这道题的思路吧。
创新互联-专业网站定制、快速模板网站建设、高性价比来凤网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式来凤网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖来凤地区。费用合理售后完善,10年实体公司更值得信赖。
首先打印出圆形的话就是在一个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('*');
}
}
}
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);
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
class PaintovalPane extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.pink);
g.fillOval(100, 100, 200, 200);
}
}
class PaintovalFrame extends JFrame
{
public PaintovalFrame()
{
setTitle("lamp");
setSize(500,500);
addWindowListener(
new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new PaintovalPane());
}
}
public class Paintoval {
public static void main(String[] args) {
JFrame f = new PaintovalFrame();
f.show();
}
}
这个可是够简单了
靠,楼上的回答那么长啊,只要一个函数,就是
drawOval(int
x,int
y,int
w,int
h);
这是是画椭圆形的函数,但是它也可以画圆形。
比如
drawOval(100,100,50,50);
就在坐标50,50画一个直径100的圆,只要把,最后的2个参数设成一样就是一个圆。要画直径200的话,就把最后2个参数设成200,200
一切OK了
代码如下:
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的圆.红色. } } }
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);
}
}