Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using convertPointToScreen in SwingUtilities  RSS feed

 
vyshnavi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use the method convertPointToScreen in SwingUtilities. I need a sample code using this method.
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vyshanavi,
First thing,u will be asked to change u r name as it doesnt match the JAVARanch pattern.
As for ur question,i got some code from some site.
hope it will be of some help.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingutilities extends JFrame {
private Point lastScreenPt = null;
private final Container contentPane = getContentPane();
private PanelWithString
outer = new PanelWithString(Color.orange),
inner = new PanelWithString(Color.red),
innermost = new PanelWithString(Color.yellow);
public swingutilities() {
Font font = new Font("Times-Roman", Font.ITALIC, 26);
contentPane.setLayout(new OverlayLayout(contentPane));
contentPane.add(innermost);
contentPane.add(inner);
contentPane.add(outer);
innermost.setMaximumSize(new Dimension(350,50));
inner.setMaximumSize(new Dimension(450,200));
outer.setMaximumSize(new Dimension(550,400));
setFont(font);
innermost.setFont(font);
inner.setFont(font);
outer.setFont(font);
contentPane.addMouseMotionListener(
new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
Point pt = e.getPoint();
outer.setString(SwingUtilities.convertPoint(
contentPane, pt, outer).toString());
inner.setString(SwingUtilities.convertPoint(
contentPane, pt, inner).toString());
innermost.setString(SwingUtilities.convertPoint(
contentPane, pt, innermost).toString());
SwingUtilities.convertPointToScreen(
pt, contentPane);
lastScreenPt = pt;
repaint();
}
});
setSize(300,300);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
if(lastScreenPt != null) {
String s = new String("Screen: " + lastScreenPt);
g.setColor(getForeground());
g.drawString(s,10,g.getFontMetrics().getHeight());
SwingUtilities.convertPointFromScreen(lastScreenPt,
contentPane);
s = "Content Pane: " + lastScreenPt;
g.drawString(s,10,g.getFontMetrics().getHeight()*2);
}
else {
g.setColor(getForeground());
g.drawString("MOVE THE MOUSE IN HERE",10,
g.getFontMetrics().getHeight());
}
}
public static void main(String a[])
{
new swingutilities();
}
}
class PanelWithString extends JPanel {
String s;
Color color;
public PanelWithString(Color color) {
this.color = color;
}
public void setString(String s) {
this.s = s;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
g.setColor(color);
g.fillRect(0,0,size.width,size.height);
if(s != null) {
g.setColor(getForeground());
g.drawString(s,10,g.getFontMetrics().getHeight());
}
}

}

cheers,
Raj
 
vyshnavi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Thanks for ur reply.
But still I need more clarification. I need to place a component in the view with respect to the screen (currently it has been placed with respect to its parent). can this method be applicable in this regard. If so, what could be the arguments of this method
convertPointToScreen(Point pt,Component c)
Point pt and Component c be possible in this
case.
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vyshnavi,
well iam not clear of ur question.
can u send me the code ,so that i can take a look at it.i hadnt worked much on this topic,but will try to have a look at it ...
Raj
 
vyshnavi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i cud not paste my entire code here since it may not be clear to u, im just sending a few lines for ur better understanding
dummyPanel.setLayout(new FlowLayout (FlowLayout.CENTER));


dummyPanel.setBounds(xPosition,yPosition,150,50);
Point loc=dummyPanel.getLocation();
SwingUtilities.convertPointToScreen(loc,dummyPanel);

dummyPanel--- the panel whose location has to be changed with respect to the screen
xPosition and yPosition--- x and y value of the dummypanel that has been placed with respect to its parent ,
that is,
JPanel parenPanel=new JPanel();
parentPanel.setBounds(20,20,500,500);
parentPanel.add(dummyPanel);

I have some other panel at the bottom of the parentPanel named stripPanel. this stripPanel is added to some other panel
say, X.add(parentPanel)
Y.add(stripPanel);
and there is no relation between the stripPanel and the parentPanel . the size and location of stripPanel may vary due to some constraints.i should place my dummyPanel exactly above the stripPanel.with the knowledge of the location and size of the stripPanel i should place the dummyPanel. so, i need to convert the location of the stripPanel with respect to the screen since the location of the stripPanel is with respect to its parent Y . i'll take that location and set the same for my dummyPanel
thanx for ur effort
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why r u using the FlowLayout() for the dummypanel,
when u r using setBounds() for it.Also,there seems to be a lot of panels involved and i am confused,sorry about that .But whats the use of Swing Utilities in it?why do u need it? just to place the components with respect to the screen?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!