简体   繁体   English

如何在Jpanel上绘画?

[英]How to paint on a Jpanel?

i am trying to add 2 different panels in a Frame. 我试图在一个框架中添加2个不同的面板。 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 我的密码是

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

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

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM