Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使用以下钢笔类和画布类在Java中绘制螺旋和多边形

如何使用以下钢笔类和画布类在Java中绘制螺旋和多边形
EN

Stack Overflow用户
提问于 2015-12-22 07:40:53
回答 1查看 2.2K关注 0票数 0

我试图绘制一个多边形,其中用户指定的边数和螺旋使用以下类。我有一个正方形的工作,但不知道如何绘制多边形。这是一个类,它创建一个可用于绘制事物的钢笔。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.awt.Color;
import java.util.Random;
public class Pen
{
    // constants for randomSquiggle method
    private static final int SQIGGLE_SIZE = 40;
    private static final int SQIGGLE_COUNT = 30;

    private int xPosition;
    private int yPosition;
    private int rotation;
    private Color color;
    private boolean penDown;
    private Canvas canvas;
    private Random random;
    /**
     * Create a new Pen with its own canvas. The pen will create a new canvas for 
     * itself to draw on, and start in the default state (centre of canvas, direction
     * right, color black, pen down).
     */
    public Pen()
    {
        this (280, 220, new Canvas("My Canvas", 560, 440));
    }
    /**
     * Create a new Pen for a given canvas. The direction is initially 0 (to the right),
     * the color is black, and the pen is down.
     *
     * @param xPos  the initial horizontal coordinate of the pen
     * @param yPos  the initial vertical coordinate of the pen
     * @param drawingCanvas  the canvas to draw on
     */
    public Pen(int xPos, int yPos, Canvas drawingCanvas)
    {
        xPosition = xPos;
        yPosition = yPos;
        rotation = 0;
        penDown = true;
        color = Color.BLACK;
        canvas = drawingCanvas;
        random = new Random();
    }
    /**
     * Move the specified distance in the current direction. If the pen is down, 
     * leave a line on the canvas.
     * 
     * @param distance  The distance to move forward from the current location.
     */
    public void move(int distance)
    {
        double angle = Math.toRadians(rotation);
        int newX = (int) Math.round(xPosition + Math.cos(angle) * distance);
        int newY = (int) Math.round(yPosition + Math.sin(angle) * distance);

        moveTo(newX, newY);
    }
    /**
     * Move to the specified location. If the pen is down, leave a line on the canvas.
     * 
     * @param x   The x-coordinate to move to.
     * @param y   The y-coordinate to move to.
     */
    public void moveTo(int x, int y)
    {
        if (penDown) {
            canvas.setForegroundColor(color);
            canvas.drawLine(xPosition, yPosition, x, y);
        }
        xPosition = x;
        yPosition = y;
    }
    /**
     * Turn the specified amount (out of a 360 degree circle) clockwise from the current 
     * rotation.
     * 
     * @param degrees  The amount of degrees to turn. (360 is a full circle.)
     */
    public void turn(int degrees)
    {
        rotation = rotation + degrees;
    }
    /**
     * Turn to the specified direction. 0 is right, 90 is down, 180 is left, 270 is up.
     * 
     * @param angle  The angle to turn to.
     */
    public void turnTo(int angle)
    {
        rotation = angle;
    }
    /**
     * Set the drawing color.
     * 
     * @param newColor  The color to use for subsequent drawing operations.
     */
    public void setColor(Color newColor)
    {
        color = newColor;
    }
    /**
     * Lift the pen up. Moving afterwards will not leave a line on the canvas.
     */
    public void penUp()
    {
        penDown = false;
    }
    /**
     * Put the pen down. Moving afterwards will leave a line on the canvas.
     */
    public void penDown()
    {
        penDown = true;
    }*

这是我从Pen类调用方法的类,这也是我试图创建一个方法的类,它绘制一个由用户指定边数的多边形,以及一个像这个spiral一样的方形的螺旋。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 import java.awt.Color;
    import java.util.Random;


    public class DrawDemo
    {
        private Canvas myCanvas;
        private Random random;

        /**
         * Prepare the drawing demo. Create a fresh canvas and make it visible.
         */
        public DrawDemo()
        {
            myCanvas = new Canvas("Drawing Demo", 500, 400);
            random = new Random();
        }

        /**
         * Draw a square on the screen.
         */
        public void drawSquare()
        {
            Pen pen = new Pen(320, 260, myCanvas);
            pen.setColor(Color.BLUE);

            square(pen);
        }

           /**
         * Draw a Triangle on the screen.
         */
        public void drawTriangle()
        {
            Pen pen = new Pen(320, 260, myCanvas);
            pen.setColor(Color.GREEN);

            triangle(pen);
        }

            /**
         * Draw a Pentagon on the screen.
         */
        public void drawPentagon()
        {
            Pen pen = new Pen(250, 200, myCanvas);
            pen.setColor(Color.GREEN);

            pentagon(pen);
        }

        /**
         * Draw a wheel made of many squares.
         */
        public void drawWheel()
        {
            Pen pen = new Pen(250, 200, myCanvas);
            pen.setColor(Color.RED);

            for (int i=0; i<36; i++) {
                square(pen);
                pen.turn(10);
            }
        }

        /**
         * Draw a square in the pen's color at the pen's location.
         */
        private void square(Pen pen)
        {
            for (int i=0; i<4; i++) {
                pen.move(100);
                pen.turn(90);
            }
        }

          /**
         * Draw a triangle in the pen's color at the pen's location.
         */
        private void triangle(Pen pen)
        {
            for (int i=0; i<3; i++) {
                pen.move(100);
                pen.turn(120);
            }
        }

            private void pentagon(Pen pen)
        {
            for (int i=0; i<5; i++) {
                pen.move(100);
                pen.turn(-72);
            }
        }

        /**
         * Draw some random squiggles on the screen, in random colors.
         */
        public void colorScribble()
        {
            Pen pen = new Pen(250, 200, myCanvas);

            for (int i=0; i<10; i++) {
                // pick a random color
                int red = random.nextInt(256);
                int green = random.nextInt(256);
                int blue = random.nextInt(256);
                pen.setColor(new Color(red, green, blue));

                pen.randomSquiggle();
            }
        }

        /**
         * Clear the screen.
         */
        public void clear()
        {
            myCanvas.erase();
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-22 08:41:57

波利根:你是说正多边形吗?你需要转向的角度是180°-(((n-2)×180)°)/n,它将减少到(360°)/n,其中n是边的数目。

螺旋:

(最后的视觉解释图片)

您需要一个initial lengthi,它是您画的第一行的长度,w是线条之间的空格。

从左上角开始。在i小于或等于0之前执行以下操作

  • 移动i
  • 90°
  • 移动i
  • 90°
  • i = i - w

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34419435

复制
相关文章
小知识:在ubuntu安装mongo db
安装 cd /home/user1 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.1.tgz tar -xzvf mongodb-linux-x86_64-3.0.1.tgz/ export PATH=$PATH:/home/user1/mongodb-linux-x86_64-3.0.1/bin 绿色软件。哈哈。 运行服务 mkdir /home/user1/data # 也许要加 --smallfiles mongod --
超级大猪
2019/11/22
7570
在https中传递查询字符串的安全性
译者:java达人-卍极客 英文链接: http://blog.httpwatch.com/2009/02/20/how-secure-are-query-strings-over-https/(点击
java达人
2018/01/31
2.2K0
在https中传递查询字符串的安全性
【DB笔试面试472】分区表在查询时如何优化?
(2) 如果需要查询2013年3月份的数据,那么请问SQL语句怎么写?要求单分区查询,且利用到CREATED列的索引。
AiDBA宝典
2019/09/30
6380
快速学习-Mongo DB简介
排序(sort) • 在 MongoDB 中使用 sort() 方法对数据进行排序,可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序
cwl_java
2020/04/16
1.2K0
Mongo查询语句
db.qiche.find({"trasferStatus":{$ne:1}}).count(); db.qiche.find({}).count(); 120.27.195.31
week
2018/08/24
1.2K0
mongo 慢查询配置
首先满查询针对的不一定是查询,增删改查都包括,因此,可以理解为一个事务的时间只有超过我们设定的时间(比如100ms)才会打印到mongo日志中,即(master.log,slave.log)。
全栈程序员站长
2022/08/09
1.2K0
Oracle——无法在查询中执行 DML 操作
create or replace function test_f(id varchar2) return varchar2 is Result varchar2(100); begin insert into sfcs_temp_17109 (sn)values(id);
_一级菜鸟
2019/09/10
4.2K0
Oracle——无法在查询中执行 DML 操作
执行Hive查询时出现OOM
使用的是缺省参数每个task分配200M内存「mapred.child.java.opts」
WHYBIGDATA
2023/01/31
9500
执行Hive查询时出现OOM
java mongo 查询统计 distinct
CommandResult result = mongoTemplate.getDb().command(
艳艳代码杂货店
2021/10/27
9680
【DB笔试面试596】在Oracle中,什么是执行计划?
执行计划指示Oracle如何获取和过滤数据、产生最终结果集,这是影响SQL语句执行性能的关键因素。在深入了解执行计划之前,首先需要知道执行计划是在什么时候产生的,以及如何让SQL引擎为语句生成执行计划。
AiDBA宝典
2019/09/29
4460
mongo DB的一般操作
最近接触了一些mongoDB 。将一些指令操作记录下来,便于查询和使用 登录 [root@logs ~]# mongo -u loguser -p log123456 --authenticationDatabase admin MongoDB shell version: 2.4.10 connecting to: test > show users > post = {"title":"My Blog Post","Content":"Here is my blog Post.","Dat
用户1217611
2018/01/30
6640
mongo DB的一般操作
[root@logs ~]# mongo -u loguser -p log123456 –authenticationDatabase admin MongoDB shell version: 2.4.10 connecting to: test > show users > post = {“title”:”My Blog Post”,”Content”:”Here is my blog Post.”,”Date”:new Date()} { “title” : “My Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:12:03.061Z”) }
全栈程序员站长
2022/07/11
4360
Debug EOS:nodeos + mongo_db_plugin
nodeos开始运行前,要先使用项目的总CmakeList.txt配置,这里我配置了boost库的位置,如果你配置了boost的环境变量可以跳过这里。
文彬
2018/09/19
2.1K0
Debug EOS:nodeos + mongo_db_plugin
理解JavaScript 中的执行上下文和执行栈
执行栈,也叫调用栈,具有 LIFO(后进先出)结构,用于存储在代码执行期间创建的所有执行上下文。
挨踢小子部落阁
2023/03/16
4080
理解JavaScript 中的执行上下文和执行栈
【DB笔试面试601】在Oracle中,给出下面执行计划的执行顺序。
分析:采用最右最上最先执行的原则看层次关系,在同一级如果某个动作没有子ID,那么就最先执行,首先,6、7、9、13最右,所以,6,7最先执行做HASH JOIN,为6,7,5。
AiDBA宝典
2019/09/29
5130
【DB笔试面试613】在Oracle中,和子查询相关的查询转换有哪些?
和NOT EXISTS类似,也选择了哈希连接,只不过是HASH JOIN ANTI NA。这里的NA,实际表示Null-Aware的意思,在11g及以后的版本中,Oracle增加了对空值敏感的反关联的支持。
AiDBA宝典
2019/09/29
4.6K0
【DB笔试面试612】在Oracle中,查询转换包含哪些类型?
在Oracle数据库中,用户发给Oracle让其执行的目标SQL和Oracle实际执行的SQL有可能是不同的,这是因为Oracle可能会对执行的目标SQL做等价改写,即查询转换。查询转换(Query Transformation),也叫逻辑优化(Logical Optimization),又称为查询改写(Query Rewrite)或软优化,即查询转换器在逻辑上对语句做一些语义等价转换,它是Oracle在解析目标SQL的过程中的非常重要的一步。查询转换能使优化器将目标SQL改写成语义上完全等价的SQL语句但生成的执行计划效率更高。
AiDBA宝典
2019/09/29
1.3K0
【DB笔试面试612】在Oracle中,查询转换包含哪些类型?
让Mongo在Spring中跑起来
本文标题为《让Mongo在Spring中跑起来》,旨在Spring中如何成功连接MongoDB并对其进行增删改查等操作,由于笔者也是刚接触,对其中的一些原由也不甚了解,若有错误之处,敬请指正。
用户1148394
2019/01/07
8240
jsp中在href中传递参数
<% Configuration conf = new Configuration(); URI uri = new URI("hdfs://192.168.0.52:9010"); FileSystem fileSystem = FileSystem.get(uri, conf); //System.out.println("Hdfs directory is"+"\n"); Path src1 = new Path("hdfs://192.168.0.52:9
闵开慧
2018/03/30
4.7K0
点击加载更多

相似问题

我的壳里是什么?

13

我是在什么壳里?

82

为什么“猫”不在我造的C壳里工作?

12

我的数据库里有有趣的人物

22

我如何设置关于疯狂商业的电子邮件?

23
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文