java - 用Java绘制图形

标签 java swing graphics

我想在一个正方形的边上画一个圆,并在 java 中有多个正方形圆圈。我快完成了,但我的输出并没有像我想要的那样出现。图片是我正在尝试做的,但没有成功。

This is what I am trying to draw[1] 这是我的代码:

a.awt.*;

public class SquaredCircles {
    public static final int WIDTH=400;
    public static final int HEIGHT=400;

    public static void main (String[] args) {
        DrawingPanel panel = new DrawingPanel(WIDTH,HEIGHT);
        Graphics g = panel.getGraphics ();

        panel.setBackground(new Color(0, 255, 255 ) );

        int x = 0;
        int y = 0;
        int size = 100;
        int rows = 5;
        int numSquares = 1;

        drawManySquares ( g, numSquares, x, y, size, rows );

        x = 10;
        y = 120;
        size = 24;
        rows = 4;
        numSquares = 4;

        drawManySquares( g, numSquares, x, y, size, rows );

        x = 150;
        y = 20;
        size = 40;
        rows = 6;
        numSquares = 5;

        drawManySquares( g, numSquares, x, y, size, rows );

        x = 130;
        y = 275;
        size = 36;
        rows = 3;
        numSquares = 3;

        drawManySquares( g, numSquares, x, y, size, rows );
    }

    public static void drawManySquares( Graphics g, int numSquares, int x, int y, int size, int rows ) {
        for ( int i = 0; i < numSquares; i++ ) {
            for ( int j = 0; j < numSquares; j++ ) {
                drawOneSquare( g, x + i  size, y + j  size, size, rows );
            }
        }
    }

    public static void drawOneSquare( Graphics g, int x, int y, int size, int rows ) {
        g.setColor ( Color.GREEN);
        g.fillRect(x , y, size, size);

        g.setColor ( Color.YELLOW);
        g.fillOval ( x, y, size, size);

        g.setColor ( Color.BLACK);
        g.drawLine(size / 2, x, size / 2, size);
        g.setColor ( Color.BLACK);
        g.drawLine(x, size / 2, size, size / 2);

        for (int i = 0; i <= rows; i = i + 1) {
            g.setColor ( Color.BLACK);
            g.drawOval(x + (i* (size/rows)), y+ (i*(size/rows)), size - (i*(size/rows +10   )) , size - (i*(size/rows +10)));
        }
    }
}

最佳答案

先看看 Painting in AWT and SwingPerforming Custom Painting看看Swing应该怎么画

将您的问题分解成可管理的 block 。您需要做的第一件事是在特定位置绘制一个给定大小的圆圈,例如

Circle

public void paintCircleAt(Graphics2D g2d, int radius, int centerX, int centerY, Color stroke, Color fill) {
    Ellipse2D.Double circle = new Ellipse2D.Double(centerX - radius, centerY - radius, radius * 2, radius * 2);
    g2d.setColor(fill);
    g2d.fill(circle);
    g2d.setColor(stroke);
    g2d.draw(circle);
}

因此,这允许您围绕 x/y 的中心点绘制一个给定半径的圆,用指定的颜色填充和勾勒轮廓,非常简单。

现在,您需要以某种方式围绕同一中心点绘制一系列圆圈,例如...

Cirlces

public void paintCirclesIn(Graphics2D g2d, int count, int radius, int centerX, int centerY, Color stroke, Color fill) {

    System.out.println(radius + "; " + centerX + "; " + centerY);

    int delta = radius / count;
    int innerRadius = radius;
    for (int index = 0; index < count; index++, innerRadius -= delta) {
        paintCircleAt(g2d, innerRadius, centerX, centerY, stroke, fill);
    }

}

好的,这基本上计算了每个圆与绘制的许多圆之间的差异(增量),这些圆的半径与前一个圆的半径差异很大。由于绘画的方式,我们从外圈开始绘画。

最后,我们需要以某种方式绘制正方形和圆形,例如...

Square

public void paintCirclesInSquare(Graphics2D g2d, int count, int x, int y, int width, int height, Color squareStroke, Color squareFill, Color circleStroke, Color circleFill) {
    int centerX = x + (width / 2);
    int centerY = y + (height / 2);
    int radius = Math.min(centerX, centerY);
    Rectangle2D box = new Rectangle2D.Double(x, y, width, height);
    g2d.setColor(squareFill);
    g2d.fill(box);
    g2d.setColor(squareStroke);
    g2d.draw(box);

    paintCirclesIn(g2d, count, radius, centerX, centerY, circleStroke, circleFill);

    g2d.drawLine(centerX, y, centerX, y + height);
    g2d.drawLine(x, centerY, x + width, centerY);
}

这再次简单地重用了我们已有的现有代码并添加到其中,绘制正方形、正方形中的圆圈以及最后的线条。

现在,从这里开始,您可以编写一个方法来获取所需的列/行数、开始的 x/y 位置、每个正方形的大小、所需的圆圈数以及颜色和重用这个功能,但我会把它留给你;)

你可以玩的可运行示例...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CirclesAndSquares {

    public static void main(String[] args) {
        new CirclesAndSquares();
    }

    public CirclesAndSquares() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            int x = getWidth() / 2;
            int y = getHeight() / 2;
//          paintCircleAt(g2d, Math.min(x, y), y, y, Color.BLACK, Color.YELLOW);
//          paintCirclesIn(g2d, 5, Math.min(x, y), x, y, Color.BLACK, Color.YELLOW);
            paintCirclesInSquare(g2d, 5, 0, 0, getWidth() - 1, getHeight() - 1, Color.BLACK, Color.GREEN, Color.BLACK, Color.YELLOW);
            g2d.dispose();
        }

        public void paintCirclesInSquare(Graphics2D g2d, int count, int x, int y, int width, int height, Color squareStroke, Color squareFill, Color circleStroke, Color circleFill) {
            int centerX = x + (width / 2);
            int centerY = y + (height / 2);
            int radius = Math.min(centerX, centerY);
            Rectangle2D box = new Rectangle2D.Double(x, y, width, height);
            g2d.setColor(squareFill);
            g2d.fill(box);
            g2d.setColor(squareStroke);
            g2d.draw(box);

            paintCirclesIn(g2d, count, radius, centerX, centerY, circleStroke, circleFill);

            g2d.drawLine(centerX, y, centerX, y + height);
            g2d.drawLine(x, centerY, x + width, centerY);
        }

        public void paintCirclesIn(Graphics2D g2d, int count, int radius, int centerX, int centerY, Color stroke, Color fill) {

            System.out.println(radius + "; " + centerX + "; " + centerY);

            int delta = radius / count;
            int innerRadius = radius;
            for (int index = 0; index < count; index++, innerRadius -= delta) {
                paintCircleAt(g2d, innerRadius, centerX, centerY, stroke, fill);
            }

        }

        public void paintCircleAt(Graphics2D g2d, int radius, int centerX, int centerY, Color stroke, Color fill) {
            Ellipse2D.Double circle = new Ellipse2D.Double(centerX - radius, centerY - radius, radius * 2, radius * 2);
            g2d.setColor(fill);
            g2d.fill(circle);
            g2d.setColor(stroke);
            g2d.draw(circle);
        }

    }

}

关于java - 用Java绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663434/

相关文章:

java - 使用构造函数注入(inject)扫描 Spring 自动组件

java - 数组定义错误

java - 单击和调整大小时滚动条消失

Java 在按键上旋转椭圆

java - Android:x 秒后多次更新 gridview

java - 单个对象覆盖对象数组,不知道为什么

java - 如何在 Jframe 中显示 3d 模型

java - 如何使用 2D 数组和 JTable 在 Java 中制作棋盘?

c# - Windows 10 操作系统上的 Windows 窗体图形问题

actionscript-3 - 画一条线: is there exists a limits of thickness in Graphics. lineStyle()?