簡體   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