来自方法的多个图形对象

Multiple graphic objects from method

我有一个任务是画跳棋盘。这是我的框架class

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class AppFrame extends JFrame {
    public AppFrame() {
        setTitle("Kółko i kwadracik");
        setSize(1000, 1500);
        setLocationRelativeTo(null);
        initGUI();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void initGUI() {
        setLayout(new BorderLayout());

        JTabbedPane tabPane = new JTabbedPane();
        tabPane.addTab("Plansza", new PlanszaGry());
        tabPane.addTab("Obrazek", new PanelZObrazkiem());
        tabPane.addTab("Wykres", new Wykres());
        tabPane.addTab("Warcaby", new Warcaby());
        tabPane.addTab("4 Warcaby", new Warcaby4());

        add(tabPane, BorderLayout.CENTER);
    }
}

比 class 创建单个跳棋盘,这是一个新的 Warcaby()

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

public class Warcaby extends JPanel {
    public void paint(Graphics g) {
        super.paint(g);

        setBackground(Color.WHITE);

        Graphics2D g2 = (Graphics2D) g;

        Stroke defaultStroke = g2.getStroke();

        int y = 9; // tu ustawiamy liczbę linii (czyli w sumie wilekość planszy)

        // linie planszy do gry
        for (int i = 0; i <= y; i++) {
            float dash[] = { 10.0f };
            Stroke lineStroke = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
                    BasicStroke.JOIN_MITER);
            g2.setStroke(lineStroke);
            g2.setColor(Color.BLACK);
            int x = i * 100;
            g2.draw(new Line2D.Double(100 + x, 100, 100 + x, 100 * y));// linie
                                                                        // pionowe

            g2.draw(new Line2D.Double(100, 100 + x, 100 * y, 100 + x)); // linie
                                                                        // poziome

        }

        // Plansza do gry (czarne/białe pola)
        for (int a = 1; a < y; a++) {
            if (a % 2 != 0) {
                for (int b = 1; b < y; b++) {
                    if (b % 2 == 0) {

                        g.setColor(Color.black);
                        g.fillRect(b * 100, a * 100, 100, 100);
                    }
                }
            } else {
                for (int b = 1; b < y; b++) {
                    if (b % 2 != 0) {

                        g.setColor(Color.black);
                        g.fillRect(b * 100, a * 100, 100, 100);
                    }
                }
            }

        }
    }
}

我的下一个任务是绘制 4 个相邻的板,老师给了我一个提示,让我创建方法来绘制一个板并提供有关其位置的信息。我什至不知道如何开始。我从这个开始:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

public class Warcaby4 extends JPanel {

    public void Warcaby(Graphics g, int x, int y) {
        super.paint(g);
        setBackground(Color.WHITE);
        Graphics2D g2 = (Graphics2D) g;
        Stroke defaultStroke = g2.getStroke();
        float dash[] = { 10.0f };
        Stroke lineStroke = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER);
        g2.setStroke(lineStroke);
        g2.setColor(Color.BLACK);

        for (int i = 0; i <= y; i++) {

            x = i * 100;
            g2.draw(new Line2D.Double(100 + x, 100, 100 + x, 100 * y));// linie
                                                                        // pionowe

            g2.draw(new Line2D.Double(100, 100 + x, 100 * y, 100 + y)); // linie
                                                                        // poziome

        }

        // Plansza do gry (czarne/białe pola)
        for (int a = 1; a < y; a++) {
            if (a % 2 != 0) {
                for (int b = 1; b < y; b++) {
                    if (b % 2 == 0) {

                        g.setColor(Color.black);
                        g.fillRect(b * 100, a * 100, 100, 100);
                    }
                }
            } else {
                for (int b = 1; b < y; b++) {
                    if (b % 2 != 0) {

                        g.setColor(Color.black);
                        g.fillRect(b * 100, a * 100, 100, 100);
                    }
                }
            }

        }
    }
}

现在我不知道在哪里以及怎么叫它 4 次,我做对了吗?请给我一些建议。 :)

我自己完成了这个任务。这是代码,以防有人遇到类似问题。

public void paint(Graphics g) {
        super.paint(g);

        rysujWarcaby((Graphics) g, 50, 50);
        rysujWarcaby((Graphics) g, 50, 500);
        rysujWarcaby((Graphics) g, 500, 50);
        rysujWarcaby((Graphics) g, 500, 500);

}

public void rysujWarcaby(Graphics g, int x, int y) {
        int xx = 1;
        int nr = 9;
        setBackground(Color.YELLOW);
        Graphics2D g2 = (Graphics2D) g;
        Stroke lineStroke = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
                        BasicStroke.JOIN_MITER);
        g2.setStroke(lineStroke);
        g2.setColor(Color.BLACK);

        for (int i = 0; i < nr; i++) {

                xx = i * 50;
                g2.draw(new Line2D.Double(x + xx, y, xx + x, y + 50 * 8));

                g2.draw(new Line2D.Double(x, y + xx, x + 50 * 8, y + xx));

        }

}

My next task is to draw 4 boards next to eachother, teacher gave me a hint to create method drawing one board with information about it's position. I can't figure it out how to even start. I've start with this:

为什么不重新使用已有的组件呢?例如,使用 GridLayout 您可以根据需要创建任意数量的组件...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class AppFrame {

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

    public AppFrame() {
        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.setLayout(new GridLayout(2, 2));
                frame.add(new Warcaby());
                frame.add(new Warcaby());
                frame.add(new Warcaby());
                frame.add(new Warcaby());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Warcaby extends JPanel {

        public Warcaby() {
            setBackground(Color.WHITE);
            setBorder(new EmptyBorder(5, 5, 5, 5));
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

//            setBackground(Color.WHITE);
            Graphics2D g2 = (Graphics2D) g;

            Insets insets = getInsets();
            int horontialPadding = insets.left + insets.right;
            int verticalPadding = insets.top + insets.bottom;

            int width = getWidth() - horontialPadding;
            int height = getHeight() - verticalPadding;

            int size = Math.min(width, height) / 10;

            int xOffset = insets.left + ((width - (size * 10)) / 2);
            int yOffset = insets.top + ((height - (size * 10)) / 2);

            for (int vertical = 0; vertical < 10; vertical++) {
                for (int horzontial = 0; horzontial < 10; horzontial++) {
                    int x = horzontial * size;
                    int y = vertical * size;
                    g2.setColor(Color.WHITE);
                    if (vertical % 2 == 0) {
                        if (horzontial % 2 == 0) {
                            g2.setColor(Color.BLACK);
                        }
                    } else if (horzontial % 2 != 0) {
                        g2.setColor(Color.BLACK);
                    }
                    g2.fillRect(xOffset + x, yOffset + y, size, size);
                }
            }
            g2.setColor(Color.BLACK);
            g2.drawRect(xOffset, yOffset, size * 10, size * 10);
        }
    }
}

抱歉,我优化了您的绘图代码,现在它可以根据可用的大小进行调整space。

此外,你永远不应该在任何绘画方法中更新 UI 的状态(比如调用 setBackground),这是一个非常糟糕的主意,可能会导致无限绘画循环会消耗您的 CPU 个循环并使您的系统无法使用(是的,我已经这样做了)

根据一般经验,您应该更喜欢 paintComponent 而不是 paint。有关详细信息,请参阅 Painting in AWT and Swing and Performing Custom Painting