| | |
Questions about Applet and JFrame
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Couldn't really say on that one. You'll just have to test it and tweak if needed. A lot of factors go into FPS besides just the resolution because most apps are doing more than simply blitting an image to the screen each cycle of the animation loop.
To keep painting as lean and responsive as possible, try to keep everything that does not directly relate to rendering out of your paintComponent() code. The paint methods can be called by many other things other than your loop, so put code that updates things or performs calculation in another method if possible and just keep the minimum about of code needed to redender the component in the paintComponent() method itself.
To keep painting as lean and responsive as possible, try to keep everything that does not directly relate to rendering out of your paintComponent() code. The paint methods can be called by many other things other than your loop, so put code that updates things or performs calculation in another method if possible and just keep the minimum about of code needed to redender the component in the paintComponent() method itself.
Last edited by Ezzaral; Jan 3rd, 2008 at 2:42 pm. Reason: a bit more info on painting
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
Hello Mr.UNOwen,
I have same problem and i need solution if u got that.
I am creating one applet and i draw one object on it but
I want a window of applet in the shape of the objct that i draw on window.
i need that window transparent such that I can draw object such that it looks as though the window is in the shape of
that object.So is there a way to get what's displayed behind the window and display it.
Thanks,
Ajay Patel
I have same problem and i need solution if u got that.
I am creating one applet and i draw one object on it but
I want a window of applet in the shape of the objct that i draw on window.
i need that window transparent such that I can draw object such that it looks as though the window is in the shape of
that object.So is there a way to get what's displayed behind the window and display it.
Thanks,
Ajay Patel
Last edited by ajaypatel; Jan 7th, 2008 at 9:11 am. Reason: set nitification
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
Hi Ezzaral,
ya,u r right. i got one link after that post.
http://www.onjava.com/pub/a/onjava/e...ex.html?page=3
in this we take screenshot of and put it in background of our window.
and this process is continuosly work in thread.
ok it nice and some what help ful to me. but window is still not moving.
ok in one of the above post Mr.UNOwen's question was to change the shape of window.
i found one link 4 that.
plz check this.
http://www-106.ibm.com/developerwork...rary/j-iframe/
Thanking you,
Ajay Patel
ya,u r right. i got one link after that post.
http://www.onjava.com/pub/a/onjava/e...ex.html?page=3
in this we take screenshot of and put it in background of our window.
and this process is continuosly work in thread.
ok it nice and some what help ful to me. but window is still not moving.
ok in one of the above post Mr.UNOwen's question was to change the shape of window.
i found one link 4 that.
plz check this.
http://www-106.ibm.com/developerwork...rary/j-iframe/
Thanking you,
Ajay Patel
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
continue of above...post
in that example that i see the image.actaully i put it but its not work 4 me.
can u help me..i put my code here..
-----------------------------------
TransparentBackground.java
-----------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingdemo;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author vinod
*/
public class TransparentBackground extends JComponent implements ComponentListener, WindowFocusListener,
Runnable,MouseListener {
private JFrame frame;
private Image background;
private long lastupdate = 0;
public boolean refreshRequested = true;
@Override
protected void processMouseEvent(MouseEvent e) {
JOptionPane.showMessageDialog(null, "processMouseEvent Called");
}
public TransparentBackground(JFrame frame) {
this.frame = frame;
updateBackground( );
frame.addComponentListener(this);
frame.addMouseListener(this);
frame.addWindowFocusListener(this);
new Thread(this).setPriority(Thread.MAX_PRIORITY);
new Thread(this).start( );
}
public void componentShown(ComponentEvent evt) {
// JOptionPane.showMessageDialog(null, "componentShown Data");
repaint( );
}
public void componentResized(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null, "componentResized Called");
repaint( );
}
public void componentMoved(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null, "componentMoved Called");
repaint( );
}
public void componentHidden(ComponentEvent evt)
{
// JOptionPane.showMessageDialog(null, "componentHidden Called");
}
public void windowGainedFocus(WindowEvent evt) { refresh( ); }
public void windowLostFocus(WindowEvent evt) { refresh( ); }
public void refresh( ) {
if(frame.isVisible( )) {
repaint( );
refreshRequested = true;
lastupdate = new Date( ).getTime( );
}
}
public void run( ) {
try {
while(true) {
Thread.sleep(250);
long now = new Date( ).getTime( );
if(refreshRequested &&
((now - lastupdate) > 1000)) {
if(frame.isVisible( )) {
Point location = frame.getLocation( );
frame.setVisible(false);
updateBackground( );
frame.setVisible(true);
frame.setLocation(location);
refresh( );
}
lastupdate = now;
refreshRequested = false;
}
}
} catch (Exception ex) {
//p(ex.toString( ));
ex.printStackTrace( );
}
}
public void updateBackground( ) {
try {
Robot rbt = new Robot( );
Toolkit tk = Toolkit.getDefaultToolkit( );
Dimension dim = tk.getScreenSize( );
background = rbt.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth( ),
(int)dim.getHeight( )));
} catch (Exception ex) {
//p(ex.toString( ));
ex.printStackTrace( );
}
}
public void paintComponent(Graphics g) {
Point pos = this.getLocationOnScreen( );
Point offset = new Point(-pos.x,-pos.y);
g.drawImage(background,offset.x,offset.y,null);
}
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3)
{
JOptionPane.showMessageDialog(null, "mouseClicked Called - 3");
}
if(e.getButton()==MouseEvent.BUTTON1)
{
JOptionPane.showMessageDialog(null, "mouseClicked Called - 1");
}
throw new UnsupportedOperationException("Not supported yet.");
}
public void mousePressed(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
-----------------------
Trans.java
-----------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingdemo;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author vinod
*/
public class Trans {
public static void main(String[] args) {
JFrame frame = new JFrame("Transparent Window");
frame.setUndecorated(true);
TransparentBackground bg = new TransparentBackground(frame);
//bg.snapBackground( );
bg.setLayout(new BorderLayout( ));
JPanel panel = new JPanel( ) {
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
Image img = new ImageIcon("3250.png").getImage( );
g.drawImage(img,0,0,null);
}
};
panel.setOpaque(false);
bg.add("Center",panel);
frame.getContentPane( ).add("Center",bg);
frame.pack( );
frame.setSize(200,200);
frame.setLocation(500,500);
frame.setVisible(true);
}
}
please put any one image file inplace of 3250.png and check is it working or not?
Thank you,
Ajay Patel
in that example that i see the image.actaully i put it but its not work 4 me.
can u help me..i put my code here..
-----------------------------------
TransparentBackground.java
-----------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingdemo;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author vinod
*/
public class TransparentBackground extends JComponent implements ComponentListener, WindowFocusListener,
Runnable,MouseListener {
private JFrame frame;
private Image background;
private long lastupdate = 0;
public boolean refreshRequested = true;
@Override
protected void processMouseEvent(MouseEvent e) {
JOptionPane.showMessageDialog(null, "processMouseEvent Called");
}
public TransparentBackground(JFrame frame) {
this.frame = frame;
updateBackground( );
frame.addComponentListener(this);
frame.addMouseListener(this);
frame.addWindowFocusListener(this);
new Thread(this).setPriority(Thread.MAX_PRIORITY);
new Thread(this).start( );
}
public void componentShown(ComponentEvent evt) {
// JOptionPane.showMessageDialog(null, "componentShown Data");
repaint( );
}
public void componentResized(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null, "componentResized Called");
repaint( );
}
public void componentMoved(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null, "componentMoved Called");
repaint( );
}
public void componentHidden(ComponentEvent evt)
{
// JOptionPane.showMessageDialog(null, "componentHidden Called");
}
public void windowGainedFocus(WindowEvent evt) { refresh( ); }
public void windowLostFocus(WindowEvent evt) { refresh( ); }
public void refresh( ) {
if(frame.isVisible( )) {
repaint( );
refreshRequested = true;
lastupdate = new Date( ).getTime( );
}
}
public void run( ) {
try {
while(true) {
Thread.sleep(250);
long now = new Date( ).getTime( );
if(refreshRequested &&
((now - lastupdate) > 1000)) {
if(frame.isVisible( )) {
Point location = frame.getLocation( );
frame.setVisible(false);
updateBackground( );
frame.setVisible(true);
frame.setLocation(location);
refresh( );
}
lastupdate = now;
refreshRequested = false;
}
}
} catch (Exception ex) {
//p(ex.toString( ));
ex.printStackTrace( );
}
}
public void updateBackground( ) {
try {
Robot rbt = new Robot( );
Toolkit tk = Toolkit.getDefaultToolkit( );
Dimension dim = tk.getScreenSize( );
background = rbt.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth( ),
(int)dim.getHeight( )));
} catch (Exception ex) {
//p(ex.toString( ));
ex.printStackTrace( );
}
}
public void paintComponent(Graphics g) {
Point pos = this.getLocationOnScreen( );
Point offset = new Point(-pos.x,-pos.y);
g.drawImage(background,offset.x,offset.y,null);
}
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3)
{
JOptionPane.showMessageDialog(null, "mouseClicked Called - 3");
}
if(e.getButton()==MouseEvent.BUTTON1)
{
JOptionPane.showMessageDialog(null, "mouseClicked Called - 1");
}
throw new UnsupportedOperationException("Not supported yet.");
}
public void mousePressed(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
-----------------------
Trans.java
-----------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingdemo;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author vinod
*/
public class Trans {
public static void main(String[] args) {
JFrame frame = new JFrame("Transparent Window");
frame.setUndecorated(true);
TransparentBackground bg = new TransparentBackground(frame);
//bg.snapBackground( );
bg.setLayout(new BorderLayout( ));
JPanel panel = new JPanel( ) {
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
Image img = new ImageIcon("3250.png").getImage( );
g.drawImage(img,0,0,null);
}
};
panel.setOpaque(false);
bg.add("Center",panel);
frame.getContentPane( ).add("Center",bg);
frame.pack( );
frame.setSize(200,200);
frame.setLocation(500,500);
frame.setVisible(true);
}
}
please put any one image file inplace of 3250.png and check is it working or not?
Thank you,
Ajay Patel
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Creation of file types and encryption
- Next Thread: adding a JFrame component to JTabbedPane
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string superclass swing system thread threads time tree variablebinding windows xor






