캔버스 Jpanel에 문제가 있으며 최적화 된 솔루션이 필요합니다. 이 포럼의 게시물에서이 디자인을 얻었습니다.애니메이션 용 프레임에서 Canvas 및 Panel 렌더링 문제가 발생했습니다.
하나 캔버스 (BallPanel)는 애니메이션, 하나의 프레임을위한 (주) 한 스윙 제어 (의 ControlPanel)
주요 ControlPanel에 모두를 찢는된다위한 패널이다
난 세 종류를 BallPanel, 하지만 결국, BallPanel에서 drawworld() 렌더링을 볼 수는 없지만 ControlPanel은입니다.
나는 애니메이션과 검정색 배경을 볼 수 없습니다이의 ControlPanel처럼 보인다 모든 프레임과 캔버스 (검은 색)에 어딘가에 그 아래에/뒤 또는하지만 눈에 보이지 않는
BallPanel :
public BallPanel()
{
try {
this.aBall = (BallServer) Naming
.lookup("rmi://localhost/BouncingBalls");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
box = new Box(0, 0, BOX_WIDTH, BOX_HEIGHT);
setPreferredSize(new Dimension(800, 600));
setIgnoreRepaint(true);
// Wire up Events
// MouseHandler mouseHandler = new MouseHandler();
// addMouseMotionListener(mouseHandler);
// addMouseListener(mouseHandler);
}
public void drawworld() throws RemoteException {
if (strategy == null || strategy.contentsLost())
{
// Create BufferStrategy for rendering/drawing
this.createBufferStrategy(2);
strategy = getBufferStrategy();
Graphics g = strategy.getDrawGraphics();
this.g2 = (Graphics2D) g;
}
// Turn on anti-aliasing
this.g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// box.draw(this.g2); // this can be another but it's also not working
// Render Background
this.g2.setColor(Color.BLACK);
this.g2.fillRect(0, 0, getWidth(), getHeight());
serball = aBall.getState();// other version UNcomment this line
for (int i = 0; i < currentNumBalls; i++) {
this.g2.setColor(serball[i].getBallColor(velocity.getLength()));
this.g2
.fillOval((int) (serball[i].position.getX() - serball[i]
.getRadius()),
(int) (serball[i].position.getY() - serball[i]
.getRadius()), (int) (2 * serball[i]
.getRadius()), (int) (2 * serball[i]
.getRadius()));
}
public void mainLoop() {
long previousTime = System.currentTimeMillis();
long currentTime = previousTime;
long elapsedTime;
long totalElapsedTime = 0;
int frameCount = 0;
while (true) {
currentTime = System.currentTimeMillis();
elapsedTime = (currentTime - previousTime); // elapsed time in
// seconds
totalElapsedTime += elapsedTime;
if (totalElapsedTime > 1000) {
currentFrameRate = frameCount;
frameCount = 0;
totalElapsedTime = 0;
}
try {
drawworld();
} catch (RemoteException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
previousTime = currentTime;
frameCount++;
}
}
public void start()
{
mainLoop();
}
시작 위의 메인 클래스는() 여기에서 호출 :
public static void main(String[] args)
{
JFrame frame = new JFrame("BallBounce");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
BallPanel ballCanvas = new BallPanel();
ControlPanel controlPanel = new ControlPanel(ballCanvas);
frame.getContentPane().add(ballCanvas);
frame.getContentPane().add(controlPanel);
frame.pack();
frame.setVisible(true);
ballCanvas.start();
}
이의 ControlPanel whichis가 다 같이 노력하고 있습니다 ontroller : 어떤 도움과 우리가 어떻게 사람 최적화 방법을 알 수 것을 제어/상호 작용하는 RMI 애니메이션과 스윙 제어와 같은 GUI를 처리 할 수에 대한
public class ControlPanel extends JPanel {
private BallPanel mainPanel;
private JButton resetButton;
public ControlPanel(BallPanel mainPanel)
{
this.setPreferredSize(new Dimension(200, 60));
this.setMaximumSize(new Dimension(5000, 100));
this.mainPanel = mainPanel;
....
..
..
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton source = (JButton)e.getSource();
if (source == resetButton)
{
mainPanel.clearBalls();
}
많은 감사합니다. jibby 라라
PS : 나 IM 원격 메소드를 호출하고 drawworld 렌더링 스레드 중 하나가 현탁액에가하거나 차단하는 동안 일부 스레드 문제가 생각
크로스 포스트 : http://www.coderanch.com/forums/jforum?module=posts&action=edit&post_id=2406332&start=0
내가 만든 이 타이머와 함께하지만 문제가 지속하시기 바랍니다 도움말 :
public CopyOfCleanBallPanel2() throws IOException, InterruptedException {
frame = new JFrame("simple gaming loop in java");
frame.setSize(BOX_WIDTH, BOX_WIDTH);
frame.setResizable(false);
displayCanvas = new CustomCanvas();
displayCanvas.setLocation(0, 0);
displayCanvas.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
displayCanvas.setBackground(Color.BLACK);
displayCanvas.setFont(new Font("Arial", Font.BOLD, 14));
displayCanvas.setPreferredSize(new Dimension(CANVAS_WIDTH,CANVAS_HEIGHT));
frame.add(displayCanvas);
displayCanvas.requestFocus();
frame.setLocationRelativeTo(null);
try {
this.aBall = (BallServer) Naming
.lookup("rmi://localhost/BouncingBalls");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
frame.pack();
frame.setVisible(true);
aBall.start();
startFrameTimer();
}
/*
* Initializes the frame (also game update) timer.
*/
private void startFrameTimer() {
frameTimer.schedule(new FrameTimerTask(), 1, GAME_TIMER_COOLDOWN);
}
public void updateSimulation() throws RemoteException {
repaintCanvas();
}
/*
* This method gets called by the timer. It updates the game simulation and
* redraws it.
*/
private void onFrameTimer() throws RemoteException {
updateSimulation();
}
/*
* Causes the whole canvas to get repainted.
*/
private final void repaintCanvas() throws RemoteException {
Graphics g = displayCanvas.getGraphics();
drawworld(g);
}
private class FrameTimerTask extends TimerTask {
public void run() {
try {
onFrameTimer();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
/*
* This custom canvas overrides the paint method thus allowing for a custom
* painting on the component.
*/
private class CustomCanvas extends Canvas {
@Override
public void paint(Graphics g) {
// Currently the game message gets drawn over the inner border
// of the canvas so we have to repaint the whole thing.
try {
repaintCanvas();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
public void drawworld(Graphics g) throws RemoteException {
g.setColor(Color.BLACK);
g.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
System.out.println("i m in drawworld ");
serBall = aBall.getState1();// other version UNcomment this line
System.out.println("i m in drawworld "+serBall.length);
for (int i = 0; i < currentNumBalls; i++) {
g.setColor(serBall[i].getBallColor(velocity.getLength()));
g
.fillOval((int) (serBall[i].position.getX() - serBall[i]
.getRadius()),
(int) (serBall[i].position.getY() - serBall[i]
.getRadius()), (int) (2 * serBall[i]
.getRadius()), (int) (2 * serBall[i]
.getRadius()));
// Draw our framerate and ball count
g.setColor(Color.WHITE);
g.drawString("FPS: " + currentFrameRate + " Balls: "
+ currentNumBalls, 15, 15);
}
}
도와주세요.
비슷한 디자인을 기반으로 한 몇 가지 예를 찾고 있습니다. –
스윙 타이머의 예는 무엇입니까? 이 포럼과 다른 포럼에는 많은 예제가 있습니다. 그렇지 않으면 최선의 방법은이 예제를 직접 만들어 보는 것입니다.이 포럼이 무엇인지 알아 내려면 여기에서 다시 돌아와보십시오. –
타이머로 만들었지 만 여전히 문제가 있습니다. –