snipt

Ctrl+h for KB shortcuts

Java

repeating image on jPanel

 public void paintComponent(Graphics g) {
        super.paintComponent(g);
    	int iw = image.getWidth(this);
        int ih = image.getHeight(this);
        for (int x = 0; x < getWidth(); x += iw) {
			for (int y = 0; y < getHeight(); y += ih) {
				g.drawImage(image, x, y, iw, ih, this);
			}
		}    
}
https://snipt.net/embed/57ba2918883c19251b5777552280b7e5/
/raw/57ba2918883c19251b5777552280b7e5/
57ba2918883c19251b5777552280b7e5
java
Java
10
2019-08-20T06:23:40
True
False
False
Nov 21, 2012 at 07:28 AM
/api/public/snipt/50581/
repeating-image-on-jpanel
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">paintComponent</span><span class="o">(</span><span class="n">Graphics</span> <span class="n">g</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">super</span><span class="o">.</span><span class="na">paintComponent</span><span class="o">(</span><span class="n">g</span><span class="o">);</span> </span><span id="L-3"><a name="L-3"></a> <span class="kt">int</span> <span class="n">iw</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="na">getWidth</span><span class="o">(</span><span class="k">this</span><span class="o">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="kt">int</span> <span class="n">ih</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="na">getHeight</span><span class="o">(</span><span class="k">this</span><span class="o">);</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">getWidth</span><span class="o">();</span> <span class="n">x</span> <span class="o">+=</span> <span class="n">iw</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="n">getHeight</span><span class="o">();</span> <span class="n">y</span> <span class="o">+=</span> <span class="n">ih</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-7"><a name="L-7"></a> <span class="n">g</span><span class="o">.</span><span class="na">drawImage</span><span class="o">(</span><span class="n">image</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">,</span> <span class="n">iw</span><span class="o">,</span> <span class="n">ih</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span> </span><span id="L-8"><a name="L-8"></a> <span class="o">}</span> </span><span id="L-9"><a name="L-9"></a> <span class="o">}</span> </span><span id="L-10"><a name="L-10"></a><span class="o">}</span> </span></pre></div> </td></tr></table>
gui