Friday 15 February 2013

Java paint image on negative coordinates -


itemprop = "text">

I want to do something best explained from this picture:

 image on JPanel /i.stack.imgur.com/13Evi.png

In words, I need the part of JPanel in the top left for the right-bottom portion of the photo I have tried to set X and Y on the negative, but no success was found, the image has not been displayed at all. Is there an easy way to do this?

I am currently using

  g.drawImage (img, x, y, null) // x and y are negative   

To attract it.

After

I'll verify you, the x / y position is being calculated correctly and a ImageObserver Adding your context g.drawImage are you painting on the screen

 Enter image details here

  import java.awt.borderLayout; Import java.awt.Dimension; Import java.awt.EventQueue; Import java.awt.Graphics; Import java.awt.image.BufferedImage; Import java.io.file; Import java.io.IOException; Import javax.imageio.ImageIO; Import javax.swing.JFrame; Import javax.swing.JPanel; Import javax.swing.UIManager; Import javax.swing.UnsupportedLookAndFeelException; Public category negative image {public static zero main (string [] args) {new negative image (); } Public Negative Image () {EventQueuekinvokeLater (new Rnnabl) {@Override public void run () Hold {{UIManager.setLookAndFeel {}} (Clasnotfound Exception | InstantiationException | illegal Aksesaksepshn | Asmrthitdrishy Awrfiksel Akspeshens ex) {} JFrame frame = New JFrame ("Test"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.setLayout (New BorderLayout ()); Frame.add (New TestPane ()); Frame.pack (); Frame.setLocationRelativeTo (null); Frame.Service only (true);}}); } Public Square TestPane JPanel {Private Buffett Image; Public TestPane () {try {img = ImageIO.read (new File ( "/ user / Swhitehead / Dropbox / Megatokeyo / Ivil_ Small.jpg")); } Catch (IOException ex) {}} @ Override public dimension getPreferedSize () {new dimension (Return 200, 200); } @ Override Protected Zero Paint Comonant (Graphics G) {SuperPaintConant (G); Int x = (getWidth () - img.getWidth ()) / 2; Int y = (getHyight () - img.getHeight ()) / 2; // Center ... g.drawImage (IMG, x, y, this); // on left ... x = - (img.getWidth () / 2); G.drawImage (IMG, X, Y, this); // right off ... x = getWidth () - (img.getWidth () / 2); G.drawImage (IMG, X, Y, this); }}}    

No comments:

Post a Comment