用Java生成多个形状...? [英] Generating multiple shapes in Java...?

查看:56
本文介绍了用Java生成多个形状...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成星形,三角形等多种类型的形状.我已经运行了代码,并且仅在显示一颗星的情况下进行编译和运行.(我希望大约10)在图形用户界面中我可以使用什么功能来生成多个形状??

How do I generate multiple type of shape like stars, triangles, etc..I've run the code and it compiles and runs with only showing one star. (I want around 10) what function do I use to generate multiple shapes in a Graphical User Interface..?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Shapes2JPanel extends JPanel {

    // draw general paths
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // call superclass's paintComponent
        Random random = new Random(); // get random number generator
        Graphics2D g2d = (Graphics2D) g;
        int[] xPoints = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
        int[] yPoints = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};
        GeneralPath star = new GeneralPath();
        star.moveTo(xPoints[0], yPoints[0]);
        for (int count = 1; count < xPoints.length; count++) {
            star.lineTo(xPoints[count], yPoints[count]);
        }
        star.closePath();
        g2d.translate(150, 150);
        for (int count = 1; count <= 20; count++) {
            g2d.rotate(Math.PI / 10.0);
        }
        g2d.setColor(new Color(random.nextInt(256),
                random.nextInt(256), random.nextInt(256)));
        g2d.fill(star);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Drawing 2D Shapes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Shapes2JPanel shapes2JPanel = new Shapes2JPanel();
        frame.add(shapes2JPanel); // add shapes2JPanel to frame
        frame.setBackground(Color.WHITE); // set frame background color
        frame.setSize(315, 330); // set frame size
        frame.setVisible(true); // display frame
    } // end main
} // end class Shapes2

推荐答案

您的代码仅生成一种形状.如果需要多个形状,则需要创建多个形状.所以:

Your code only generates one shape. If you want multiple shapes then you need to create multiple shapes. So:

  1. 不要在paintComponent()方法中生成形状.相反,您可能具有addStar(...),addTriangel()之类的方法.

  1. Don't generate the shapes in the paintComponent() method. Instead you might have methods like addStar(...), addTriangel().

然后创建一个ArrayList来保存形状.因此,上面的方法将创建形状,然后将其添加到ArrayList中.

Then you create an ArrayList to hold the shapes. So the method from above will create the shape and then add it to the ArrayList.

然后,paintComponent()方法将遍历ArrayList并绘制每个Shape.

Then the paintComponent() method will iterate through the ArrayList and paint each Shape.

有关基本代码,请使用形状使用上述方法.该链接还将为您提供实用程序类,以轻松创建星形"和其他有趣的形状.

Check out Playing With Shapes for the basic code for using the above approach. The link will also provide you with a utility class to easily create "star shapes" and other interesting shapes.

    for (int count = 1; count <= 20; count++) {
        g2d.rotate(Math.PI / 10.0);
    }

上面的代码不执行任何操作.它循环了20次并改变了旋转角度,但是没有画任何东西,所以只有最后一次旋转会被使用.

The above code does nothing. It loops 20 times and change the rotation but nothing is painted so only the last rotation will ever be used.

在绘画方法中不要使用random(...)方法.您无法控制Swing何时调用绘画方法,因此您的形状将保持随机变化的颜色.

Don't use the random(...) method in a painting method. You can't control when Swing will invoke the painting method, so you shapes would keep randomly changing colors.

这篇关于用Java生成多个形状...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆