jpanel

Animated Sprites with Java Swing

旧时模样 提交于 2019-11-26 17:22:06
问题 Can someone tell me how can I slowdown the sprites appearance to create a more smooth animation? When I run the code it appears the last (27th) sprite in the JPanel. The animation processing is too fast! Someone told me about Swing Timer, but unfortunately I tried several times with that and I couldn't put the code to run well :( Here is the code that I have so far: package sprites; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage;

How to fit Image size to JFrame Size?

断了今生、忘了曾经 提交于 2019-11-26 16:54:34
问题 I have a JPanel into a JFrame . I loaded a picture on the JPanel but its shown just a part of the picture: This is the part of the code where i did it: JPanel panelImg = new JPanel() { public void paintComponent(Graphics g) { Image img = new ImageIcon("Welcome.png").getImage(); Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); setSize(size); setLayout(null); g.drawImage(img, 0, 0, null); } }; mainFrame

how to trigger an action in parent JPanel when a component in a child JPanel is updated (Java Swing)

﹥>﹥吖頭↗ 提交于 2019-11-26 16:47:03
问题 I am trying to build an MVC application in Java Swing. I have a JPanel that contains four JComboBoxes and this JPanel is embedded into a parent JPanel. The parent JPanel has other controls in addition to the child JPanel. The child JPanel's model gets correctly updated whenever I change the values of the JComboBoxes (it's basically a date picker with one combo box each for year, month, day of month, and hour of day). What I cannot figure out is how I can trigger the parent JPanel's model to

Can't a Swing component be added to multiple containers?

独自空忆成欢 提交于 2019-11-26 16:39:56
I'm trying (testing something else) to add one JButton reference into two JPanels to test it, and it disappears from the first panel it was added to! So, can't a Swing component be added to multiple containers? Thank you in advance. From: http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html : Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second. As you've discovered, you can't share components. However there

Display a jpg image on a JPanel

橙三吉。 提交于 2019-11-26 16:35:03
问题 What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel? Cheers. 回答1: ImageIcon image = new ImageIcon("image/pic1.jpg"); JLabel label = new JLabel("", image, JLabel.CENTER); JPanel panel = new JPanel(new BorderLayout()); panel.add( label, BorderLayout.CENTER ); 回答2: You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel. 回答3: I'd probably use an ImageIcon and set it on a

JPanel Drop Shadow

旧街凉风 提交于 2019-11-26 15:49:08
问题 I have a JPanel element and I would like added a drop shadow to it, how can I add a nice faded drop shadow to the element? Do I need to use external libraries or is there something that is built in that I can use? Example: 回答1: 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);

JPanel vs JFrame in Java

落爺英雄遲暮 提交于 2019-11-26 15:31:56
问题 I am learning Java gui. The way I learnt to create a window is to inherit or Extend JFrame class and it is good to use it, as JFrame contains all the properties of a Window. Now If I want to add something to this window, I need to use add() method. But Today I came across JPanel which also creates a windows and we can add stuff by jpanelObjec.add() . What is the difference between the two methods? Are they somehow related? 回答1: You should not extend the JFrame class unnecessarily (only if you

paintComponent() vs paint() and JPanel vs Canvas in a paintbrush-type GUI

删除回忆录丶 提交于 2019-11-26 15:27:51
I got some interesting ideas and criticism from this , this and this post (see last post for the code of the GUI in question). Nevertheless, I'm still quite confused about some things. Mainly, what is the least expensive way of displaying user-introduces graphics? More specifically, I used a paintComponent() method from JPanel class by making an object of this class in the MouseDragged() method together with paintComponent(getGraphics()) method ( AuxClass2 and AuxClass1 accordingly). Apparently, using getGraphics() and paintComponent() instead of repaint() are bad ideas, I suspect something to

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

删除回忆录丶 提交于 2019-11-26 14:43:07
问题 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

How to right-justify icon in a JLabel?

六眼飞鱼酱① 提交于 2019-11-26 14:42:58
问题 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