jpanel

How to animate Rectangle in JPanel?

主宰稳场 提交于 2019-12-01 13:07:41
I want to learn some tricks about JAVA for my project. I want to animate my Rectangle leftoright and righttoleft but I can't apply the same functions for ball animation. In addition,how can I start my ball in different x-direction with a border of y-coordinate ? Thanks a lot for your advices and helping. My codes: import javax.swing.Timer; import java.util.ArrayList; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MultipleBall extends JApplet { public MultipleBall() { add(new BallControl()); } class BallControl extends JPanel { private BallPanel ballPanel = new

I need to make my JPanel resize dynamically as new components are being added to it

不问归期 提交于 2019-12-01 13:03:39
I need to let users add more text fields to my JFrame so once the size of the frame has exceeded its original value a scroll pane would step in. Since I cannot add JScrollPane to JFrame in order to enable scrolling I decided to put the JPanel on the JFrame and pass the JPanel object into the JScrollPane constructor. Scrolling now works fine but only until it has reached the borders of the JPanel. The thing is the size of JPanel stays as is and is not stretching dynamically. What happens is the buttons in my code are using up all the space of the JPanel being the size of 300x300 but what I want

Drawing in JPanel vs JComponent

核能气质少年 提交于 2019-12-01 13:00:03
I need some help understanding why the drawing works differently in JComponent vs JPanel. import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class Particle extends JComponent implements Runnable{ private int x = 45; private int y = 45; private int cx; private int cy; private int size; private Color color; private JFrame frame; public Color getColor(){ return color = new Color(100,0,190); } public Particle(){ frame = new JFrame(); frame.setSize(400, 400); frame.setResizable

How to change images based on keystrokes

。_饼干妹妹 提交于 2019-12-01 12:43:47
问题 I'm creating a program for a school project. It is a fan based program of Pokémon and I am having a little trouble understanding how to change images based on the key strokes. Here is the code so far for the Character Class import java.awt.Image; import java.awt.event.KeyEvent; import java.io.*; //the File class import java.util.*; //the Scanner class import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.image.*; import javax.swing.ImageIcon; public class MainCharacter {

Lag spike when moving player

风格不统一 提交于 2019-12-01 12:30:23
The player is a panel, and it is getting removed, its position changed, and then re-added to another panel (which is what contains this method) which is drawn to the main frame. There are also a lot of other small panels containing a grass sprite being drawn to the primary panel as terrain tiles. I think the problem is that when I call revalidate() , it revalidates all those little panels as well. How can I solve this? EDIT: I should mention that I am using RelativeLayout to position the players on the primary panel. private class MainKeyAdapter extends KeyAdapter { @Override public void

Making Table With List Of JPanels

北城余情 提交于 2019-12-01 12:09:46
I need to a table in Java application. First I used to a Object of class JTable but my table has a lot of features and at now I try to use a list of JPanel components instead a table. How can I make a table with a list of panels? If you need to create a table composed of JPanel s containing JTextArea , start with something like: JPanel table = new JPanel(); table.setLayout(new BoxLayout(table, BoxLayout.X_AXIS)); for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { table.add(getRow(numberOfColumns)); } where getRow is defined by private Component getRow(int numberOfColumns) { JPanel

Components on the JPanel doesn't show

[亡魂溺海] 提交于 2019-12-01 12:07:11
I created a JPanel and i modify it a bit. I change it's background to gradient color here is the class. public class JGradientPanel extends JPanel { private static final int N = 32; private Color color1, color2; public JGradientPanel(Color color1, Color color2) { this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N)); this.color1 = color1; this.color2 = color2; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //Color color1 = getBackground(); //Color color2 = color1.darker(); int w = getWidth(); int h = getHeight();

How do I start optimising my Java code? - CPU is at 100%

六眼飞鱼酱① 提交于 2019-12-01 11:41:44
i have written an application, but for some reason it keeps peaking at 100%. I ran a profile r on a few of the classes and a report show that isReset() and isRunning() seems to be called alot of times. Do you see anything wrong please inform me. thanks Class 1 is the only class that uses the isReset() code so i hope this helps u guys in detecting the error Class 1 package SKA; /* * ver 0.32 June 2009 * * Bug Fix Release: * * Fixed Array resize * Fixed Red Black Tree delete method * Fixed Red Black Tree save/read option * Update help file * */ /* * Additions: * ver 0.30 May 2009 * * Added Red

Why I should use a JPanel?

℡╲_俬逩灬. 提交于 2019-12-01 11:28:07
What's the difference between: public class Test { public static void main(String[] args) { JButton button= new JButton("1"); button.setVisible(true); JPanel panel= new JPanel(); panel.add(button); panel.setVisible(true); JFrame frame = new JFrame(); frame.add(panel); frame.setVisible(true); frame.pack(); } } and public class Test { public static void main(String[] args) { JButton button= new JButton("1"); button.setVisible(true); JFrame frame = new JFrame(); frame.add(button); frame.setVisible(true); frame.pack(); } } I know that a JPanel is a container for GUI components but I really don't

Put a JTextfield on a JPanel?

試著忘記壹切 提交于 2019-12-01 11:18:45
问题 Why the textfield is not appearing on my panel which is inside my frame? I mean is there some additional action necessary to make the components of the panel visible? I hope somebody can help me.... public class example1 { public static void main(String[] args) { JFrame tt=new TT(); } } class TT extends JFrame { JTextField textField; JPanel panel; JButton button1; JButton button2; public TT() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null);