2014-04-17 3 views
0

TV 쇼에서 가져온 프로그램 인 THE OFFICE가 회의실에 앉아서 화면의 튀는 DVD 로고를 보면서 구석 구석을 치고 있습니다. 사각형은 가장자리를 칠 때 색이 바뀌기로되어 있습니다. 그러나 몇 가지 문제가 있습니다.사무실에서 튀는 큐브 프로그램

문제 1 : 스퀘어가 가장자리에서 튀는 경우가 있습니다. 다른 시간은 그것이 가라 앉고, 나는 이유를 이해할 수 없다.

문제 2 : 가장자리에 부딪 힐 때 사각형의 색을 변경하는 방법을 모르겠습니다.

3 호 : JFRAME 전체 화면을 만드는 방법을 배우려고합니다. 그리고 내 화면의 전체 화면이 아니라 다른 사람의 전체 화면.

코드는보다 쉽게 ​​읽을 수 있도록 온라인 IDE에 게시되었습니다. 찾을 수 있습니다 HERE

그렇지 않으면 그 링크에 너무 바빠서. 여기 아래에 게시됩니다.

import java.util.Random; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class BouncingMischievousSquare extends JPanel implements ActionListener { 

private static final int SQUARE_SIZE = 40; 
private static final int SPEED_OF_SQUARE = 6; 
private int xPosit, yPosit; 
private int xSpeed, ySpeed; 

BouncingMischievousSquare(){ 
    //speed direction 
    xSpeed = SPEED_OF_SQUARE; 
    ySpeed = -SPEED_OF_SQUARE; 
    //a timer for repaint 
    //http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html 
    Timer timer = new Timer(100, this); 
    timer.start(); 
} 
public void actionPerformed(ActionEvent e){ 
    //Screensize 
    int width = getWidth(); 
    int height = getHeight(); 
    xPosit += xSpeed; 
    yPosit += ySpeed; 
    //test xAxis 
    if(xPosit < 0){ 
     xPosit = 0; 
     xSpeed = SPEED_OF_SQUARE; 
    } 
    else if(xPosit > width - SQUARE_SIZE){ 
     xPosit = width - SQUARE_SIZE; 
     xSpeed = -SPEED_OF_SQUARE; 
    } 
    if(yPosit < 0){ 
     yPosit = 0; 
     ySpeed = SPEED_OF_SQUARE; 
    } 
     else if(yPosit > height - SQUARE_SIZE){ 
     xPosit = height - SQUARE_SIZE; 
     xSpeed = -SPEED_OF_SQUARE; 
     } 
    //ask the computer gods to redraw the square 
    repaint(); 
} 
public void paintComponent(Graphics g){  
    super.paintComponent(g); 
    g.fillRect(xPosit, yPosit, SQUARE_SIZE, SQUARE_SIZE); 
} 
} 

메인 클래스

import javax.swing.*; 



public class MischievousMain { 
public static void main(String[] args) { 
    JFrame frame = new JFrame("Bouncing Cube"); 
    frame.setSize(500, 500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // mischievous square input 
    frame.add(new BouncingMischievousSquare()); 
    frame.setVisible(true); 
} 



} 

어쨌든, 내 코드를 읽을 시간을내어 주셔서 감사합니다. 고맙습니다. 저는이 문제를 해결하는 다양한 방법에 정말로 관심이 있습니다.

답변

5

문제 1 : 스퀘어가 가장자리에서 튀는 경우가 있습니다. 다른 시간은 가라 앉고, 나는 이유를 이해할 수 없다.

당신이 자신을 싫어 하겠지만,

} else if (yPosit > height - SQUARE_SIZE) { 
    xPosit = height - SQUARE_SIZE; 
    xSpeed = -SPEED_OF_SQUARE; 
} 

될해야 ...

} else if (yPosit > height - SQUARE_SIZE) { 
    yPosit = height - SQUARE_SIZE; 
    ySpeed = -SPEED_OF_SQUARE; 
} 

당신은 xPosyitxSpeed 대신 yPosyitySpeed ...

을 사용하고

문제 2 : 이 가장자리를 칠 때 사각형의 색을 변경하는 방법을 모르겠습니다. 당신이 가장자리 충돌과 변화 방향, 간단한 변경 뭔가 다른 패널의 전경 색을 감지 할 때마다 기본적으로

, ...

이것은 당신이 무작위로 선택할 수있는 색상의 목록을 가지고해야 할 수도 또는 단순히 무작위로 색상 당신의 paintComponent 방법 그리고

, 당신은 RECT을 채우기 전에 간단한 사용 g.setColor(getForeground()) ...

... 추신 ... 생성

삶을 편하게하려면 무작위 색상을 생성하거나 전경색을 임의 색상으로 설정하는 메소드를 작성하면됩니다 (예 : ...).

protected void randomiseColor() { 

    int red = (int) (Math.round(Math.random() * 255)); 
    int green = (int) (Math.round(Math.random() * 255)); 
    int blue = (int) (Math.round(Math.random() * 255)); 

    setForeground(new Color(red, green, blue)); 

} 

문제 세 : 나는 JFrame의 전체 화면을 만드는 방법을 배우려고 노력하고있다. 그리고 내 화면의 전체 화면이 아니라 다른 사람의 화면입니다.

Full-Screen Exclusive Mode API

+0

AHH를 살펴 보자. 그 점을 지적 해 주셔서 감사합니다. 나는 그것을 크게, 고맙다 !! – Frightlin

+0

@ Frightlin Glad it helped;) – MadProgrammer