Obtaining focus on a JPanel
Although you’re indicating that the panel can be focusable, the panel isn’t asking for focus. Try using myPanel.requestFocus();.
Although you’re indicating that the panel can be focusable, the panel isn’t asking for focus. Try using myPanel.requestFocus();.
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:
Instead of having a single JPanel, why don’t you try with a CardLayout and switch whether to show the InnerWorld or the OuterWorld according to a flag that determines where in the program you’re at. As you’re implementing KeyListener, I think that’s for you to be able to move your character, please take a look … Read more
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
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
The Basic Structure: The extends keyword means that DrawPanel inherits from JPanel. In other words, DrawPanel “is a” JPanel. As such, it can override its methods (the ones that aren’t marked final). You might want to do that for several reasons. For example, you might want to gain access to the panel’s Graphics class, which … Read more
Set an EmptyBorder around your JPanel. Example: JPanel p =new JPanel(); p.setBorder(new EmptyBorder(10, 10, 10, 10));
In order to reflect changes after adding/removing or resizing a component that is on a visible container call revalidate() and repaint() on the containers instance after adding/removing or resizing the component. Though this will not work in your code the main reason being inside JButton classes you recreate a new instance of MainPanel when in … Read more
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
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