簡體   English   中英

用Java繪制圖形

[英]Drawing graphics in Java

我試圖在一個正方形的圓邊畫一個圓圈,並在java中有多個方形圓圈。 我差不多完成了,但我的輸出並沒有像我想的那樣出來。 這張照片是我想要做的,但它沒有成功。

這就是我想要繪制的[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)));
        }
    }
}

首先看看AWT中的繪畫和Swing以及執行自定義繪畫 ,看看如何在Swing中完成繪畫

將您的問題分解為可管理的塊。 您需要做的第一件事就是在特定位置繪制一個給定大小的圓圈,例如

圈

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

}

好吧,這基本上計算了每個圓與塗料之間的差異(delta),這些塗料與前一個圓的半徑差別很大。 由於繪畫的方式,我們從外圈開始畫畫。

最后,我們需要一些方法畫一個正方形和圓形,類似......

廣場

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

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM