用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);
}
}
画椭圆可以通过画矩形及其内切椭圆实现,示例代码如纳茄闭下
如果纳斗只需要椭圆,则无需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);
}
}
展开全部
用 drawOval(int x,int y,int w,int h); x,y代表敬森坐标 w,h代表宽度和高度 比如
drawOval(50,50,50,100);这是一个椭闷悄形,
drawOval(50,50,50,50); 这是亮罩亩一个圆形.
drawOval(50,50,50,100);这是一个椭闷悄形,
drawOval(50,50,50,50); 这是亮罩亩一个圆形.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("椭圆"))
{
paintFlag=2;
}
}
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseDragged(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
if(paintFlag==1)
{
shape=new Line2D.Double(x1,y1,x2,y2);
}
else if(paintFlag==2)
{
coordinateChange();
shape=new Ellipse2D.Double(newX1,newY1,newX2-newX1,newY2-newY1);
}
else if(paintFlag==3)
{
coordinateChange();
shape=new Rectangle2D.Double(newX1,newY1,newX2-newX1,newY2-newY1);
}
((PaintPanel)paintPanel).setShape(shape);
((PaintPanel)paintPanel).repaint();
晌物 }
public void mouseMoved(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
((PaintPanel)paintPanel).addShape(shape);
((PaintPanel)paintPanel).repaint();
}
private void coordinateChange()
{
if((x1<x2) && (y1<y2))
{
newX1=x1;newY1=y1;
newX2=x2;newY2=y2;
}
if((x1<x2) && (y1>y2))
{
newX1=x1;newY1=y2;
newX2=x2;newY2=y1;
}
else if((x1>x2) && (y1>y2))
{
宴拿液newX1=x2;newY1=y2;
newX2=x1;newY2=y1;
}
else if((x1>x2) && (y1<y2))
{
newX1=x2;newY1=y1;
newX2=x1;newY2=y2;
}
} 敏判
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询