jpanel

Why does calling dispose() on Graphics object cause JPanel to not render any components

半城伤御伤魂 提交于 2019-11-27 09:29:32
After learning that dispose() should be called on Graphics / Graphics2D object after use, I went about changing my game to incorporate this. When I added g2d.dispose() in overridden paintComponent(Graphics g) of JPanel , my components which I added (extensions of JLabel class) where not rendered I was able to still click on them etc but they would not be painted . I tested with a normal JLabel and JButton with same effect (though JButton is rendered when mouse is over it). So my question is why does this happen? Here is an SSCCE to demonstrate: after uncommenting call to dispose() in

How to right-justify icon in a JLabel?

假如想象 提交于 2019-11-27 09:29:01
For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING) , the icon is painted right after text, no matter how wide the label is. This is particularly bad for a list, as the icons would be all over the place depending on how long the text is for each item. I traced the code and it seems to be that in SwingUtilities#layoutCompoundLabelImpl , text width is simply set to SwingUtilities2.stringWidth(c, fm, text) , and icon x is set to follow text without considering label width. Here is the simplest case: import java.awt.*; import javax.swing.*; public class TestJLabelIcon

Infinite background for game

梦想与她 提交于 2019-11-27 09:22:16
I am working on a Java project to simulate the flight of a helicopter in a frame. The helicopter moves on the screen using the arrow keys. I want the helicopter to be able to move infinitely, that is, when the helicopter reaches the edge of the frame, the background should move in the opposite direction to have the effect of endless terrain. Here is the code I have so far: import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.net.URL; import java.io.IOException; import javax.imageio.ImageIO; import javax

Using addMouseListener() and paintComponent() for JPanel

佐手、 提交于 2019-11-27 09:09:42
This is a follow-up to my previous question. I've simplified things as much as I could, and it still doesn't work! Although the good thing I got around using getGraphics() . A detailed explanation on what goes wrong here is massively appreciated. My suspicion is that something's wrong with the the way I used addMouseListener() method here. EDIT completely rewrote the code. Still does not work properly though. import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing

Java GUI repaint() problem?

强颜欢笑 提交于 2019-11-27 08:58:19
I have a JFrame. This JFrame contains a JButton. I click the JButton and 10 JTextFields are created. the problem: I cannot see them until "I force a repaint()" by resizing the window. Only then do I see the JTextFields created. CODE: JPanel points = new JPanel(); //Creating the JTextFields: for (int i=0; i<10; i++) { JTextField textField = new JTextField(); points.add(textField); } repaint(); this.repaint(); super.repaint(); points.repaint(); THANK YOU - after the for loop, I just called points.validate() and it worked... Tom Hawtin - tackline Container.add API docs sayeth: Note: If a

Java: Add Background image to frame [duplicate]

十年热恋 提交于 2019-11-27 08:52:15
问题 Possible Duplicate: java swing background image drawing your own buffered image on frame I am trying to add a back ground image to my frame, but nothing that I have done works. I designed a slot machine consisting of several panels added to the container. Now, I am trying to add a nice background to the frame. I tried using the paint method. But, since I am already using the paint method to paint the reel images, it is not working on the background. I also tried adding a JLabel, but when I do

How do I draw on a JPanel from multiple outside classes?

纵然是瞬间 提交于 2019-11-27 08:50:00
问题 I am currently making a game with a main menu and a world where you actually play. I have a class called Game , which inherits from JPanel and implements the Runnable , MouseListener , KeyListener and ActionListener interfaces (only important parts included) I also have two classes InWorldHandler and OutWorldHandler for handling the mechanics in the world and outside of it respectively. The Game class: public class Game extends JPanel implements Runnable, KeyListener, MouseListener,

JFrame with Background Image and a JPanel

六眼飞鱼酱① 提交于 2019-11-27 08:48:28
问题 I have the following: public class Frame { public static void main(String[] args) { JFrame frame = new JFrame("Frame Demo"); Panel panel = new Panel(); frame.add(panel); frame.setBounds(250,100,800,500); frame.setVisible(true); } } public class Panel extends JPanel { JButton singlePlayerButton; JButton multiPlayerButton; BufferedImage image; Gui gui; public Panel() { gui = new Gui(); add(gui); try { image = ImageIO.read(new File("C:\\Users\\void\\workspace\\BattleShips\\src\\Testumgebung\

Java swing, scrollbar does not working with absolute layout

本小妞迷上赌 提交于 2019-11-27 08:47:48
问题 I want to have a jpanel with fixed size without layout manager in jscrollpane (which will be also without layout manager). I cant use any layout manager because i need to create an rectangle/circle at a location where user clicks (and allow drag & drop for all the created emelents) setLocationRelativeTo(null); setSize(300,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setName("dnd test"); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h=ScrollPaneConstants.HORIZONTAL

JLabel won't show with JPanel.setLayout(null). Why?

房东的猫 提交于 2019-11-27 08:33:52
问题 I want to show many different labels over a map, so I'm using null layout in my panel, and calling setLocation for each label. For some reason, though, the labels don't show. If I remove the pan.setLayout(null), then the label appears in the top-center of the panel. Why isn't null layout working with setPosition? package mapa; import java.awt.*; import javax.swing.*; public class Mapa extends JFrame { private static JPanel pan; private static JLabel lab; public Mapa() { } private static void