Java 如何在 Jpanel 上绘画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21121859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:45:10  来源:igfitidea点击:

How to paint on a Jpanel?

javaeclipseswingjpanelpaintcomponent

提问by

i am trying to add 2 different panels in a Frame. one panel adds few buttons in the frame. others frame will add a chess board into the frame. i am confused, how to draw this board on a panel. my Frame will have a board on the top and buttons at the bottom. Moreover, let me know if i am somewhere wrong in the given code can anybody help me? my Code is

我想在一个框架中添加 2 个不同的面板。一个面板在框架中添加了几个按钮。其他框架将在框架中添加一个棋盘。我很困惑,如何在面板上绘制此板。我的 Frame 顶部有一块板,底部有按钮。此外,如果我在给定的代码中出现错误,请告诉我有人可以帮助我吗?我的代码是

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    private JFrame main;
    private JPanel board;
    private JPanel buttons;
    private JButton add;
    private JButton delete;

    public Test()
    {
        main=new JFrame();
        board=new JPanel();
        buttons=new JPanel();
        add=new JButton("Add");
        delete=new JButton("Delete");
        init();
        addButtons();
    }
    public void init()
    {
        main.setSize(700,700);
        main.setVisible(true);
        main.setDefaultCloseOperation(main.EXIT_ON_CLOSE);
    }
    public void addButtons()
    {
        buttons.setSize(700,40);
        buttons.setLayout(new FlowLayout());
        buttons.add(add);
        buttons.add(delete);
        main.add(buttons,BorderLayout.SOUTH);

    }
    public void addBoxes()
    {
        // what should be my code here...??
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Test();

    }
}

采纳答案by Paul Samsotha

  1. You need a component to paint on, like a JPanel.
  2. You need to @Override its paintComponentmethod
  3. You can use a loop to paint using Graphicscontext
  4. Use a flag to alternate between colors.
  1. 您需要一个要在其上绘画的组件,例如JPanel.
  2. 你需要@Override它的paintComponent方法
  3. 您可以使用循环来使用Graphics上下文进行绘制
  4. 使用标志在颜色之间交替。

Take a look at some Painting Graphics tutorials

看看一些绘画图形教程

In the mean time, give this a whirl

与此同时,试一试

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Board extends JPanel {

    private static final int DIM_WIDTH = 640;
    private static final int DIM_HEIGHT = 640;
    private static final int SQ_SIZE = 80;

    boolean black = true;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < DIM_HEIGHT; i += SQ_SIZE) {
            if (black) {
                black = false;
            } else {
                black = true;
            }
            for (int j = 0; j < DIM_WIDTH; j += SQ_SIZE) {
                if (black) {
                    g.setColor(Color.WHITE);
                    g.fillRect(j, i, SQ_SIZE, SQ_SIZE);
                    black = false;
                } else {
                    g.setColor(Color.BLACK);
                    g.fillRect(j, i, SQ_SIZE, SQ_SIZE);
                    black = true;
                }
            }
        }
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new Board());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize() {
        return new Dimension(DIM_WIDTH, DIM_HEIGHT);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

enter image description here

在此处输入图片说明