2014-11-01 2 views
-1

저는 두 개의 2D 원을 서로 겹쳐서 쓰려고하는 초보 프로그래머입니다. 각 원의 크기가 다르므로 xCenter 및 yCenter를 추가하거나 축소하면 축의 모양이 이동하지만 축의 중심이 확실하다는 것을 어떻게 알 수 있습니까? 불행하게도 나는 초심자로서 많은 도구가 없기 때문에 가능한 가장 간단한 정보를 매우 높이 평가할 것입니다. 고맙습니다!Java에서 2D 모양 맞추기

import java.awt.Graphics; 
import java.awt.Color; 
import java.awt.Container; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class RedCross extends JPanel 
{ 
    public void paintComponent(Graphics g) 
    { 
    super.paintComponent(g); 
    int xCenter = getWidth()/2; 
    int yCenter = getHeight()/2; 
    g.setColor(Color.BLUE); 
    g.fillOval(xCenter, yCenter, 40, 40); 
    g.setColor(Color.RED); 
    g.fillOval(xCenter, yCenter, 10, 10); 
    } 
    public static void main(String[] args) 
    { 
    JFrame window = new JFrame("Target"); 
    window.setBounds(300, 300, 200, 200); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    RedCross panel = new Target(); 
    panel.setBackground(Color.WHITE); 
    Container c = window.getContentPane(); 
    c.add(panel); 
    window.setVisible(true); 
    } 
} 

답변

0

객체의 중심은 코드에서 window/2 - self/2
입니다 :

int xPanel = getWidth(); 
int yPanel = getHeight(); 

int sizeCircleOne = 40; 
int sizeCircleTwo = 10; 

// Drawing circle 1 
g.setColor(Color.BLUE); 
g.fillOval(
xPanel/2 - sizeCircleOne/2, 
yPanel/2 - sizeCircleOne/2, 
sizeCircleOne, 
sizeCircleOne 
); 

// Drawing circle 2 
g.setColor(Color.RED); 
g.fillOval(
xPanel/2 - sizeCircleTwo/2, 
yPanel/2 - sizeCircleTwo/2, 
sizeCircleTwo, 
sizeCircleTwo 
); 

가 작동하는지 알려주세요.

해피 코딩 :) -Charlie

0

원을 가운데 맞춤하는 방법은 개체를 가운데 맞춤하는 방법입니다. 너비의 반, 높이의 반에 점을 찍으십시오. 이것이 당신의 중심입니다. 이것은 원의 중심이 중심점에서 등거리에있는 원상의 모든 점을 갖기 때문입니다. 그것은 원의 정의입니다.