JPanel Drop Shadow

So I looked into swingx which extends JPanel and was able to achieve the results I was looking for with the following code: public class Canvas extends JXPanel{ public Canvas(){ DropShadowBorder shadow = new DropShadowBorder(); shadow.setShadowColor(Color.BLACK); shadow.setShowLeftShadow(true); shadow.setShowRightShadow(true); shadow.setShowBottomShadow(true); shadow.setShowTopShadow(true); this.setBorder(shadow); } } And the result:

How do I draw an image to a JPanel or JFrame?

Try this: package com.sandbox; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class SwingSandbox { public static void main(String[] args) throws IOException { JFrame frame = buildFrame(); final BufferedImage image = ImageIO.read(new File(“C:\\Projects\\MavenSandbox\\src\\main\\resources\\img.jpg”)); JPanel pane = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); … Read more

Image resizing and displaying in a JPanel or a JLabel without loss of quality

Of course you can resize the image there are many different ways like Image#getScaledInstance(int width,int height,int hints), but this has its perils. The main problem being: Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used. I would not recommend … Read more

How to draw grid using swing class Java and detect mouse position when click and drag

There are any number of ways to get this to work, depending on what it is you want to achieve. This first example simply uses the 2D Graphics API to render the cells and a MouseMotionListener to monitor which cell is highlighted. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import … Read more

How to switch JPanels in a JFrame from within the panel?

Like so : import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class CardLayoutDemo extends JFrame { public final String YELLOW_PAGE = “yellow page”; public final String RED_PAGE = “red page”; private final CardLayout cLayout; private final JPanel mainPane; boolean isRedPaneVisible; public CardLayoutDemo(){ setTitle(“Card Layout Demo”); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); mainPane … Read more