09. oktober 2003 - 16:49
Der er
4 kommentarer og 1 løsning
Effective rendering?
Hello! I try to build a simple fictive transperant window. I got som help with a example from the Internet but I was not happy with the calculating of X and Y cordinates for the position. So I tried to calculate it my self but the result was that my calulate was raelly not effective since the window flicker alot when draging it around. But I can't tell why. To see the window now you will se a red circle. So if any one could help me and explaine whay and a better whay to drag the window around I would be most thankfull. The code below is when the rendering is effective, to try when the cordinates for the loction is better you have to uncomment the code inside mousePressed and mouseDraged and block the code that is open now. Best regards Fredrik Andersson import java.util.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.applet.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.border.*; import java.awt.image.*; public class GlassWindowTest extends JWindow implements MouseListener, MouseMotionListener { private Image screen; private Image backBuffer; private Graphics backGraphics; private Dimension preferredSize = new Dimension(250,200); private Dimension screenSize; private Rectangle screenRectangle; private int xdiff = 0; private int ydiff = 0; private static Robot robot; private int mousePressedX; private int mousePressedY; private int mouseDraggedX; private int mouseDraggedY; private Dimension windowSize; private int newX; private int newY; public GlassWindowTest() { super(); JPanel content = (JPanel) getContentPane(); content.setOpaque(false); setSize( getPreferredSize() ); windowSize = getSize(); screenSize = Toolkit.getDefaultToolkit().getScreenSize(); screenRectangle = new Rectangle(); screenRectangle.setSize( screenSize ); Rectangle bounds = getBounds(); newX = (screenSize.width - bounds.width) / 2; newY = (screenSize.height - bounds.height) / 2; try { robot = new Robot(); } catch(Exception e) { e.printStackTrace(); } addMouseListener( this ); addMouseMotionListener( this ); copyScreen(); setSize(250, 200); centerFrameOnScreen(); show(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if(backBuffer == null) { createBackGraphics(); } if( screen != null ) { backGraphics.drawImage( screen, 0, 0, windowSize.width, windowSize.height, newX, newY, newX+windowSize.width, newY+windowSize.height, null); } backGraphics.setColor(Color.red); backGraphics.fillOval(50, 50, 50,50); g.drawImage(backBuffer, 0, 0, null); } private void createBackGraphics() { backBuffer = createImage(250, 200); backGraphics = backBuffer.getGraphics(); } public void centerFrameOnScreen() { setBounds( newX, newY, 250, 200); } public void copyScreen() { screen = robot.createScreenCapture( screenRectangle ); } public Dimension getMinimumSize() { return this.preferredSize; } public Dimension getPreferredSize() { return this.preferredSize; } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { xdiff = e.getX() - newX; ydiff = e.getY() - newY; if( xdiff < 0 ) { xdiff = -xdiff; } if( ydiff < 0 ) { ydiff = -ydiff; } /* mousePressedX = e.getX(); mousePressedY = e.getY(); */ } public void mouseDragged(MouseEvent e) { newX = newX + (e.getX() - xdiff); newY = newY + (e.getY() - ydiff); setLocation( newX, newY ); repaint(); /* mouseDraggedX = e.getX(); mouseDraggedY = e.getY(); xdiff = mouseDraggedX - mousePressedX; ydiff = mouseDraggedY - mousePressedY; mousePressedX = mouseDraggedX; mousePressedY = mouseDraggedY; newX = newX + (xdiff); newY = newY + (ydiff); setLocation( newX , newY ); repaint(); */ } public void mouseMoved(MouseEvent e) { } public static void main(String[] args) { GlassWindowTest glassWindow = new GlassWindowTest(); } }
Annonceindlæg tema
09. oktober 2003 - 17:12
#1
Make the following changes to your Class //Add these variables. private boolean first = false; private java.awt.Point mouseP; private double oldPosX, oldPosY, mouseFirstX, mouseFirstY, mouseNowX, mouseNowY, oldMouseX, oldMouseY; //Replace these methods public void mousePressed(MouseEvent e) { first = true; } public void mouseDragged(MouseEvent e) { mouseP = e.getPoint(); if(first){ mouseFirstX = mouseP.getX(); mouseFirstY = mouseP.getY(); } mouseNowX = mouseP.getX(); mouseNowY = mouseP.getY(); oldPosX = this.getX(); oldPosY = this.getY(); setLocation((int)((oldPosX + mouseNowX) - mouseFirstX) , (int)((oldPosY + mouseNowY) - mouseFirstY) ); first = false; }
09. oktober 2003 - 17:38
#2
If I should add comments, it would be like this. // Get mouseposition inside JWindow if(first){ mouseFirstX = mouseP.getX(); mouseFirstY = mouseP.getY(); } // Get mouseposition on screen mouseNowX = mouseP.getX(); mouseNowY = mouseP.getY(); //Get position of JWindow on screen oldPosX = this.getX(); oldPosY = this.getY();
09. oktober 2003 - 17:53
#3
This comment ain't exactly right // Get mouseposition on screen mouseNowX = mouseP.getX(); mouseNowY = mouseP.getY(); It should have been // Get NEW mouseposition inside JWindow, happens when dragging the mouse
10. oktober 2003 - 11:07
#4
Thanks for your help Cartsten, below is the final result. But do you know why my solution result in so much flicker compare to yours? Best regards Fredrik package glasswindow; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; public class GlassWindowTest extends JWindow implements MouseListener, MouseMotionListener { private Image screen; private Image backBuffer; private Graphics backGraphics; private Dimension preferredSize = new Dimension(250,200); private Dimension screenSize; private Rectangle screenRectangle; private int xdiff = 0; private int ydiff = 0; private static Robot robot; private Dimension windowSize; private int newX; private int newY; private boolean first = false; private java.awt.Point mouseP; private double oldPosX, oldPosY, mouseFirstX, mouseFirstY, mouseNowX, mouseNowY; public GlassWindowTest() { super(); JPanel content = (JPanel) getContentPane(); content.setOpaque(false); setSize( getPreferredSize() ); windowSize = getSize(); screenSize = Toolkit.getDefaultToolkit().getScreenSize(); screenRectangle = new Rectangle(); screenRectangle.setSize( screenSize ); Rectangle bounds = getBounds(); newX = (screenSize.width - bounds.width) / 2; newY = (screenSize.height - bounds.height) / 2; try { robot = new Robot(); } catch(Exception e) { e.printStackTrace(); } addMouseListener( this ); addMouseMotionListener( this ); copyScreen(); setSize(250, 200); centerFrameOnScreen(); show(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if(backBuffer == null) { createBackGraphics(); } if( screen != null ) { backGraphics.drawImage( screen, 0, 0, windowSize.width, windowSize.height, newX, newY, newX+windowSize.width, newY+windowSize.height, null); } backGraphics.setColor(Color.red); backGraphics.fillOval(50, 50, 50,50); g.drawImage(backBuffer, 0, 0, null); } private void createBackGraphics() { backBuffer = createImage(250, 200); backGraphics = backBuffer.getGraphics(); } public void centerFrameOnScreen() { setBounds( newX, newY, 250, 200); } public void copyScreen() { screen = robot.createScreenCapture( screenRectangle ); } public Dimension getMinimumSize() { return this.preferredSize; } public Dimension getPreferredSize() { return this.preferredSize; } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { first = true; } public void mouseDragged(MouseEvent e) { mouseP = e.getPoint(); if(first) { mouseFirstX = mouseP.getX(); mouseFirstY = mouseP.getY(); } mouseNowX = mouseP.getX(); mouseNowY = mouseP.getY(); oldPosX = this.getX(); oldPosY = this.getY(); newX = (int)((oldPosX + mouseNowX) - mouseFirstX); newY = (int)((oldPosY + mouseNowY) - mouseFirstY); setLocation( newX, newY ); repaint(); first = false; } public void mouseMoved(MouseEvent e) { } public static void main(String[] args) { GlassWindowTest glassWindow = new GlassWindowTest(); } }
10. oktober 2003 - 12:21
#5
I will try to explain public void mousePressed(MouseEvent e) { xdiff = e.getX() - newX; // If give these things som fictive numbers // e.getX mouseposition in JWindow = 10 // newX = JWindow position on screen = 200 // Your definition: xdiff = e.getX() - newX; // will give xdiff the value of -190 // Later you change that to a positive number if( xdiff < 0 ) { xdiff = -xdiff; } // xdiff is now 190 // This gives the same as my 'first = false;' xdiff = e.getX(); ydiff = e.getY(); public void mouseDragged(MouseEvent e) { newX = newX + (e.getX() - xdiff); // Remember that xdiff is 190 // newX will get the value 20 // newX = 200 + (10 - 190) // 20 newX = newX + (e.getX() - xdiff); // Which means that every time you move your mouse // one point, you switch the windows X-position from // 200 to 20, that's a major jump. Hope this helps you, i can't get it any closer !
Kurser inden for grundlæggende programmering