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);
}
}
어쨌든, 내 코드를 읽을 시간을내어 주셔서 감사합니다. 고맙습니다. 저는이 문제를 해결하는 다양한 방법에 정말로 관심이 있습니다.
AHH를 살펴 보자. 그 점을 지적 해 주셔서 감사합니다. 나는 그것을 크게, 고맙다 !! – Frightlin
@ Frightlin Glad it helped;) – MadProgrammer