为网格创建一个绘制矩形(用黑色填充)的Java函数 [英] Creating a draw rectangle (filled with black color) function in Java for a grid

查看:243
本文介绍了为网格创建一个绘制矩形(用黑色填充)的Java函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序中创建了一个网格。下面是用于创建网格的代码。

  import java.awt.Graphics; 

import javax.swing.JComponent;
import javax.swing.JFrame;
$ b $ class类Grid继承JComponent {
public void paint(Graphics g){
g.drawRect(10,10,800,500); (int i = 10; i <= 800; i + = 10)


g.drawLine(i,10,i,510); (int i = 10;i≤500; i + = 10)


g.drawLine(10,i,810,i);



public class CoreControl {

public static void main(String [] a){
JFrame window = new JFrame ();
window.setSize(840,560);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane()。add(new Grid());
window.setVisible(true);
}

}

我想要做的是创建一个函数,该函数将根据我给出的坐标绘制一个矩形(填充黑色)。基本上我想用黑色填充网格的某些单元格,我的想法是在单元格坐标上绘制黑色填充的矩形。我试图制作另一个名为drawRectangle的类,并在主函数中调用它,就像window.getContentPane()。add(new drawRectangle())一样, );然而,这并没有工作(只有drawRectangle显示,而不是网格)。

我也想重复使用这个函数来保持创建矩形。



如何创建这个函数?

另外如果你知道一个更好的方法,我应该构造这个程序请让我知道(我是新来的Java,所以我打开任何建议)。

解决方案


  1. 不要使用 paint ,使用 paintComponent 并且不要忘记调用 super .paintComponent

  2. JComponent 可能不是最佳选择, JPanel 可能是更好的选择

  3. >

您可以查看执行自定义绘画 2D图形为mo详细信息。



我建议不要试图让第二个组件执行填充。只需在网格类中提供一个方法,该方法提供单元格的x / y位置(以网格方式)并填充 paintComponent 方法中的单元格



以示例更新


  import java.awt.Color; 
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CoreControl {

public static class Grid extends JPanel {

private List< Point> fillCells;

public Grid(){
fillCells = new ArrayList<>(25);


@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(Point fillCell:fillCells){
int cellX = 10 +(fillCell.x * 10);
int cellY = 10 +(fillCell.y * 10);
g.setColor(Color.RED);
g.fillRect(cellX,cellY,10,10);
}
g.setColor(Color.BLACK);
g.drawRect(10,10,800,500); (int i = 10; i <= 800; i + = 10)

{
g.drawLine(i,10,i,510);


$ b(int i = 10; i <= 500; i + = 10){
g.drawLine(10,i,810,i);



public void fillCell(int x,int y){
fillCells.add(new Point(x,y));
repaint();



$ b public static void main(String [] a){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
}

Grid grid = new Grid();
JFrame window = new JFrame();
window.setSize(840,560);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(grid);
window.setVisible(true);
grid.fillCell(0,0);
grid。 fillCell(79,0);
grid.fillCell(0,49);
grid.fillCell(79,49);
grid.fil lCell(39,24);
}
});
}
}


I have created a grid in my program. Below is the code used to create the grid.

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

class Grid extends JComponent {
    public void paint(Graphics g) {
        g.drawRect (10, 10, 800, 500);    

        for (int i = 10; i <= 800; i+= 10)
            g.drawLine (i, 10, i, 510);

        for (int i = 10; i <= 500; i+= 10)
            g.drawLine (10, i, 810, i);
    }
}

public class CoreControl {

    public static void main(String[] a) {
        JFrame window = new JFrame();
        window.setSize(840,560);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(new Grid());
        window.setVisible(true);
    }

}

What I want to do is to create a function which will draw a rectangle (filled with black color) based on the coordinates that I give it. Basically I want to populate certain cells of the grid with black color and my idea is to draw black filled rectangles on the cell coordinates. How do I make this function?

I tried making another class called drawRectangle and called it in the main function like so window.getContentPane().add(new drawRectangle()); however that did not work (only drawRectangle shows up and not the grid).

I also want to be able to use this function repeatedly to keep creating rectangles.

How do I do create this function?

Also if you know a better way that I should structure this program please let me know (I am new to Java so I am open to any suggestions).

解决方案

  1. Don't use paint, use paintComponent and don't forget to call super.paintComponent
  2. JComponent may not be the best choice, JPanel is probably a better choice
  3. What's wrong with Graphics#fillRect(int, int, int, int)?

You might to take a look at Performing Custom Painting and 2D Graphics for more details.

I'd advice against trying to have a second component to performing the filling. Simply provide a method in you grid class that supplies the cell's x/y position (in grid terms) and fill the cell within the paintComponent method

Updated with example

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CoreControl {

    public static class Grid extends JPanel {

        private List<Point> fillCells;

        public Grid() {
            fillCells = new ArrayList<>(25);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Point fillCell : fillCells) {
                int cellX = 10 + (fillCell.x * 10);
                int cellY = 10 + (fillCell.y * 10);
                g.setColor(Color.RED);
                g.fillRect(cellX, cellY, 10, 10);
            }
            g.setColor(Color.BLACK);
            g.drawRect(10, 10, 800, 500);

            for (int i = 10; i <= 800; i += 10) {
                g.drawLine(i, 10, i, 510);
            }

            for (int i = 10; i <= 500; i += 10) {
                g.drawLine(10, i, 810, i);
            }
        }

        public void fillCell(int x, int y) {
            fillCells.add(new Point(x, y));
            repaint();
        }

    }

    public static void main(String[] a) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Grid grid = new Grid();
                JFrame window = new JFrame();
                window.setSize(840, 560);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                window.add(grid);
                window.setVisible(true);
                grid.fillCell(0, 0);
                grid.fillCell(79, 0);
                grid.fillCell(0, 49);
                grid.fillCell(79, 49);
                grid.fillCell(39, 24);
            }
        });
    }
}

这篇关于为网格创建一个绘制矩形(用黑色填充)的Java函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆