
正文
绘制圆形的代码java java绘制圆形并计算周长和面积
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用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);
}
相关问答
Q1: 怎么用JAVA画半个圆或者是半圆弧?
画圆一般通过继承JPanel 或者JFrame ,通过调用panel或者frame中的Graphics实例完成画图。
java绘图的基本原理:画一个圆
import javax.swing.*;
import java.awt.*;
public class DrawCicle extends JFrame{MyPanel mp=null;public static void main(String[] args) {DrawCicle dc=new DrawCicle();}
public DrawCicle(){mp =new MyPanel();this.add(mp);this.setSize(300, 250);
this.setLocation(600, 300);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE)this.setVisible(true);}}
Q2: 试编写一个java应用程序,要求输入一个圆的半径,输出其面积
代码为:
Scanner scanner=new Scanner(System.in);
System.out.println("请输入圆绘制圆形的代码java的半径:");
int r=Integer.parseInt(scanner.next()); //获取半径
System.out.println("圆的面积为:"+Math.PI*r*r);
扩展资料:
java所具有的特性:
1.简单性
Java看起来设计得很像C++绘制圆形的代码java,但是为绘制圆形的代码java了使语言小和容易熟悉绘制圆形的代码java,设计者们把C++语言中许多可用的特征去掉了绘制圆形的代码java,这些特征是一般程序员很少使用的。例如,Java不支持goto语句,代之以提供break和continue语句以及异常处理。Java还剔除了C++的操作符过载(overload)和多继承特征,并且不使用主文件,免去了预处理程序。
因为Java没有结构,数组和串都是对象,所以不需要指针。Java能够自动处理对象的引用和间接引用,实现自动的无用单元收集,使用户不必为存储管理问题烦恼,能更多的时间和精力花在研发上。
2.面向对象
Java是一个面向对象的语言。对程序员来说,这意味着要注意应中的数据和操纵数据的方法(method),而不是严格地用过程来思考。在一个面向对象的系统中,类(class)是数据和操作数据的方法的集合。
数据和方法一起描述对象(object)的状态和行为。每一对象是其状态和行为的封装。类是按一定体系和层次安排的,使得子类可以从超类继承行为。在这个类层次体系中有一个根类,它是具有一般行为的类。Java程序是用类来组织的。
Java还包括一个类的扩展集合,分别组成各种程序包(Package),用户可以在自己的程序中使用。例如,Java提供产生图形用户接口部件的类(java.awt包),这里awt是抽象窗口工具集(abstractwindowingtoolkit)的缩写,处理输入输出的类(java.io包)和支持网络功能的类(java.net包)。
3.分布性
Java设计成支持在网络上应用,它是分布式语言。Java既支持各种层次的网络连接,又以Socket类支持可靠的流(stream)网络连接,所以用户可以产生分布式的客户机和服务器。
网络变成软件应用的分布运载工具。Java程序只要编写一次,就可到处运行。
4.编译和解释性
Java编译程序生成字节码(byte-code),而不是通常的机器码。Java字节码提供对体系结构中性的目标文件格式,代码设计成可有效地传送程序到多个平台。Java程序可以在任何实现了Java解释程序和运行系统(run-timesystem)的系统上运行。
在一个解释性的环境中,程序开发的标准“链接”阶段大大消失了。如果说Java还有一个链接阶段,它只是把新类装进环境的过程,它是增量式的、轻量级的过程。因此,Java支持快速原型和容易试验,它将导致快速程序开发。这是一个与传统的、耗时的“编译、链接和测试”形成鲜明对比的精巧的开发过程。
5.稳健性
Java原来是用作编写消费类家用电子产品软件的语言,所以它是被设计成写高可靠和稳健软件的。Java消除了某些编程错误,使得用它写可靠软件相当容易。
Java是一个强类型语言,它允许扩展编译时检查潜在类型不匹配问题的功能。Java要求显式的方法声明,它不支持C风格的隐式声明。这些严格的要求保证编译程序能捕捉调用错误,这就导致更可靠的程序。
可靠性方面最重要的增强之一是Java的存储模型。Java不支持指针,它消除重写存储和讹误数据的可能性。类似地,Java自动的“无用单元收集”预防存储漏泄和其它有关动态存储分配和解除分配的有害错误。Java解释程序也执行许多运行时的检查,诸如验证所有数组和串访问是否在界限之内。
异常处理是Java中使得程序更稳健的另一个特征。异常是某种类似于错误的异常条件出现的信号。使用try/catch/finally语句,程序员可以找到出错的处理代码,这就简化了出错处理和恢复的任务。
6.安全性
Java的存储分配模型是它防御恶意代码的主要方法之一。Java没有指针,所以程序员不能得到隐蔽起来的内幕和伪造指针去指向存储器
。更重要的是,Java编译程序不处理存储安排决策,所以程序员不能通过查看声明去猜测类的实际存储安排。编译的Java代码中的存储引用在运行时由Java解释程序决定实际存储地址。
Java运行系统使用字节码验证过程来保证装载到网络上的代码不违背任何Java语言限制。这个安全机制部分包括类如何从网上装载。例如,装载的类是放在分开的名字空间而不是局部类,预防恶意的小应用程序用它自己的版本来代替标准Java类。
7.可移植性
Java使得语言声明不依赖于实现的方面。例如,Java显式说明每个基本数据类型的大小和它的运算行为(这些数据类型由Java语法描述)。
Java环境本身对新的硬件平台和操作系统是可移植的。Java编译程序也用Java编写,而Java运行系统用ANSIC语言编写。
8.高性能
Java是一种先编译后解释的语言,所以它不如全编译性语言快。但是有些情况下性能是很要紧的,为了支持这些情况,Java设计者制作了“及时”编译程序,它能在运行时把Java字节码翻译成特定CPU(中央处理器)的机器代码,也就是实现全编译了。
Java字节码格式设计时考虑到这些“及时”编译程序的需要,所以生成机器代码的过程相当简单,它能产生相当好的代码。
9.多线索性
Java是多线索语言,它提供支持多线索的执行(也称为轻便过程),能处理不同任务,使具有线索的程序设计很容易。Java的lang包提供一个Thread类,它支持开始线索、运行线索、停止线索和检查线索状态的方法。
Java的线索支持也包括一组同步原语。这些原语是基于监督程序和条件变量风范,由C.A.R.Haore开发的广泛使用的同步化方案。用关键词synchronized,程序员可以说明某些方法在一个类中不能并发地运行。这些方法在监督程序控制之下,确保变量维持在一个一致的状态。
10.动态性
Java语言设计成适应于变化的环境,它是一个动态的语言。例如,Java中的类是根据需要载入的,甚至有些是通过网络获取的。
参考资料:百度百科-Java
Q3: 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);
}
}
Q4: 用JAVA语言编写在屏幕上绘制一个圆形并且分为思等分,分别填充4中不同的颜色。
//看看行不行绘制圆形的代码java:
package com;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class Circle extends JFrame {
private ChessPanel chessPanel=new ChessPanel(550);
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Circle thisClass = new Circle();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Circle() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(700, 600);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(chessPanel, BorderLayout.CENTER);
}
return jContentPane;
}
class ChessPanel extends JPanel{
private int r;
public ChessPanel(int r){
this.r=r;
//setBackground(Color.RED);
setSize(300,200);
}
public void paintComponent(Graphics g){ //覆盖paintComponent()方法
super.paintComponent(g); //必须先调用父类绘制圆形的代码java的方法
g.setColor(Color.DARK_GRAY);
//g.fillOval(0, 0, r, r);
double a,b;
int x,y;
g.setColor(Color.GREEN);
g.fillArc(r/2, r/2, r/2, r/2, 0, 90);
g.setColor(Color.DARK_GRAY);
g.fillArc(r/2, r/2, r/2, r/2, 90, 90);
g.setColor(Color.red);
g.fillArc(r/2, r/2, r/2, r/2, 180, 90);
g.setColor(Color.yellow);
g.fillArc(r/2, r/2, r/2, r/2, 270, 90);
}
}
}
Q5: 求一个java程序:绘图程序包括画圆,椭圆,线,矩形,自定义。并且可以调图形颜色!
public enum ShapeTypes {
LINE, CIRCLE, RECTANGLE
}
public interface Shape {
void paint(Graphics g);
}
public class Rectangle implements Shape {
// 矩形左上角的坐标
private int x, y;
// 矩形的宽度和高度
private int width, height;
private Color rectangleColor;
public Rectangle() {
super();
}
public Rectangle(int x, int y, int width, int height, Color rectangleColor) {
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.rectangleColor = rectangleColor;
}
@Override
public void paint(Graphics g) {
g.setColor(rectangleColor);
g.drawRect(x, y, width, height);
}
}
public class Line implements Shape {
// 直线的起始位置
private int x1, y1;
// 直线的终止位置
private int x2, y2;
private Color lineColor;
public Line(int x1, int y1, int x2, int y2, Color lineColor) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.lineColor = lineColor;
}
public Line() {
super();
}
@Override
public void paint(Graphics g) {
g.setColor(lineColor);
g.drawLine(x1, y1, x2, y2);
}
}
public class Circle implements Shape {
// 圆的颜色
private Color circleColor;
// 圆心的坐标
private int x, y;
// 圆的半径
private int radius;
public Circle() {
super();
}
public Circle(int x, int y, int radius, Color circleColor) {
super();
this.circleColor = circleColor;
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void paint(Graphics g) {
g.setColor(circleColor);
// 画弧, 当弧的宽度和高度一致且从0~360度时就是原形了
g.drawArc(x, y, radius, radius, 0, 360);
}
}
public class SketchpadPanel extends Canvas implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = -5229161042153132522L;
// 鼠标点击起始坐标和当前坐标
private int beginX = 0, beginY = 0, currentX = 0, currentY = 0;
// 判断鼠标是否被按下
private boolean isMousePressing = false;
// 保存当前的图形, 在撤销和恢复时使用
private final StackShape currentShapes = new StackShape();
// 保存已经删除过的图形
private final StackShape deletedShapes = new StackShape();
private ShapeTypes type;
private Color color;
public SketchpadPanel() {
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* 撤销方法
*/
public void undo() {
if (currentShapes.size() 0) {
// 从所有保存过的图形中取出最后一个, 放入到已删除的图形中去
Shape shape = currentShapes.pop();
deletedShapes.push(shape);
repaint();
}
}
/**
* 恢复撤销方法
*/
public void redo() {
if (deletedShapes.size() 0) {
// 从所有删除的图形中取出最后一个, 放入保存的图形中
Shape shape = deletedShapes.pop();
currentShapes.push(shape);
repaint();
}
}
/**
* 设置命令
*
* @param type
*/
public void setShapeType(ShapeTypes type) {
this.type = type;
}
/**
* 设置颜色
*
* @param color
*/
public void setColor(Color color) {
this.color = color;
}
public void updete(Graphics g) {
paint(g);
}
/**
* 绘制画板
*/
@Override
public void paint(Graphics g) {
// 绘制画板
Dimension size = getSize();
int width = size.width;
int height = size.height;
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 绘制所有图形
Shape shape = null;
EnumerationShape e = currentShapes.elements();
while (e.hasMoreElements()) {
shape = e.nextElement();
shape.paint(g);
}
// 如果当前鼠标没有释放
if (isMousePressing) {
g.setColor(color);
switch (type) {
// 绘制直线
case LINE:
g.drawLine(beginX, beginY, currentX, currentY);
break;
// 绘制矩形
case RECTANGLE:
if (currentX beginX) {
if (currentY beginY) {
// 如果当前位置在起始位置的左上方, 则以鼠标当前位置为矩形的左上角位置
g.drawRect(currentX, currentY, beginX - currentX, beginY - currentY);
} else {
// 如果当前位置在起始位置的左下方, 则以鼠标当前位置的横坐标和起始位置的纵坐标作为矩形的左上角位置
g.drawRect(currentX, beginY, beginX - currentX, currentY - beginY);
}
} else {
if (currentY beginY) {
// 如果当前位置在起始位置的右上方, 则以鼠标起始位置的很坐标和当前位置的纵坐标作为矩形的左上角位置
g.drawRect(beginX, currentY, currentX - beginX, beginY - currentY);
} else {
// 如果当前位置在起始位置的右下方, 则已起始位置作为矩形的左上叫位置
g.drawRect(beginX, beginY, currentX - beginX, currentY - beginY);
}
}
break;
// 绘制圆形
case CIRCLE:
// 半径为a*a + b*b的平方根
int radius = (int) Math
.sqrt((beginX - currentX) * (beginX - currentX) + (beginY - currentY) * (beginY - currentY));
g.drawArc(beginX - radius / 2, beginY - radius / 2, radius, radius, 0, 360);
break;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
/**
* 当鼠标按下的时候获得起始坐标
*/
@Override
public void mousePressed(MouseEvent e) {
beginX = e.getX();
beginY = e.getY();
isMousePressing = true;
}
/**
* 当鼠标释放时获得当前坐标
*/
@Override
public void mouseReleased(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
isMousePressing = false;
// 当释放鼠标时, 将绘制的图形保存到shapes中
switch (type) {
// 绘制直线
case LINE:
Line line = new Line(beginX, beginY, currentX, currentY, color);
currentShapes.push(line);
break;
// 绘制圆形
case CIRCLE:
// 半径为a*a + b*b的平方根
int radius = (int) Math
.sqrt((beginX - currentX) * (beginX - currentX) + (beginY - currentY) * (beginY - currentY));
Circle circle = new Circle(beginX - radius / 2, beginY - radius / 2, radius, color);
currentShapes.push(circle);
break;
// 绘制矩形
case RECTANGLE:
Rectangle rectangle = null;
if (currentX beginX) {
if (currentY beginY) {
rectangle = new Rectangle(currentX, currentY, beginX - currentX, beginY - currentY, color);
} else {
rectangle = new Rectangle(currentX, beginY, beginX - currentX, currentY - beginY, color);
}
} else {
if (currentY beginY) {
rectangle = new Rectangle(beginX, currentY, currentX - beginX, beginY - currentY, color);
} else {
rectangle = new Rectangle(beginX, beginY, currentX - beginX, currentY - beginY, color);
}
}
currentShapes.push(rectangle);
break;
}
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
this.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
public class SketchpadFrame extends JFrame {
private static final long serialVersionUID = -7080053971741609904L;
private final JPanel commandPanel = new JPanel(); // 存放命令的面板
private final JPanel colorPanel = new JPanel(); // 存放颜色的面板
private final JPanel mainPanel = new JPanel(); // 主面板
private final JButton redButton = new JButton("红色");
private final JButton blueButton = new JButton("蓝色");
private final JButton greenButton = new JButton("绿色");
private final JButton lineButton = new JButton("直线");
private final JButton circleButton = new JButton("圆");
private final JButton rectangleButton = new JButton("矩形");
private final JButton undoButton = new JButton("撤销");
private final JButton redoButton = new JButton("恢复撤销");
private final JButton exitButton = new JButton("退出");
SketchpadPanel sketchPanel = new SketchpadPanel();
private void initFrame() {
commandPanel.setLayout(new FlowLayout());
commandPanel.add(lineButton);
commandPanel.add(circleButton);
commandPanel.add(rectangleButton);
commandPanel.add(undoButton);
commandPanel.add(redoButton);
commandPanel.add(exitButton);
colorPanel.setLayout(new FlowLayout());
colorPanel.add(redButton);
colorPanel.add(blueButton);
colorPanel.add(greenButton);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(commandPanel, BorderLayout.NORTH);
mainPanel.add(colorPanel, BorderLayout.CENTER);
getContentPane().add("South", mainPanel);
getContentPane().add("Center", sketchPanel);
// 初始化设置:颜色和命令
lineButton.setForeground(Color.RED);
sketchPanel.setColor(Color.RED);
redButton.setForeground(Color.RED);
sketchPanel.setShapeType(ShapeTypes.LINE);
}
private void initListener() {
redButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redAction(e);
}
});
blueButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
blueAction(e);
}
});
greenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
greenAction(e);
}
});
undoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
undoAction(e);
}
});
redoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redoAction(e);
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitAction(e);
}
});
lineButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lineAction(e);
}
});
circleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
circleAction(e);
}
});
rectangleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rectangleAction(e);
}
});
}
public SketchpadFrame() {
initFrame();
initListener();
this.setSize(500, 600);
setLocationByPlatform(true);
setResizable(true);
}
/* ********************处理事件********************* */
private void undoAction(ActionEvent e) {
sketchPanel.undo();
}
private void redoAction(ActionEvent e) {
sketchPanel.redo();
}
private void exitAction(ActionEvent e) {
System.exit(0);
}
private void lineAction(ActionEvent e) {
// 选中按钮为红色, 其余为黑色
lineButton.setForeground(Color.RED);
circleButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.LINE);
}
private void circleAction(ActionEvent e) {
circleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.CIRCLE);
}
private void rectangleAction(ActionEvent e) {
rectangleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
circleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.RECTANGLE);
}
private void redAction(ActionEvent e) {
redButton.setForeground(Color.RED);
blueButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.RED);
}
private void blueAction(ActionEvent e) {
blueButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.BLUE);
}
private void greenAction(ActionEvent e) {
greenButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
blueButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.GREEN);
}
}
/**
*
* @author 不落的太阳(Sean Yang)
* @version 1.0
* @since JDK 1.8
*
*/
public class SketchpadMain {
/**
* 测试方法
*
* @param args命令行参数
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new SketchpadFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
绘制圆形的代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java绘制圆形并计算周长和面积、绘制圆形的代码java的信息别忘了在本站进行查找喔。






