Question: How can I fill a Java rectangle with an image? I want to fill an entire JFrame with a jpeg/jpg image that sits in the
How can I fill a Java rectangle with an image? I want to fill an entire JFrame with a jpeg/jpg image that sits in the same working directory as my java files. My code is currently:
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; import javax.swing.JFrame;
public class CardTable extends JComponent{ private static final int WIDTH = 600; private static final int HEIGHT = 400; private Rectangle table; public CardTable(){ table = new Rectangle(0,0,WIDTH,HEIGHT); } public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.draw(table); } public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(600,400); frame.setTitle("Test Canvas"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CardTable sampleTable = new CardTable(); frame.add(sampleTable); frame.setVisible(true); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
