Generating multiple shapes in Java…?

你说的曾经没有我的故事 提交于 2021-02-16 15:16:05

问题


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

回答1:


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

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

  2. 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.

  3. 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);
    }

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.

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.



来源:https://stackoverflow.com/questions/32552886/generating-multiple-shapes-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!