2015-07-03 43 views
1

代碼:如何在隨機座標上繪製一個隨機大小的圓,以便該圓完全可見?

Random rand = new Random(); 
JPanel mainPanel; 

int randomSize = 0; 
int randomPositionX = 0; 
int randomPositionY = 0; 

final static int FRAME_HEIGHT = 500; 
final static int FRAME_WIDTH = 500; 

final static int TITLE_BAR = 30 ; 

final static int MAX_SIZE  = 100; 
final static int MIN_SIZE  = 10 ; 

/* All the below code is put into a method */ 

mainPanel = new JPanel(){ 
    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.RED); 
     g.fillOval(randomPositionY, randomPositionX, randomSize, randomSize); 
    } 
}; 

do{ 
    randomSize = rand.nextInt(MAX_SIZE) + 1; 
}while(randomSize < MIN_SIZE); 

do{ 
    randomPositionX = rand.nextInt(FRAME_WIDTH); 
    randomPositionY = rand.nextInt(FRAME_HEIGHT); 
}while((randomPositionX + randomSize > FRAME_WIDTH) || (randomPositionY + randomSize > FRAME_HEIGHT - TITLE_BAR)); 

repaint(); 

我想要的是具有隨機的尺寸,使得它應該有10的最小尺寸和圓也應該在隨機畫協調這種爲100的最大尺寸的圓在JPanel mainPanel內的圈子是完全可見的

請注意,mainPanel將被添加到大小設置爲setSize(FRAME_WIDTH, FRAME_HEIGHT);的JFrame中。

但問題是,有時,在圓的一部分是成功的一半外一半的JPanel的內:

SCREENSHOT

哪兒我去錯了嗎?

回答

3
  1. 您正在嘗試使用計算圓的位置時,當框架包括可變高標題欄和框架的鑲邊幀大小
  2. 您正在嘗試使用的幀大小時,它的無關緊要,這是你應該使用組件的大小,因爲它是mainPanel「中的座標,你要畫

所以,我怎麼解決這些問題方面?

放棄所有框架「填充」/「偏移」。 mainPanel有它自己的座標上下文(它的左上角將是0x0),它將在它的父級的座標上下文中,所以你不需要關心框架或它的插入。

簡化您的計算,例如...

int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1); 

int randomPositionX = rand.nextInt(getWidth() - randomSize); 
int randomPositionY = rand.nextInt(getHeight() - randomSize); 

應該讓你產生結果,這將不屬於可視區域外,隨着尺寸的最大範圍的當前大小容器減去所需的大小,只是要注意,如果可用的尺寸越小則MAX_SIZE,你將有問題;)

的例子生成一個新的圈子每秒

Random Circles

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

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

    public Test() { 
     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 static class TestPane extends JPanel { 

     public final static int MAX_SIZE = 100; 
     public final static int MIN_SIZE = 10; 
     private Rectangle bounds; 
     private Random rand; 

     public TestPane() { 
      rand = new Random(); 
      Timer timer = new Timer(1000, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 

        int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1); 

        int randomPositionX = rand.nextInt(getWidth() - randomSize); 
        int randomPositionY = rand.nextInt(getHeight() - randomSize); 

        bounds = new Rectangle(randomPositionX, randomPositionY, randomSize, randomSize); 
        repaint(); 
       } 
      }); 
      timer.start(); 
     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      if (bounds != null) { 
       g2d.setColor(Color.RED); 
       g2d.fillOval(bounds.x, bounds.y, bounds.width, bounds.height); 
      } 
      g2d.dispose(); 
     } 

    } 

} 
+1

那麼,我該如何解決這些問題呢? –

+1

@CoolGuy放棄所有框架引用並關注組件的實際大小 – MadProgrammer