黑马程序员技术交流社区
标题:
java绘图
[打印本页]
作者:
珊湖礁
时间:
2013-12-5 10:43
标题:
java绘图
本帖最后由 珊湖礁 于 2013-12-5 16:51 编辑
我看了一下,java里面的绘图
import java.awt.Graphics;
import java.swing.JApplet;
public class CircleApplet extends JApplet
{
public void paint(Graphics s)
{
for(int i=0;i<=15;i++)
s.drawOval(250-2*i,50-2*i,30+4*i,30+4*i);
}
}//这个代码可以画一个圆环,我现在想知道用什么方法可以画一个正六边形,请大家各显神通
作者:
王家胜
时间:
2013-12-5 15:56
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
public class nomain extends JFrame
{
private static final int x=200;
private static final int y=50;
private static final int width=300;
public void drawMyPic()
{
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public void paint(Graphics g)
{
Shape l1=new Line2D.Double(x, y, x+width, y);
Shape l2=new Line2D.Double(x+width, y, x+width+width/2, y+(width/2)*Math.sqrt(3));
Shape l3=new Line2D.Double(x+width+width/2, y+(width/2)*Math.sqrt(3), x+width, y+2*((width/2)*Math.sqrt(3)));
Shape l4=new Line2D.Double( x+width, y+2*((width/2)*Math.sqrt(3)), x, y+2*((width/2)*Math.sqrt(3)));
Shape l5=new Line2D.Double(x, y+2*((width/2)*Math.sqrt(3)),(x-width/2), y+(width/2)*Math.sqrt(3));
Shape l6=new Line2D.Double((x-width/2), y+(width/2)*Math.sqrt(3), x, y);
Graphics2D gr=(Graphics2D)g;
gr.draw(l1);
gr.draw(l2);
gr.draw(l3);
gr.draw(l4);
gr.draw(l5);
gr.draw(l6);
}
public static void main(String[] args)
{
nomain m=new nomain();
m.drawMyPic();
}
}
复制代码
注意这个画的是标注正六边形,如果显示不是标准正六边形很正常,就算法而言是是六边形
作者:
王家胜
时间:
2013-12-5 15:59
这是我首次写这些代码以前见都没见过,有点吃力
作者:
大雄9293
时间:
2013-12-5 16:15
import javax.swing.*;
import java.awt.*;
public class DrawPolygon extends JFrame{
public DrawPolygon(){
setTitle("DrawPolygon");
add(new PolygonsPanel());
}
public static void main(String [] args){
DrawPolygon frame = new DrawPolygon();
frame.setLocationRelativeTo(null);
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class PolygonsPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
int xCenter = getWidth()/2;
int yCenter = getHeight()/2;
int radius = (int)(Math.min(getWidth(),getHeight())*0.4);
Polygon polygon = new Polygon();
polygon.addPoint(xCenter + radius ,yCenter);
polygon.addPoint((int)(xCenter+radius *Math.cos(2*Math.PI/6)),(int)(yCenter-radius *Math.sin(2*Math.PI/6)));
polygon.addPoint((int)(xCenter+radius*Math.cos(2*2*Math.PI/6)),(int)(yCenter-radius *Math.sin(2*2*Math.PI/6)) );
polygon.addPoint((int)(xCenter+radius*Math.cos(3*2*Math.PI/6)),(int)(yCenter-radius*Math.sin(3*2*Math.PI/6)));
polygon.addPoint((int)(xCenter+radius * Math.cos(4*2*Math.PI/6)),(int)(yCenter - radius *Math.sin(4*2*Math.PI/6)));
polygon.addPoint((int)(xCenter+radius *Math.cos(5*2*Math.PI/6)),(int)(yCenter - radius *Math.sin(5*2*Math.PI/6)));
g.drawPolygon(polygon);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2