<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3034852670449113048</id><updated>2024-10-06T21:48:11.109-07:00</updated><title type='text'>Yves&amp;#39; Java &amp;amp; More</title><subtitle type='html'>As a computer science student, I&#39;ve had to look sometimes for hours at a time for a line or two of code necessary to do something simple yet so specific that it&#39;s difficult to find. In the future, I will try to share with you some of these techniques in order to facilitate your programming experience.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-6226689245983279741</id><published>2010-11-09T06:26:00.000-08:00</published><updated>2010-11-09T06:31:39.436-08:00</updated><title type='text'></title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOD5IvHOqkejI0ssW2tN7pFbbnE3DFYlVwln-l0vEkcyU8S2D0PxEQBsSEwiRX1WXLcXe61w-Vb0-UQ6ZirYUcxpvGhe4mW-iT6xs2o7i6fqyWJz7Ez9hWDrQC2r_w1MA7flgjqvnH2WoC/s640/SSA50006.JPG&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOD5IvHOqkejI0ssW2tN7pFbbnE3DFYlVwln-l0vEkcyU8S2D0PxEQBsSEwiRX1WXLcXe61w-Vb0-UQ6ZirYUcxpvGhe4mW-iT6xs2o7i6fqyWJz7Ez9hWDrQC2r_w1MA7flgjqvnH2WoC/s400/SSA50006.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Old House by Yves&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Not my house, but nice composition anyway.</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/6226689245983279741/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/old-house-by-yves-not-my-house-but-nice.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/6226689245983279741'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/6226689245983279741'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/old-house-by-yves-not-my-house-but-nice.html' title=''/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOD5IvHOqkejI0ssW2tN7pFbbnE3DFYlVwln-l0vEkcyU8S2D0PxEQBsSEwiRX1WXLcXe61w-Vb0-UQ6ZirYUcxpvGhe4mW-iT6xs2o7i6fqyWJz7Ez9hWDrQC2r_w1MA7flgjqvnH2WoC/s72-c/SSA50006.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-2438662272907169792</id><published>2010-11-09T06:18:00.000-08:00</published><updated>2010-11-09T14:25:38.305-08:00</updated><title type='text'>BufferedImage transparency</title><content type='html'>BufferedImage class is a very useful tool for certain animations. Not only you could double buffer with these images, but you could organise a complicated animated scene of many layered BufferedImages on top of each other. In my Sudoku program, when mouse is over a cell it gets highlighted in yellow like so:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjD9yXpRHav5A9D7yY-xiFlzRHtHjLOsK-61H0KGugIAbR8-rObV_iJ642tH9moFytHmOX95XhRj0gfLBqSbrk4i4RjWQtd8U6xN39FOajzs71q4vCQ_dbLOCAnH1nWKzehmKfadsY1V_q/s1600/highlight.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjD9yXpRHav5A9D7yY-xiFlzRHtHjLOsK-61H0KGugIAbR8-rObV_iJ642tH9moFytHmOX95XhRj0gfLBqSbrk4i4RjWQtd8U6xN39FOajzs71q4vCQ_dbLOCAnH1nWKzehmKfadsY1V_q/s320/highlight.png&quot; width=&quot;291&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;I could think of 2 ways to achieve this effect:&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;1. clear the whole image -&amp;gt; paint yellow square in its position -&amp;gt; paint table and digits&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;2. paint yellow square over table -&amp;gt; paint digit over it -&amp;gt; clear cell when mouse is relocated and paint digit again&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;However, the third easier and more efficient, in my opinion, way that I used is use BufferedImage. The first Buffered image contains the table and digits; the second image contains the highlight of the cell with the mouse over it. In doing this, all I need to do is print the table and digits once, then paint a yellow square on the second BufferedImage, under the current cell. If mouse is moved I can easily clear the whole second image with the yellow square on it without affecting the sudoku table. Here is how to do it:&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;/* This is still in my ...extends JPanel class from the last tutorial*/ &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;private BufferedImage img, backImg; //these are the two images&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;Graphics2D g, gBack;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //and these graphics objects will be used to paint on the two images&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;/...&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;/*in Constructor among other initializations*/&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //initialize all buffered images used for the drawing of sudoku and the graphics obj connected to them&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; img = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.BITMASK);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; backImg = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.BITMASK);&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g = (Graphics2D) img.getGraphics();&amp;nbsp; //now g can be used to paint on the img BufferedImage&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gBack = (Graphics2D) backImg.getGraphics();&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;So up to this point we have 2 transparent BufferedImages with the size of the current container.&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;ATTENTION!&lt;/span&gt; The container&#39;s size has to be already set and I even said setVisible(true); before initializing the images. If the container&#39;s size is still 0 then the images won&#39;t initialize properly.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;BufferedImage.BITMASK type is essential for this problem, since it makes the images transparent.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Now in a method that I do not want to bother you with, I paint the grid on the img object. With Graphics2D you can do things like:&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;g.setStroke(new BasicStroke(thickStroke)); /*to set the width of painted lines where thickStroke is an integer indicating stroke in pixels*/&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;g.drawLine(x, inset, x, y); // to draw a line with the stroke specified&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;g.setColor(Color.BLACK); //sets the colour to use in painting&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;, etc.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Going to my highlight method I have:&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /**&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * Highlights a cell on the graphics component provided.&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * @param row&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * @param column&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * @param g&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * @param highlight If set to null, this method simply makes everything transparent.&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void highlightCell(int row, int column, Graphics2D g, Color highlight) {&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Composite c = g.getComposite(); //save original composite&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); //set to draw transparent&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.fillRect(0, 0, getWidth(), getHeight()); //clear everything with transparent rectangle&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //determine x and y coordinates of upper-right corner of inticated cell&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int highlightX = inset + (column * squareWidth);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int highlightY = inset + (row * squareWidth);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //draw a square at the specific cell&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setComposite(c); //reset the composite to normal drawing&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (highlight != null) {&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setColor(highlight);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.fillRect(highlightX, highlightY, squareWidth, squareWidth);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; repaint();&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;}&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Since I am not very familiar with Composite class, I decided to backup the normal Composite used before I change it into transparent drawing mode by the second line of code in the method. When I call the method, I pass gBack as the second parameter in order to paint the highlight on the backImg object. The method is easy to understand and in the end you can see the repaint(); command. This command calls the paintComponent(Graphics g) method of JPanel class. Here is how I&#39;ve overridden it:&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #999999; text-align: left;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void paintComponent(Graphics g) {&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; super.paintComponent(g);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //paint from bottom to top backImg and the sudoku itself on top&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (backImg!= null) {&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.drawImage(backImg, 0, 0, this);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (img != null) {&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.drawImage(img, 0, 0, this);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;As you should already know, everything that gets painted on the JPanel has to be painted here in this method. super.paintComponent(g); is there to clean the last things painted on the JPanel. After that I paint the new things I would like to see on my JPanel which is the highlight on backImg and on top of it the sudoku table on img.&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Notice that in the case of my sudoku application, in the highlight method, I could simply clear the BufferedImage with the background colour (white) instead of doing all the Composite stuff to clear it with transparency. I did it because in my real application I actually have a third BufferedImage under the other 2 so I need to paint with transparency in order to see it. Besides you would need to know this in order to use this technique efficiently. It could be used for any animations where you have something in front that needs to be independent from something on the back that is still visible.&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Good luck and comment if you have any recommendations, ideas or questions.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/2438662272907169792/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/bufferedimage-transparency.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/2438662272907169792'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/2438662272907169792'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/bufferedimage-transparency.html' title='BufferedImage transparency'/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjD9yXpRHav5A9D7yY-xiFlzRHtHjLOsK-61H0KGugIAbR8-rObV_iJ642tH9moFytHmOX95XhRj0gfLBqSbrk4i4RjWQtd8U6xN39FOajzs71q4vCQ_dbLOCAnH1nWKzehmKfadsY1V_q/s72-c/highlight.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-7866604390931020483</id><published>2010-11-05T11:44:00.000-07:00</published><updated>2010-11-05T11:45:25.463-07:00</updated><title type='text'></title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR_Wy1L8s7ickaKxOwoBGxEWw7RBEPp3i_mxthOPgQuBSNsGEmI-VUHOgic7Gpy9TBfweL-ie_QOT4nSOXThGxrPPV43zrnwqOTZwatpDPjay4BQypKf_HwVvSs-EvP6vqGktOn6yuBYy4/s800/Untitled_HDR2_small.jpg&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;297&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR_Wy1L8s7ickaKxOwoBGxEWw7RBEPp3i_mxthOPgQuBSNsGEmI-VUHOgic7Gpy9TBfweL-ie_QOT4nSOXThGxrPPV43zrnwqOTZwatpDPjay4BQypKf_HwVvSs-EvP6vqGktOn6yuBYy4/s400/Untitled_HDR2_small.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&amp;nbsp;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Montreal again, this is the first time I get a decent result from merging photos in HDR image. The problem is that my camera is quite old, so the bracketing setting is pathetic. Still, it&#39;s a nice shot.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/7866604390931020483/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/montreal-again-this-is-first-time-i-get.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/7866604390931020483'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/7866604390931020483'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/montreal-again-this-is-first-time-i-get.html' title=''/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR_Wy1L8s7ickaKxOwoBGxEWw7RBEPp3i_mxthOPgQuBSNsGEmI-VUHOgic7Gpy9TBfweL-ie_QOT4nSOXThGxrPPV43zrnwqOTZwatpDPjay4BQypKf_HwVvSs-EvP6vqGktOn6yuBYy4/s72-c/Untitled_HDR2_small.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-3042831306197176341</id><published>2010-11-05T11:36:00.000-07:00</published><updated>2010-11-05T11:36:08.698-07:00</updated><title type='text'>MouseListener, MouseMotionListener &amp; KeyListener</title><content type='html'>For a Sudoku game I am currently working on, I decided that I want the user to click on a cell that I have drawn on a JPanel and enter a digit using the keyboard. This is an Event Driven Programming challenge. To solve the problem I decided to use a MouseListener in order to capture the click event, a MouseMotionListener to locate the mouse pointer and a KeyListener for the digit input by pressing a key on the keyboard.&lt;br /&gt;
&lt;br /&gt;
Lets start with the MouseMotionListener class. It is an inner class for the public class ClassSDK extends JPanel {} class.&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;private class MouseTracker implements MouseMotionListener {&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mouseDragged(MouseEvent e) {/*do nothing*/&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mouseMoved(MouseEvent e) {&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //check if mouse is over the sudoku table&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (e.getX() &amp;gt; inset &amp;amp;&amp;amp; e.getX() &amp;lt; (inset + (9 * squareWidth))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //mouse in horizontal range; check vertical&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&amp;amp; e.getY() &amp;gt; inset &amp;amp;&amp;amp; e.getY() &amp;lt; (inset + (9 * squareWidth))) {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //in sudoku table...&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //convert mouse coordinates to cell coordinates&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mouseCol = (e.getX() - inset) / squareWidth;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mouseRow = (e.getY() - inset) / squareWidth;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; highlightCell(mouseRow, mouseCol, gBack, Color.ORANGE);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //set mouseCol and mouseRow to -1 identifying mouse is out of table&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mouseRow = -1;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mouseCol = -1;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //clear the backImg highlight effect&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; highlightCell(0, 0, gBack, null);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt; &lt;br /&gt;
&lt;div style=&quot;color: white;&quot;&gt;So in this class I am tracking the mouse over the JPanel in which I have drawn the sudoku table. Once the mouse pointer is above the sudoku table (insets is a variable that contains the size of my empty border around the table) I start converting the coordinates of the pointer to coordinates of a cell in the sudoku table. In case you don&#39;t know, a sudoku table consists of 9 rows and 9 columns. highlightCell() is a method which can either paint a specified cell in certain colour, or clear it from the colour. I will not describe how it works now though. It will be a part of my BufferedImages use blog. You will see where I initialize and add this listener to the ClassSDK JPanel in my MouseListener class: &lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;color: #999999;&quot;&gt;private class ClickListen implements MouseListener {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mouseClicked(MouseEvent e) {/*do nothing*/&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mousePressed(MouseEvent e) {/*do nothing*/&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mouseReleased(MouseEvent e) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //check if click was inside the sudoku&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (mouseRow == -1 || mouseCol == -1) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return; //click outside sudoku table&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*check if there is an entry process already initiated (flag -1 on lockRow and lockCol means no entry process)*/&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (lockRow != -1 || lockCol != -1) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //if so unload the input listener for that process&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; KeyListener[] l = getKeyListeners();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (l.length &amp;gt; 0) { //if there was a key listener which should be the case&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeKeyListener(l[0]); //remove it&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Removed previous input listener.&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //ready to start input method&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; createSDK();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mouseEntered(MouseEvent e) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //start tracking the mouse once entered in the component&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addMouseMotionListener(new MouseTracker());&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; requestFocus(); //automatically focust the panel with the mouse over it&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void mouseExited(MouseEvent e) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //remove motion listener if mouse is out of panel to save ressources&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MouseMotionListener[] l = getMouseMotionListeners();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeMouseMotionListener(l[0]);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&lt;div style=&quot;color: white;&quot;&gt;The createSDK() method is a method which controls the input together with my KeyListener class. The part where I check the variables lockRow and lockCol for a flag of -1 is that, if the user clicks on a cell and starts the entry process but does not enter a value and clicks on another cell instead, the keylistener that listens for entry in the previous cell has to be removed, else with one click of a button the value would be entered in both cells. lockRow and lockCol save the coordinates of the cell where the click occurred until the point when a value is entered and the entry process finishes, when they switch value back to -1.&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /**&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * Called by createSDK, this class waits for a digit to be entered and then&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * saves it to the mainG array, paints it on the grid.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private class InputListen implements KeyListener {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void keyTyped(KeyEvent e) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;key typed&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (lockRow == -1 || lockCol == -1) //there is no lock on any cell&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char input = e.getKeyChar();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //check if input is a digit&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Character.isDigit(input)) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //save input to sudoku array and&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //show input on the sudoku table&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addDigitToMainG(lockRow, lockCol, Integer.parseInt(Character.toString(input)));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //clear visual highlight of cell and reset lock variables&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lockCol = -1;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lockRow = -1;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; highlightCell(0, 0, gLock, null);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //remove the key listener&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; KeyListener[] l = getKeyListeners();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeKeyListener(l[0]);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void keyPressed(KeyEvent e) {/*do nothing*/&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void keyReleased(KeyEvent e) {/*do nothing*/&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;span style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: white;&quot;&gt;&amp;nbsp;This is how the key listener works. It unloads itself once the process has finished. addDigitToMainG(int,int,int) is a method that adds a number to specific row and column in my array and then prints it on the screen as well.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;color: white;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&lt;span style=&quot;color: white;&quot;&gt;It&#39;s a lot of code and explanation and I try to be detailed in my comments, so if you have any questions please comment. &lt;span style=&quot;color: #f3f3f3;&quot;&gt;This is a good exercise on event driven programming combining 3 &lt;/span&gt;differen&lt;span style=&quot;color: #f3f3f3;&quot;&gt;t types of listeners. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/3042831306197176341/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/mouselistener-mousemotionlistener.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/3042831306197176341'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/3042831306197176341'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/mouselistener-mousemotionlistener.html' title='MouseListener, MouseMotionListener &amp; KeyListener'/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-5963227108157817232</id><published>2010-11-03T07:55:00.000-07:00</published><updated>2010-11-03T07:55:23.623-07:00</updated><title type='text'></title><content type='html'>&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Ci9_Dcez7mmIhm9LOqm9kfvxkQA5-hLMcmHtSQeErrZTV-WCT_Kt6ekO4b4ERDmOXUOBU3evDsavbOHy5yDzw-d0tBXkjSBFEu8VqzIwqq_q91YnHg3eqlZqU4oOZpG23lbroIMn1umE/s800/SSA50164.JPG&quot; style=&quot;clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Ci9_Dcez7mmIhm9LOqm9kfvxkQA5-hLMcmHtSQeErrZTV-WCT_Kt6ekO4b4ERDmOXUOBU3evDsavbOHy5yDzw-d0tBXkjSBFEu8VqzIwqq_q91YnHg3eqlZqU4oOZpG23lbroIMn1umE/s400/SSA50164.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Montreal Sunset by Yves&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;Off topic:&lt;br /&gt;
Taking photographs is kind of a hobby that I have.&lt;br /&gt;
&lt;br /&gt;
I find this photo particularly good as the yellow illumination of the sunset on the yellow trees produced an incredibly bright colour.&lt;br /&gt;
&lt;br /&gt;
I had to react fast as I saw the train approaching in order to capture it too. I will publish photos as well as my Java experiences, since I find both of them very interesting.</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/5963227108157817232/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/montreal-sunset-by-yves-off-topic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/5963227108157817232'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/5963227108157817232'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/montreal-sunset-by-yves-off-topic.html' title=''/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Ci9_Dcez7mmIhm9LOqm9kfvxkQA5-hLMcmHtSQeErrZTV-WCT_Kt6ekO4b4ERDmOXUOBU3evDsavbOHy5yDzw-d0tBXkjSBFEu8VqzIwqq_q91YnHg3eqlZqU4oOZpG23lbroIMn1umE/s72-c/SSA50164.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-1922908296549696796</id><published>2010-11-02T16:31:00.000-07:00</published><updated>2010-11-03T04:43:08.517-07:00</updated><title type='text'></title><content type='html'>Apple to stop supporting Java? It is a pitty that we have to suffer because of the self-centered decisions dictated by a company that has a temporary &lt;span id=&quot;hotword&quot;&gt;&lt;span id=&quot;hotword&quot; name=&quot;hotword&quot; style=&quot;background-color: transparent; cursor: default;&quot;&gt;pinnacle&lt;/span&gt; &lt;span id=&quot;hotword&quot; name=&quot;hotword&quot; style=&quot;background-color: transparent; cursor: default;&quot;&gt;&lt;/span&gt;&lt;/span&gt;of global power. How could it be for the better? Well, I guess we&#39;ll have to study objective C guys :) . &lt;a href=&quot;http://www.blogger.com/goog_1911435745&quot;&gt;Here check CNET&#39;s article: &lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.blogger.com/goog_1911435745&quot;&gt;&lt;br /&gt;
&lt;/a&gt; &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://news.cnet.com/8301-31021_3-20020338-260.html&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://i.i.com.com/cnwk.1d/i/tim/2010/10/21/Picture_63.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/1922908296549696796/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/apple-to-stop-supporting-java-it-is.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/1922908296549696796'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/1922908296549696796'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/apple-to-stop-supporting-java-it-is.html' title=''/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-5836790606332618632</id><published>2010-11-02T11:58:00.000-07:00</published><updated>2010-11-02T11:58:48.292-07:00</updated><title type='text'>Add an image background to a JFrame</title><content type='html'>This semester we started studying GUI and the first thing I wanted to know is how to make these dull JFrame windows look better. Here is a simple class that I created to change the JFrame&#39;s background to any image. It&#39;s a good idea to make it a package since you can use it with any JFrame.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #999999;&quot;&gt;/**&lt;br /&gt;
&amp;nbsp;* A panel with the background image on it.&lt;br /&gt;
&amp;nbsp;* @author Yves&lt;br /&gt;
&amp;nbsp;*/&lt;br /&gt;
class Background extends JPanel {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private Image img;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Background(String path) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this(new ImageIcon(path).getImage());&amp;nbsp; //this loads the image from a file (path) to an Image object&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Background(Image img) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.img = img;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setPreferredSize(size);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setSize(size);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setVisible(true);&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;background constructor done&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void paintComponent(Graphics g) {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;background painted&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.drawImage(img, 0, 0, this); //draw the image on the JPanel&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;/div&gt;&lt;br /&gt;
The idea is that this class represents a JPanel and it loads an image from an image file that you specify via the constructor. Afterwards, it sets the size of the JPanel equal to the size of the image and paints it on the JPanel.&lt;br /&gt;
&lt;br /&gt;
Now to use this JPanel as the background for a JFrame all you have to do is:&lt;br /&gt;
&amp;nbsp;(Lets say this is a class that extends JFrame)&lt;br /&gt;
1.&lt;span style=&quot;color: #999999;&quot;&gt; &lt;/span&gt;&lt;b style=&quot;color: #999999;&quot;&gt;final String BACKGROUND_IMAGE = &quot;.\\img\\rough_edge.png&quot;;&amp;nbsp; /*determine the path to the image file*/&lt;/b&gt;&lt;br /&gt;
pay attention to the double slashes, since a single slash indicates an escape code e.g. \n \t \b&lt;br /&gt;
the image is in my project folder in a folder called img, if you run the application from a jar file the img folder has to be in the same folder as the jar&lt;br /&gt;
&lt;br /&gt;
2. initialize a Background object&lt;br /&gt;
&lt;div style=&quot;color: #999999;&quot;&gt;&lt;b&gt;private Background bg; //a field for the background object&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #999999;&quot;&gt;/* in the constructor*/&lt;/div&gt;&lt;span style=&quot;color: #999999;&quot;&gt;bg&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #999999;&quot;&gt; = new Background(BACKGROUND_IMAGE); //initialize in constructor&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
3. set the JFrame&#39;s ContentPane to the JPanel with the background on it. ContentPane is the layer of the JFrame where your components (Buttons, TextFields, etc.) are located.&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #999999;&quot;&gt;this.setContentPane(bg); &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
4. do not forget to set the layout manager for your JFrame AFTER you change the content pane to the Background object, since it changes to the layout set in the Background class, which I believe is FlowLayout (default layout for JPanel)&lt;br /&gt;
e.g.&amp;nbsp; &lt;b style=&quot;color: #999999;&quot;&gt;this.setLayout(new FlowLayout(FlowLayout.CENTER,2,0)); //set a layout for the new content pane&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: yellow;&quot;&gt;Another small thing, if you want to simply change the background color of a &lt;/span&gt;JFrame&lt;span style=&quot;color: yellow;&quot;&gt; and it doesn&#39;t work, try calling the content pane of the &lt;/span&gt;JFrame&lt;span style=&quot;color: yellow;&quot;&gt;: &lt;span style=&quot;color: white;&quot;&gt;this.&lt;/span&gt;&lt;/span&gt;getContentPane&lt;span style=&quot;color: white;&quot;&gt;().&lt;/span&gt;setColor&lt;span style=&quot;color: white;&quot;&gt;(Color col);&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/5836790606332618632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/add-image-background-to-jframe.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/5836790606332618632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/5836790606332618632'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/add-image-background-to-jframe.html' title='Add an image background to a JFrame'/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3034852670449113048.post-2267977017250719854</id><published>2010-11-02T10:37:00.000-07:00</published><updated>2010-11-02T10:37:14.243-07:00</updated><title type='text'>First blog ever</title><content type='html'>Hello everyone,&lt;br /&gt;
&lt;br /&gt;
As being new to blogs, it will take me some time to get accustomed to it. To introduce myself, I would say that I live in Canada and study in college at this moment. My college program is Computer Science and Mathematics and in the near future I am hoping to enter into University&#39;s Computer Science. Programming has always interested me (ever since I was 10), but I had never found the time and will to learn on my own up to the point I entered college. It is ironic in a way that I was &quot;lazy&quot; or did not have the right atmosphere for progressing in programming at an early age when I was interested in it, and for that, now I am trying to catch up and dedicate most of my time on it.&lt;br /&gt;
&lt;br /&gt;
I will try posting some tips on how to make certain things work in Java as these challenges occur to me, because every different project (at school or not) requires different techniques. Hopefully, this information would be usefull for other Java students like myself.</content><link rel='replies' type='application/atom+xml' href='http://yvesjava.blogspot.com/feeds/2267977017250719854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://yvesjava.blogspot.com/2010/11/first-blog-ever.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/2267977017250719854'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3034852670449113048/posts/default/2267977017250719854'/><link rel='alternate' type='text/html' href='http://yvesjava.blogspot.com/2010/11/first-blog-ever.html' title='First blog ever'/><author><name>Yves</name><uri>http://www.blogger.com/profile/08416050145014498784</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>