「java椭圆」java画椭圆
本篇文章给大家谈谈java椭圆,以及java画椭圆对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
用java怎样画椭圆?
你的问题是是使用java画椭圆,可以使用awt和swing类库实现
画椭圆可以通过画矩形及其内切椭圆实现,示例代码如下
如果只需要椭圆,则无需g2.draw(rect);
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// 画矩形
double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;
Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect);
// 画rect的内切椭圆
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
}
}
java中椭圆焦点如何定义
1、定义一个ImageView
定义一个ImageView是为了装载图片,其中的图片将被rotate用来进行旋转,其他View亦可。
资源文件为
Java代码
?xml version="1.0" encoding="utf-8"?
LinearLayout
xmlns:android=""
android:layout_width="match_parent"
android:layout_height="match_parent"
ImageView
android:id="@+id/infoOperating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/operating"
android:scaleType="center"
/ImageView
/LinearLayout
其中的android:src为图片内容,可使用附件中的图片。
java代码为
Java代码
ImageView infoOperatingIV = (ImageView)findViewById(R.id.infoOperating);
2、定义rotate旋转效果
在res/anim文件夹下新建tip.xml文件,内容如下
Java代码
?xml version="1.0" encoding="utf-8"?
set xmlns:android=""
rotate
android:fromDegrees="0"
android:toDegrees="359"
android:duration="500"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" /
/set
含义表示从0到359度开始循环旋转,0-359(若设置成360在停止时会出现停顿现象)度旋转所用时间为500ms,旋转中心距离view的左顶点为50%距离,距离view的上边缘为50%距离,即正中心,具体每个含义见下面的具体属性介绍。
java代码为
Java代码
Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);
LinearInterpolator lin = new LinearInterpolator();
operatingAnim.setInterpolator(lin);
setInterpolator表示设置旋转速率。LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果,具体可见下面android:interpolator的介绍。
a. 关于其中的属性意义如下(红色部分加以注意):
android:fromDegrees 起始的角度度数
android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针。如10圈则比android:fromDegrees大3600即可
android:pivotX 旋转中心的X坐标
浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心
android:pivotY 旋转中心的Y坐标
浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心
android:duration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。
android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,
android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行
android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止
android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效
android:detachWallpaper 表示是否在壁纸上运行
android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。
normal保持内容当前的z轴顺序
top运行时在最顶层显示
bottom运行时在最底层显示
b. 运行速度
运行速度为运行时间(android:duration)除以运行角度差(android:toDegrees-android:fromDegrees),比如android:duration为1000,android:toDegrees为360,android:fromDegrees为0就表示1秒转1圈。
c. 循环运行
Java代码
android:fromDegrees="0"
android:toDegrees="360"
android:repeatCount="-1"
android:repeatCount="-1"即表示循环运行,配合上android:fromDegrees="0" android:toDegrees="360"表示不间断
3、开始和停止旋转
在操作开始之前调用
Java代码
if (operatingAnim != null) {
infoOperatingIV.startAnimation(operatingAnim);
}
在操作完成时调用
Java代码
infoOperatingIV.clearAnimation();
许多朋友不知道如何停止旋转animation,所以强制设置rotate转动多少圈表示操作,但却无法与操作实际的进度匹配上,实际上只要如上代码所示清除animation即可。
Java如何编译椭圆周长的程序
publicclassCircle{圆的半径,privatedoublemRadius,publicCircle(doublemRadius){,this.mRadius=mRadius,}获取圆的周长,publicdoublegetLength,{return2Math.PImRadius,},获取圆的面积publicdoublegetArea{returnMath.PImRadiusmRadius。这样即可在Java编译椭圆周长的程序。
JAVA 任意椭圆方向画法
使用 AffineTransform 把Ellipse2D 旋转一下就可以了。
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.Color;
import java.awt.Shape;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.File;
import javax.imageio.ImageIO;
public class DemoPNG {
public static void main(String[] args) throws Exception {
BufferedImage bi = new BufferedImage(600, 800, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.WHITE);
g2d.fillRect(0,0,600, 800);
g2d.setPaint(Color.RED);
Ellipse2D s1 = new Ellipse2D.Double(100, 100, 400, 600);
g2d.draw(s1);
AffineTransform transform = AffineTransform.getRotateInstance(Math.PI/4, 300, 400);
Shape r1 = transform.createTransformedShape(s1);
g2d.fill(r1);
AffineTransform transform2 = AffineTransform.getRotateInstance(Math.PI/2, 300, 400);
Shape r2 = transform2.createTransformedShape(s1);
g2d.draw(r2);
g2d.dispose();
ImageIO.write(bi, "PNG", new File("demo.png"));
}
}
java 椭圆算法
以下代码,将输出一个椭圆,再有问题,我可远程助你。如下:
/**
*(300,100)(400,100)
*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Lipse
{
public static void main(String[] args)
{
new MainFrame();
}
}
class MainFrame extends JFrame implements ActionListener
{
JPanel pane=new JPanel();
JTextField T_a,T_b;
JButton Draw,Show;
JLabel L_a,L_b;
int a,b;
MainFrame()
{
super("DrawLipse Window");
Container con=this.getContentPane();
con.setLayout(null);
pane.setBounds(20,20,850,550);
pane.setBackground(new Color(100,156,200));
con.add(pane);
L_a=new JLabel("请输入长半径:a");
L_a.setBounds(180,580,100,20);
con.add(L_a);
L_b=new JLabel("请输入短半径:b");
L_b.setBounds(180,630,100,20);
con.add(L_b);
T_a=new JTextField();
T_a.setBounds(300,580,50,20);
con.add(T_a);
T_b=new JTextField();
T_b.setBounds(300,630,50,20);
con.add(T_b);
Draw=new JButton("画椭圆");
Draw.setBounds(550,580,90,30);
Draw.addActionListener(this);
con.add(Draw);
Show=new JButton("显示坐标");
Show.setBounds(550,620,90,30);
Show.addActionListener(this);
con.add(Show);
this.addWindowListener(new CloseWindow());
this.setBounds(20,20,900,700);
this.setVisible(true);
this.setResizable(false);
}/*MainFrame()*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Draw)
{
a=Integer.parseInt(T_a.getText().trim());
b=Integer.parseInt(T_b.getText().trim());
Line line=new Line(this);
line.drawLipse(a,b);
}
if(e.getSource()==Show)
{
Graphics g1=this.pane.getGraphics();
g1.setColor(Color.PINK);
g1.drawLine(0,300,920,300);//----x---
g1.drawLine(410,0,410,720);//----y---
g1.dispose();
}
}/*method actionPerformed*/
}
class Line
{
MainFrame jb;
Line(MainFrame jb)
{
this.jb=jb;
}
public void drawLipse(int a,int b)
{
int x,y;
double d1,d2;
x=0; y=b;
d1=b*b+a*a*(-b+0.25);
Graphics g=jb.pane.getGraphics();
g.setColor(Color.red);
g.drawLine(x+410,y+300,x+410,y+300);
g.drawLine(-x+410,-y+300,-x+410,-y+300);
g.drawLine(-x+410,y+300,x+410,-y+300);
g.drawLine(x+410,-y+300,x+410,-y+300);
try
{
while(b*b*(x+1)a*a*(y-0.5))
{
if(d1=0)
{
d1+=b*b*(2*x+3);
x++;
}
else
{
d1+=(b*b*(2*x+3)+a*a*(-2*y+2));
x++;
y--;
}
g.drawLine(x+410,y+300,x+410,y+300);
g.drawLine(-x+410,-y+300,-x+410,-y+300);
g.drawLine(-x+410,y+300,x+410,-y+300);
g.drawLine(x+410,-y+300,x+410,-y+300);
Thread.sleep(30);
}// top of while
}catch(Exception e){}
d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
try
{
while(y0)
{
if(d2=0)
{
d2+=b*b*(2*x+2)+a*a*(-2*y+3);
x++;
y--;
}
else
{
d2+=a*a*(-2*y+3);
y--;
}
g.drawLine(x+410,y+300,x+410,y+300);
g.drawLine(-x+410,-y+300,-x+410,-y+300);
g.drawLine(-x+410,y+300,x+410,-y+300);
g.drawLine(x+410,-y+300,x+410,-y+300);
Thread.sleep(30);
}/* bottom of while*/
}catch(Exception e){}
} /*DrawLipse*/
}
class CloseWindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
关于java椭圆和java画椭圆的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。