JAVA 任意椭圆方向画法

JAVA 任意椭圆方向画法,第1张

使用 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"))

    }

}

以下代码,将输出一个椭圆,再有问题,段液我可远程助你。如下:

/**

 *(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(y>0)

      {

     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)

 }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12500286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存