jpanel

addKeyListener() doesn't work for JPanel

守給你的承諾、 提交于 2019-11-26 20:38:00
I am trying to make a game engine. I have made the Game class but the error resides in the KeyBoard class. Here I leave some code. Class:: Game package transfer2pc.co.cc.game.tileengine; import java.awt.Graphics; import java.util.HashMap; import javax.swing.JPanel; import transfer2pc.co.cc.game.tileengine.input.KeyBoard; public abstract class Game extends JPanel implements Runnable { /** * */ private static final long serialVersionUID = 640206679500196209L; HashMap<String, ?> maps = null; KeyBoard keyBoard = null; public Game(){ super(); keyBoard = new KeyBoard(this); setKeyBoard(keyBoard);

get width and height of JPanel outside of the class

若如初见. 提交于 2019-11-26 18:43:16
问题 So I created a simple simple simulation where squares are spawned randomly with random vectors and bounce of the edges of the window. I wanted it to take into account the window being resized. So that if I change the dimensions of the window from 600x600 to 1200x600 the squares will bounce of the new border rather than 600x600. I tried doing getWidth() getHeight() but it would return 0. So I put it in the pain() (since it gets called on window resize) method and saved the return values as

How to Change java Cardlayout from another separate class

蓝咒 提交于 2019-11-26 18:36:36
问题 Please I have been trying to switch CardLayout from another class (JPanel) which is one of the card on the CardLayout, I have search and made research about this for a very long time but found nothing helpful. I have a CardLayout and two separate JPanels that I added to the CardLayout, now I want to be able to switch the cards after performing activities on the separate JPanel or separate class, so how do I switch the CardLayout from another class? my code below. package myApp; import java

Place JLabel on top of JLabel with image in

人走茶凉 提交于 2019-11-26 18:33:53
问题 I am pretty sure that this question has been asked before, but my case is slightly different as in i am trying to place a JLabel on top of a JLabel acting as a background, I want to display changing numbers using the JLabels and the numbers need to display over the background, however i am a bit of a swing n00b, thanks in advance, Jonathan 回答1: Without fully appreciating your requirements, if you simply need to display text over a background image, you'd be better off placing the label on top

Exporting a JPanel to an image

怎甘沉沦 提交于 2019-11-26 18:24:55
问题 So I've been trying to export an image that I've drawn on a JPanel into an image. I've been using this method: BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); paint(g); try { ImageIO.write(image, "png", new File([location goes here]); } catch (IOException e) {} I get an image in my intended location but I get a compressed version of what my JPanel shows. The same happens if I try to export a BMP as well. Is

Why is my JLabel not showing up

ぐ巨炮叔叔 提交于 2019-11-26 18:02:06
I am calling this method called check in one of my abstract classes but for some reason the JLabel (problem) I am adding to the JPanel (panel) is not showing up. Why is this occurring? Any explanations, I am using both the repaint, and validate methods but still nothing shows up. The problem you're having is you're blocking the Event Dispatching Thread, prevent the UI from been updated or any new events from been processed... It starts here... for(int i = 0; i < 15; i++) { //... //Check to see if user has enetered anything // And is compounded here while(!answered) { Thread.sleep(duration); //

Printing a JPanel with Scrollable Jtable On it [closed]

Deadly 提交于 2019-11-26 18:01:54
I just want to print a JPanel which has a scrollable JTable on it. I have oriented JPanel with JTable on it. But I can print only visible area. The contents in scrollable JTable area are not printed. There is a reason why I suggest using something like Jasper Reports, this has taken the better part of two days to nut out. JTable doesn't like been treated this way. Basically, there is the "UI view", which shows the data on the screen and the "print view" which used to generate the print out. UI View... Print view... This example, on A4 paper, will generate 22 pages... import java.awt

animate JPanel (slide in) with timer

南笙酒味 提交于 2019-11-26 17:52:12
I am trying to make a JPanel slide in from the side using this class i made: public class AnimationClass { private int i; private int y; private JPanel panel; private int xTo; private Timer timer; private int xFrom; synchronized void slidePanelInFromRight(JPanel panelInput, int xFromInput, int xToInput, int yInput, int width, int height) { this.panel = panelInput; this.xFrom = xFromInput; this.xTo = xToInput; this.y = yInput; panel.setSize(width, height); timer = new Timer(0, new ActionListener() { public void actionPerformed(ActionEvent ae) { for (int i = xFrom; i > xTo; i--) { panel

How do I paint multiple objetcs that move at different speeds in Java?

﹥>﹥吖頭↗ 提交于 2019-11-26 17:51:36
问题 I am working on homework for class, and its late because I can't seem to understand the material despite all the research that I am doing. I am a beginner and do not know much in the way of java. Also, this is my first post so please be forgiving when you are reading this. I am building on source code from my textbook, which I updated recently for past homework, but now I am trying to generate a class that draws multiple squares and moves those objects independently and at different speeds.

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

半世苍凉 提交于 2019-11-26 17:24:58
问题 So, I'm trying to make a basic functional menu for a simple game. I tried to do this by creating 2 JPanels, one for the actual game, and another for my menu. What I'm trying to do is have a button on my Menu panel that when pressed, switches the JPanel being displayed in the parent JFrame from that of the menu to that of the actual game. Here is my code: class Menu extends JPanel { public Menu() { JButton startButton = new JButton("Start!"); startButton.addActionListener(new Listener()); add