方法

时间:2015-12-02 18:50:57

标签: java swing jframe graphic

我有一个绘制跳棋板的任务。这是我的框架类

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

比创建单个跳棋板的类,它是一个新的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次,我甚至做得对吗?请给我一些建议。 :)

2 个答案:

答案 0 :(得分:1)

  

我的下一个任务是在彼此旁边绘制4个板,老师给了我一个提示,创建方法绘制一个板,其中包含有关它的位置的信息。我无法弄清楚如何开始。我从这开始:

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

enter image description here

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

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

此外,你永远不应该从任何绘制方法中更新UI的状态(比如调用setBackground),这是一个非常糟糕的想法,可能导致无限的绘制循环,这将消耗你的CPU周期和使您的系统无法使用(是的,我已经完成了这个)

作为一般经验法则,您应该优先paintComponent而不是paint。有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting

答案 1 :(得分:0)

我已经成功完成了这项任务。这是代码,以防有人会有类似的问题。

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

        }

}