jpanel

Gradually accelerate sprite on key pressed; gradually decelerate on key released

你说的曾经没有我的故事 提交于 2019-11-29 15:46:51
I've been trying to figure out how I can gradually accelerate a sprite on key pressed and then once the key is released, gradually slow down to a stop, much like the ship in Asteroids . I would like to do this without any game engine if possible. I searched this up on SO and found related questions, but they did not answer my question exactly in my opinion. What I've thought of so far is: //while the key is being pressed //move by increasing y value //but continue to increase the amount y increases by as you hold this down, //until it reaches certain maxSpeed //when the key is released,

More than one JPanel in a Frame / having a brackground Image and another Layer with Components on the top

馋奶兔 提交于 2019-11-29 15:22:58
I've got a JFrame with a JPanel in which there is a JLabel with an ImageIcon(). Everything's working perfectly, problem is i now want to add another JPanel with all the other stuff like buttons and so on to the JFrame. But it still shows the background Image on top and nothing with the second JPanel. Can someone help me? Here is an extract of my code: JFrame window = new JFrame("Http Download"); /* * Background Section */ JPanel panel1 = new JPanel(); JLabel lbl1 = new JLabel(); /* * Component Section */ JPanel panel2 = new JPanel(); JLabel lbl2 = new JLabel(); /* * Dimension Section */

CardLayout in Java change by action in one of the 'cards'

不想你离开。 提交于 2019-11-29 15:12:50
I am making a simple game using a JFrame . I have made a simple "Start" screen which basically consists of a String and a JButton . I am picking up the button click with the actionPerformed(ActionEvent e) method. I don't know how to change the cards using a button click. This may seem like a simple problem to solve, but the twist comes with this: My main JFrame, my StartScreen and my JPanel where the game takes place are all in separate files. My main file, Virus.java, is where I create the JFrame . My file VirusGamePanel.java is where the game takes place. My file StartScreen.java is the

How does one make a component in the .CENTER of a Borderlayout occupy all center space and resize with the app?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 15:09:46
My app/JFrame, using Borderlayout, has a toolbar at the top or north, a statusbar at the bottom or south and a JPanel.JTabbedPane.JScrollPane.JTable in the center. The JPanel is always a fixed size which is roughly adjustable using the various set*Size() methods applied in various combinations to the various components. But it's always a fixed size and always has east and west gaps. The north and south components stay fixed height and resize horizontally as one would expect. Surely this is not a new or unique design. Is this normal behaviour? Is there some trick I've missed? This is

Set background color in Java on a JPanel doesn't work

佐手、 提交于 2019-11-29 14:31:51
I am working on a "paint-like" application (a little drawing software) to familiarize with Java 2D components. Here is my problem: I have a JFrame whose ContentPane is an instance of a class inheriting from JPanel. I want to set the background color to white but it remains on its default color... The class name corresponding to ContentPane is Container. Here is a simplified code: public class Container extends JPanel { public Container() { super(); this.setBackground(Color.WHITE); } } The JFrame constructor contains the line: this.setContentPane(mainContainer); Have I missed something? Thx.

Adding JPanel from another class to JPanel in JFrame

你说的曾经没有我的故事 提交于 2019-11-29 14:18:38
问题 I can't get my JFrame from main class to display JPanel from another class. Everything compiles without errors. JFrameTest.java: package jframetest; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class JFrameTest extends JFrame { public JFrameTest() { FlowLayout mainLayout = new FlowLayout(); setSize(320, 480); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(mainLayout); JPanel panelMain = new

Drawing 2 Balls to move in different direction on Java but one disappeared [closed]

浪子不回头ぞ 提交于 2019-11-29 13:06:26
I'm trying to create a program that would have 2 balls drawn, with one at the North center and the other at the south center. I am required to move the balls in different directions with the first ball at the North moving randomly towards south and the other ball at the south center to move towards the North. I can make the North Center ball move downwards but the second ball at the South disappears right after it is drawn. PS: I need to have 2 inner class which is Ball1 and Ball2 . Please help. Many thanks. Problems... while-loop in the Event Dispatching Thread which adjusts the positions of

Resizing Path2D circle by clicking and dragging its outer edge

允我心安 提交于 2019-11-29 13:05:21
So, when I click and drag a Path2D circle on my JPanel, it is resizing. The problem is that when I initially click-and-drag it, the circle jumps to a smaller size, but then resized correctly as you click and drag. The easiest way to demonstrate is to run the runnable code below. I know I need to fix this code: @Override public void mouseDragged(MouseEvent e) { int mouseX = e.getX(); int mouseY = e.getY(); if (resizing) { System.out.println("resizing"); Rectangle bounds = shapes.get(currentIndex).getBounds(); int shapeX = bounds.x; int shapeY = bounds.y; shapes.get(currentIndex).reset(); shapes

How to prevent JLabel positions from resetting?

我们两清 提交于 2019-11-29 12:58:05
I have a JPanel that contains 11 JLabels, each of them registered with a MouseMotionListener like so (generated by Netbeans): label1.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseMotionEvent evt){ label1MouseDragged(evt); } and the individual labelXMouseDragged methods contain (for example): label1.setLocation(label1.getParent().getMousePosition()); This Panel lives inside another Panel alongside various other controls. I find that I can drag my labels just fine inside the panel (I had my methods correctly checking for bounds, but I have left them like the

Passing the click event on one jPanel to another JPanel

五迷三道 提交于 2019-11-29 12:55:56
I have one outer panel, in the outer panel I have another jPanel placed on it. if i do a right click on the inner panel, outer panel's right click action should happen. if i do left click on the inner panel, its own inner panel's click action should happen. Is it possible to pass click event from one panel to another panel? There are a number issues you need to resolve for this to work. The first is understanding that a mouse event is contextual to the component that created it, in particular, the location information. The click point is the offset from the source components x/y position