2014-11-12 4 views
-1

안녕하세요 그래픽 드로잉과 드로잉을 다시하고 싶지 않지만 어떻게해야할지 모르겠다. 내 패널이 그린 그래픽을 모두 삭제하고 동일한 코드로 다시 시작하기를 원합니다. 나는 여기에 게시 된 몇 가지 방법을 시도했지만 아무도 일을하지 않습니다. 토글 버튼이주어진 수 후에이 JPanel을 다시 칠하는 방법

 public class Main extends JPanel implements ActionListener { 
Timer timer; 
private double angle = 444; 
private double scale = 1; 
private double delta = 0.0001; 
RoundRectangle2D.Float r = new RoundRectangle2D.Float(); 
int counter = 0; 

public Main() { 
    timer = new Timer(55, this); 
    timer.start(); 
} 

public void paint(Graphics g) { 
    counter++; 

    int h = getHeight(); 
    int w = getWidth(); 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.setColor(new Color(randomNumber(0, 155), randomNumber(0, 255),randomNumber(0, 155), randomNumber(0, 255))); 
    drawCircles(g2d, getWidth()/2, getHeight()/2, 250); 
    if(counter > 200){ 
     g2d.clearRect (0, 0, getWidth(), getHeight()); 
     super.paintComponent(g2d); 
     counter = 0; 
    } 
} 

public int randomNumber(int min, int max) { 

    int c = new Random().nextInt((max - min) + 1); 
    return c; 
} 

public static void main(String[] args) { 

    JFrame frame = new JFrame(); 
    frame.setUndecorated(true); 

    Dimension dim = new Dimension(Toolkit.getDefaultToolkit() 
      .getScreenSize().width, Toolkit.getDefaultToolkit() 
      .getScreenSize().height); 

    frame.setSize(dim); 
    frame.setLocation(0, 0); 
    frame.setBackground(new Color(0, 0, 0, 255)); 
    frame.add(new Main()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 
void drawCircles(Graphics graphics, int xMid, int yMid, int radius) { 
    // end recursion 
    if(radius < 5) 
     return; 

    // Draw circle 

    // start recursion 
    //left 
    drawCircles(graphics, xMid-radius, yMid, radius/2); 
    ((Graphics2D) graphics).rotate(angle); 
    graphics.drawOval(xMid - radius, yMid - radius, radius * 2, radius * 2); 

    //right 
    drawCircles(graphics, xMid+radius, yMid, radius/2); 
    graphics.drawOval(xMid - radius, yMid - radius, radius * 2, radius * 2); 

    ((Graphics2D) graphics).rotate(angle); 

    ((Graphics2D) graphics).rotate(angle); 
    ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
    ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); 
} 

public void actionPerformed(ActionEvent e) { 

    if (scale < 0.01) 
     delta = -delta; 
    else if (scale > 0.99) 
     delta = -delta; 
    scale += delta; 
    angle += 0.001; 
    repaint(); 
} 
} 

답변

0

내가 완전히 이해 잘 모르겠지만, 당신이 (예를 들어)는 JToggleButton에 사용할 수는 그리기 방지 다운. 나는 당신의 drawCircles() 방법 안에이 같은 것을 볼 수 있습니다 : 당신의 예에서

void drawCircles(Graphics graphics, int xMid, int yMid, int radius) 
{ 
    if(!toggleBtn.isSelected() // the toggle button is pressed 
    { 
     // draw something 
    } 
} 

, 당신은 두 개의 원과 두 개의 타원을 그리는. 내가 너를 올바르게 이해했다면, 예를 들어, 메소드의 중간에서 멈추고 싶을 때 첫 번째 원만 그리기를 원한다. 그런 다음, 어느 시점에서 두 개의 타원과 나머지 원을 그리기를 원합니다. 불행히도, 당신은 그렇게 할 수 없습니다. 메서드 중간에 메서드를 중지 (또는 일시 중지) 할 수 없습니다.

메서드가 완료 될 때까지 실행해야합니다 (끝나거나 예외가 발생하는지 여부). 그러나 한 가지 모양 (예 : 원)을 그리는 일종의 작업을 만들 수 있습니다. 여러 작업을 만들면 많은 원을 그릴 수 있습니다. 이를 수행하려면 Concurrency 및 아마도 약 Java Tasks에 대해 알아야합니다. 이러한 작업을 일종의 순서로 실행할 수 있으며 동시성 때문에 내가 원하는 것처럼 이러한 그리기 작업을 일시 중지했다가 다시 시작할 수 있습니다.