2015-01-21 3 views
0

JPanel에 R-G-B 값을 반환하는 JSlider가 있습니다. JPanel의 Costructor에서 생성합니다. 동일한 Panel (paintComponent 사용)에 작은 원을 그려서 슬라이더를 사용하여 색상을 변경합니다. 나는 슬라이더 이동과 동시에 색이 변하기를 바래. 그럼 다시 그리는 방법을 사용합니다. 두 개의 버튼이있는 패널 옆에 다른 패널이 있습니다 .. 첫 번째 패널에서 메소드 repaint를 사용하는 경우 두 번째 패널의 버튼이 첫 번째 패널의 topLeft에 복제됩니다. 왜? 감사합니다.repaint()를 사용하는 이상한 위치 재 지정;

첫 번째 패널 :

public class OptionsPanel extends JPanel { 


static JSlider RBG = new JSlider(0,255); 
OptionsPanel(){ 

this.setVisible(false); 
this.setSize(350,1000); 
this.setLayout(null); 
this.setBackground(new Color(200,200,0)); 
Main.f1.add(this); 



RBG.setVisible(true); 
RBG.setSize(255,50); 
RBG.setLocation(30,240); 


this.add(RBG); 



LotL lotl = new LotL(); 
Button save = new Button("Save"); 

save.setVisible(true); 
save.setSize(100,40); 
save.setLayout(null); 
save.setLocation(60,300); 
save.addActionListener(lotl); 
save.setBackground(Color.yellow); 
save.identificatore=3; 
this.add(save); 







    } 


    boolean draw=false; 

    @Override 
public void paintComponent(Graphics g){ 



g.drawOval(50,100,70,70); 

g.setColor(new Color(RBG.getValue(),180,200)); 
g.fillOval(50,100,70,70); 


repaint(); 




} 
} 

, 제 2 패널 : 당신은 페인트 사슬을 끊었다

public class FirstPanel extends JPanel{ 
FirstPanel(){ 

this.setVisible(true); 
this.setSize(1000,1000); 
this.setLayout(null); 
this.setBackground(new Color(255,200,180)); 
Main.f1.add(this); 

Button start = new Button("Start Game!"); 


Button options = new Button("Options"); 
LotL LotL = new LotL(); 



start.setVisible(true); 
start.setSize(200,80); 
start.setLayout(null); 
start.setLocation(400,450); 
start.addActionListener(LotL); 
start.setBackground(Color.green); 
start.identificatore=1; 
this.add(start); 

options.setVisible(true); 
options.setSize(200,70); 
options.setLayout(null); 
options.setLocation(400,550); 
options.addActionListener(LotL); 
options.setBackground(Color.green); 
options.identificatore=2; 
this.add(options); 

} 


} 
+2

JButton 대신 Button을 사용하고 있습니다. 문제에 대한 최소한의 완전하고 실행 가능한 ** 예를 제공하면 더 많은 도움을받을 수 있습니다. –

+0

'save.identificatore = 3;'그'Button'은 무엇입니까? 이것은 당신의 커스텀 클래스입니까? 그것은 무엇을 연장합니까? –

+0

죄송합니다 이유가 있습니다. Button은 JButton의 확장 클래스입니다. 광고 속성 'id'를 추가하십시오. identificatore .. 감사합니다 MadProgrammer! 이제 모든 작품! – alessandroAmedei

답변

2

...

@Override 
public void paintComponent(Graphics g){ 

    g.drawOval(50,100,70,70); 

    g.setColor(new Color(RBG.getValue(),180,200)); 
    g.fillOval(50,100,70,70); 


    repaint(); 

} 

Graphics가 전달되는 공유 자원이다 지정된 페인트 사이클 동안 페인트 된 모든 구성 요소에 적용됩니다.

paintComponent의 작업 중 하나는 그림에 대한 Graphics 컨텍스트를 준비하지만 구성 요소 배경색으로 채우는 것입니다.

사용자 정의 페인팅을 수행하기 전에 super.paintComponent을 호출해야합니다.

@Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.drawOval(50,100,70,70); 

    g.setColor(new Color(RBG.getValue(),180,200)); 
    g.fillOval(50,100,70,70); 
} 

또한, 다시 그리기를 트리거 할 수있다, 아무도 어떤 페인트 메소드 내에서 구성 요소의 상태를 수정 직접 결코 호출해서는 안 public 될 수 paintComponent에 대한 필요가 결코, 당신은 자신을 얻을 것이다 궁극적으로 CPU를 소비하고 컴퓨터를 사용할 수 없도록 만드는 무한 루프가됩니다.

은 자세한 내용

또한 null 레이아웃을 사용하지 않아야 대한 Painting in AWT and SwingPerforming Custom Painting를 살펴 보자, 완벽한 픽셀 레이아웃은 현대적인 UI 설계 내 환상입니다. 구성 요소의 개별 크기에 영향을주는 요소가 너무 많습니다. 어느 요소도 제어 할 수없는 요소가 너무 많습니다. 스윙은 핵심 레이아웃 관리자와 함께 작동하도록 설계되었습니다. 이러한 문제를 해결하면 더 이상 문제를 해결할 필요가 없습니다.

관련 문제