The Artima Developer Community
Sponsored Link

Java Answers Forum
Jpanel and Jframe

6 replies on 1 page. Most recent reply: Oct 24, 2005 2:24 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
mazhongkun

Posts: 1
Nickname: ralf0716
Registered: Sep, 2005

Jpanel and Jframe Posted: Oct 20, 2005 1:01 PM
Reply to this message Reply
Advertisement
I can want a ball is moving in the window, but it doesn't work.
the idea is :
draw a ball in red color,then draw the same ball in the ground color to cover the previous ball,
the then draw another ball beside the previous one ,the cover ..
so it looks like the ball is moving.
but the result is at beginning time, where is no ball at the window, at last the ball appear at the final position ,
there is no movement.
here is the program:
//import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class operation extends JFrame{

Panel graph;

public operation(String name)
{
super(name);
setSize(400,600);
graph = new Panel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(graph,BorderLayout.CENTER);

}


public static void main(String args[])
{
operation picture =new operation("seapot");
picture.setVisible(true);
}



}

import javax.swing.*;
import java.awt.*;
import java.awt.Point;
import java.applet.Applet;
import java.lang.InterruptedException;

public class Panel extends JPanel{


public Panel()
{}

public void paintComponent(Graphics g)
{


setBackground(Color.white);
int x1=30;
int y1=30;
int x2=0;

for(int i=0;i<50;i++)
{
g.setColor(Color.white);
g.fillOval(x2,y1,50,50);
x1=x1+1;
g.setColor(Color.red);
g.fillOval(x1,y1,50,50);
x2=x1;

try{Thread.sleep(10);}catch(Exception exp){}
}

the reason i thought is the first the computer calculate the things of the panel, then put the panel on the frame, so we can only see the final result.
i am right?
how to realize what i want .


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Jpanel and Jframe Posted: Oct 20, 2005 10:31 PM
Reply to this message Reply
Reformat your code using the java tags as shown in the "formatting your post" section, then someone might actually read your code.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Jpanel and Jframe Posted: Oct 20, 2005 10:36 PM
Reply to this message Reply
The aswer to you question however is, that all happens to fast.

Use a timer.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Jpanel and Jframe Posted: Oct 20, 2005 10:39 PM
Reply to this message Reply
My fault.

You did indeed try to stop the thread (that's what happens if someone trys to read unformated code).

You're right about the reason.

Try creating a timer wich calls repaint() once in a while.
Inside the paintComponent() method draw the ball only once.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Jpanel and Jframe Posted: Oct 20, 2005 11:20 PM
Reply to this message Reply
mazhongkun:

The text area in which you write place your posting has instructions to your right on how to format your code.
Please repost your code whilst following these instructions i.e your code must be encampused in Java tags. You see the
reality is that on Artima there is a diverse amount of skill in a whole wack of areas. For instance if you're asking about
Swing, I would have probably responded to your post in a
matter of hours from when you posted it depending on your
geographical location (I deal with Swing 9 hrs a day at
work and 3 hours at home on personal projects). Matthias
also seems to be extremely competent in Swing.

That said to me, its extremely discouraging to look at untidy code so there are lazy days like today being a
Friday when I'd just say, forget that.

For you'r own good please follow the instructions you will find that you will get an extremely fast response rate
on artima relative to any other time (especially if its a Swing based question) I spend my coffee breaks on this site.

Kondwani

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: Jpanel and Jframe Posted: Oct 23, 2005 7:03 PM
Reply to this message Reply
Animation in Java requires that you decide on two distinct issues. Resolution of the speed and smoothness of the motion. If your motion needs to update 30 times per second, and it needs to paint all frames, then you can use a loop with a sleep and call JComponent.paintImediately(). If the resolution is looser, then you can call repaint() inside of a SwingUtilities.invokeLater() with a delay between calls. Then, in the paintComponent, you use the current time, or a frame number to paint the current, correct rendering. By doing invoke later, you allow slow computers to still update in real time, but draw fewer frames/sec.

You can't call sleep in a paintComponent() call and expect to see anything on the display. Your paint routing is painting into a double buffering buffer, which is composited onto the screen after you return from paintComponent().

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Jpanel and Jframe Posted: Oct 24, 2005 2:24 AM
Reply to this message Reply
Thanks Gregg.
I have to copy that answer in my "Notes" folder.

This will really help me in the future to make anything work smoother.

Flat View: This topic has 6 replies on 1 page
Topic: Help me about code!!! Previous Topic   Next Topic Topic: OutOfMemoryException

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use