使用swing绘制java网格

时间:2015-12-02 06:26:10

标签: java swing

我想使用java绘制网格(10x10),但我们必须实现 它在drawRectMethod中使用JFrame,这是我到目前为止的程序

import java.awt.*;
import javax.swing.*;

public class Grid extends JFrame {

    public Grid() {
        setSize(500, 500);
        setVisible(true);
    }

    // draw grid
    public void paint(Graphics g) {
        for (int x = 30; x <= 300; x += 30)
            for (int y = 30; y <= 300; y += 30)
                g.drawRect(x, y, 30, 30);
    }

    public static void main(String args[]) {
        Grid application = new Grid();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2 个答案:

答案 0 :(得分:1)

此代码正常运作。

只需删除25

import java.awt.*;
 import javax.swing.*;
 public class Grid extends JFrame {

 public Grid()    {       
 setSize( 500, 500 );
 setVisible( true );   
 } 
public void paint( Graphics g )    
 {  
 for ( int x = 30; x <= 300; x += 30 )
 for ( int y = 30; y <= 300; y += 30 ) 
 g.drawRect( x, y, 30, 30 );

 } 
 public static void main( String args[] ) 
 {
     Grid application = new Grid();
 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   }  } 

答案 1 :(得分:1)

我不确定你的问题是什么,但你的实施稍微偏离......

  • 不要从JFrame延伸,您不会在课程中添加任何新功能,并且它不适合执行自定义绘画,因为它不是不是双缓冲的,它在帧的表面和用户之间有JRootPanecontentPane。此外,您还有在框架装饰下绘画的风险。有关详细信息,请查看How can I set in the midst?How to get the EXACT middle of a screen, even when re-sized
  • 不要覆盖顶级容器的paint(或通常),请参阅前一点。相反,从一个类似于JPanel和覆盖paintComponent的组件开始。另外,不要忘记调用绘制方法super方法以保持绘画链合同。有关详细信息,请查看Painting in AWT and SwingPerforming Custom Painting
  • 不要依赖幻数,而是使用已知值来决定你想做什么。

Grid

import java.awt.*;
import javax.swing.*;

public class Grid {

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

    public Grid() {
        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.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int size = Math.min(getWidth() - 4, getHeight() - 4) / 10;
            int width = getWidth() - (size * 2);
            int height = getHeight() - (size * 2);

            int y = (getHeight() - (size * 10)) / 2;
            for (int horz = 0; horz < 10; horz++) {
                int x = (getWidth() - (size * 10)) / 2;
                for (int vert = 0; vert < 10; vert++) {
                    g.drawRect(x, y, size, size);
                    x += size;
                }
                y += size;
            }
            g2d.dispose();
        }

    }
}