繁体   English   中英

使用秋千绘制Java网格

[英]Drawing java grid using swing

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

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

该代码有效。

只需删除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 );   }  } 

我不确定您的问题是什么,但是您的实现略有偏离...

  • 不要从JFrame扩展,您不会在类上添加任何新功能,并且它不是执行自定义绘画的理想选择,因为它没有双重缓冲,并且在框架的表面和用户之间具有JRootPanecontentPane 同样,您冒着在框架装饰下绘画的风险。 看看我该如何设置? 以及如何调整屏幕的精确度,即使重新调整大小以获取更多详细信息。
  • 不要覆盖(或通常)顶层容器的paint ,请参阅上一点。 取而代之的是从从JPanel类扩展而来的组件开始,并改写paintComponent 同样不要忘记调用paint方法的super方法,以保持paint chain合同。 有关更多详细信息,请查看AWT中的绘画以及“摇摆执行自定义绘画”
  • 不要依赖于幻数,而是使用已知的值来决定要做什么。

格网

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

    }
}

暂无
暂无

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

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