Java threads with Swing - java

I would like to illustrate a project about railroads.
I decided to use Swing.
I have a background map in a JPanel and I draw little circles that moves on the railroads.
It works perfectly if I have only one train, but I would like to add more trains.
Here is what I started to do (and works) :
public static void main(String[] args) {
// JFrame and background panel construction
JFrame frame = new JFrame();
JLayeredPane lpane = new JLayeredPane();
ImagePanel panelBg = new ImagePanel(new ImageIcon("map.jpg").getImage());;
frame.setPreferredSize(new Dimension(1791, 695));
frame.setLayout(new BorderLayout());
frame.add(lpane,BorderLayout.CENTER);
lpane.setBounds(0,0,panelBg.getImg().getWidth(null),panelBg.getImg().getHeight(null));
panelBg.setBounds(0,0,panelBg.getImg().getWidth(null),panelBg.getImg().getHeight(null));
panelBg.setOpaque(true);
lpane.add(panelBg, new Integer(0), 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
go(lpane,panelBg);
}
private static void go(JLayeredPane pan,ImagePanel panBg) {
Parcours panelP = new Parcours();
panelP.setBounds(0,0,panBg.getImg().getWidth(null),panBg.getImg().getHeight(null));
panelP.setOpaque(false);
pan.add(panelP, new Integer(1), 0);
for(int i=0; i<panelP.getTable().size(); i++){
panelP.setPosX(panelP.getTable().get(i).getX()-6);
panelP.setPosY(panelP.getTable().get(i).getY()-6);
panelP.repaint();
try{
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
"go" reads an ArrayList containing coordinates where my circle should go.
I really don't know how to create several trains.
Should I create several JPanels or only one with all my circles ?
If I remember well, I should use Threads but I tried to implement them and I can't start.
Thank you for your help

You could use a central data object that stores the trains.In every cycle the trains get drawn inside swing. On the other side the trains get updated from your threads.
Another approach contains train objects that get drawn and run a thread inside them to update them self.

Thank you Robin (see comments of my first post), Swing Timers seem to be the best solution so far.
I removed my awful Thread.sleep and set timers instead, it works, thanks again.
Thank you Templar too

Related

JTable only shows the right border of the table

I have a JTable and I want to make a tic tac toe themed table. I have managed to get rid of three borders without really knowing how...
Here's my code:
public class Table extends JPanel {
public static void main( String[] args ) {
JFrame jFrame = new JFrame();
JTable table = new JTable(3, 3);
table.setBounds(90, 20, 200, 200);
for(int i = 0; i < 3; i++) {
table.setRowHeight(i, 67);
}
table.setBackground(Color.black);
table.setGridColor(Color.white);
table.setForeground(Color.white);
// content pane so I can set the background
jFrame.getContentPane().add(table);
jFrame.getContentPane().setBackground(Color.black);
jFrame.setSize(400, 400);
jFrame.setLayout(null);
jFrame.setVisible(true);
}
}
I also have some stuff that initializes the table more like setting values but I don't think that will matter but I can add it if you want. So how could I get the right border of the table to disappear too?
When I compile this code, the top, left and bottom borders are not visible
You can usesetBorder method with a LineBorder in order to fill it:
table.setGridColor(Color.white);
table.setBorder(BorderFactory.createLineBorder(table.getGridColor()));
By the way, setLayout(null) and setBounds is a bad practice that is going to give you hard time in the future (plus it is not user-friendly, since you cannot resize your frame and won't work in different screen sizes). Try to use LayoutManagers and let them do the work for you.
Also, take a look at initial threads. All swing applications should run on their own thread and not in the main thread.

Closing only a single instance of JFrame?

So I have two classes, Main and MakeUserWindow, inside of my Main class I call MakeUserWindow several times with different parameters by using a loop. The only problem is, this creates several windows that overlap each-other (Which isn't that much of a deal, it's just that I can get 20 windows on top of each-other). What I thought of doing was simply using window.dispose(); right before recalling the instance, however, when I do that it closes all instances of the window. Not allowing me to recreate the instance using the same variable. Is there a way of closing only the single instance like window.close(); that I am unaware about, or am is there just a better way of doing this? I have searched for awhile before coming here, no results have helped.
For some reference, here is a simplified version of what I am doing
(MakeUserWindow is a class that extends JFrame)
MakeUserWindow newWindow;
for(stuff){
newWindow.dispose();
newWindow = new MakeUserWindow("parameters here");
}
EDIT---
The reason I initialize MakeUserWindow outside of the loop is because I need to use newWindow's properties.
Thanks for reading, -Zach.
I had tested it and this is what i got:
JFrame frame = new JFrame();
for (int i = 0; i < 5 ; i++) {
frame.dispose();
frame = new JFrame();
}
More or less like your code. Only the last frame survived cause you are closing the others when you do the ".dispose()". What you can do is a Map that keeps all the instances.
Map<String, JFrame> frames = new HashMap<String, JFrame>();
JFrame frame = new JFrame();
for (int i = 0; i < 5 ; i++) {
frame = new JFrame();
frames.put("Window" + i,frame);
}
And if you want to close a frame you do:
frames.get("WindowX").dispose();

java swing multiple jpanels

Basically all I want to do is draw two rows of buttons for a lights out game (homework), but I don't know how to make both panels show up. I've pretty much almost no graphics before, and I don't really understand anything I'm doing.
The panels them selves work, but it just shows whichever I add second (I assume it's overwriting the previous panel)
public static void main(String[] args) {
String nButtonsString = JOptionPane.showInputDialog("How many buttons would you like?");
int nButtons = Integer.parseInt(nButtonsString);
JFrame myFrame = new JFrame();
myFrame.setTitle("Linear Lights Out Game.");
myFrame.setSize(FRAME_SIZE);
JPanel control_buttons = new Linear_Controls();
myFrame.add(control_buttons);
JPanel lights = new LinearLightsPanel(nButtons);
myFrame.add(lights);
myFrame.pack();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.add(control_buttons);
myFrame.add(lights);
By default a JFrame uses a BorderLayout. Also, by default components get added to the CENTER of the BorderLayout. However the CENTER can only contain a single component so only the last component added is displayed. Try:
myFrame.add(control_buttons, BorderLayout.NORTH);
Now the two components should show up.
Read the section from the Swing tutorial on Using Layout Managers for more information and examples. Also take a look at the Trail/Table of Contents link to see other useful topics for basic Swing usage.
You have to create the 2 panels A and B and add them to another panel C. Then you add C to your frame.
A bit better explained: This is what you have now:
JPanel lights = new LinearLightsPanel(nButtons);
myFrame.add(lights);
But you would like something like:
JPanel lightsA = new LinearLightsPanel(nButtonsA);
JPanel lightsB = new LinearLightsPanel(nButtonsB);
JPanel lightsC = new JPanel();
lightsC.add(lightsA);
lightsC.add(lightsB);
myFrame.add(lightsC);

Java swing GUI creation

I'm developing a Java GUI and I need:
A label in first row(only one label).
Starting 2nd row need to add say 100 buttons which extends to multiple lines(width shouldn't go beyond the visible screen)
In a new line one more Label
From next line say 100 buttons which extends to multiple lines(width shouldn't go beyond the visible screen)...
[OPTIONAL] If the components exceeds JFrame height then need a scroll facility to the main window (only vertical)
I have a strange results with flow layout, sometimes it stick to visible width, sometimes it sets even 500 buttons in a single row.
I have tried every layout and also multipanes. Still no luck.
Please guide.. just need an idea, No need of code
Updated with code: Sorry guys, that was my first question to stackoverflow
Thanks for prompt response
Infact i tried many, here is a simple one.
setLayout(new FlowLayout());
setTitle("JAVA GUI");
setSize(500,500);
setVisible(true);
add(new JLabel("row 1"));
JPanel panel1 = new JPanel(new FlowLayout());
for(int i=0;i<200;i++){
panel1.add(new JButton("b"+i));
}
add(panel1);
Here the panel1 is appearing in a sigle row which goes beyond the visible part of the screen.
I think this can be solved by setting maximumsize to Jframe, but no idea how to set its size to FULL SCREEN.
You can try MigLayout.
http://www.miglayout.com/
Also this question is not really a question for stack overflow. A good way to ask your question would be to post your code and tell us what is wrong with it and what it is supposed to do.
While this is not the norm for 'good' stackOverflow questions, I don't have any problem with it myself. Some people cannot deal with anything except code. I would suggest that, if you're going to post code, that you take the trouble to post code that will compile, run, and demonstrate your situation. It really helps those of us out here understand what you're seeing and what you're trying to do.
You talk about "rows"; be aware that rows and columns are terms used with things like GridLayout and GridBagLayout, but I don't think they're appropriate for what you describe.
In your description, you don't say what you want scrolled. It would appear you want the entirety of the UI scrolled, I'll assume that for now.
I would try a JPanel with BoxLayout, oriented vertically, for the overall main UI. You will put some things into that:
The first JPanel.
Another JPanel, set with FlowLayout, holding the first bunch of buttons.
Another JPanel with the next JLabel
And a fourth JPanel, set with FlowLayout, holding the second bunch of buttons.
Now, I would put the top-level panel into a JScrollPane, and then put that into the CENTER section of a Frame (with its default BorderLayout), and see what happens. To tell the truth, I'm not sure, but these are the things I would start with.
I cannot tell, without running code, why you get odd behavior sometimes.
As said in a previous comment, using a ContentPane is the way to go. Here is a working example of what you want:
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JAVA GUI");
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
int nbLines = 10;
for (int i = 0; i < nbLines; i++) {
JPanel linePanel = new JPanel(new FlowLayout());
linePanel.add(new JLabel("row " + i));
for(int j = 0; j < 50; j++) {
linePanel.add(new JButton("b" + j));
}
panel1.add(linePanel);
}
frame.setContentPane(panel1);
//frame.setSize(500, 500);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
}
And here is what I get:
If you want to have left-aligned buttons you can use:
JPanel linePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

java swing repaint()

(second question in a few hours)
Kay so I'm making a chess variant in java, I have my console program working how I want it but now I'm trying to convert it to a swing GUI while STILL keeping the console things intact. So up to now I have my array of squares with pieces in them for the console, and a 2-dimensional array of JPanels with pieces in them for the GUI. I haven't implemented moving pieces in the GUI yet so I'm still doing it from the console but the actual GUI doesn't update after I've moved a piece...even though it does on the console (sorry if this is confusing).
The GUI consists of a constructor which calls some methods drawBoard() and drawSidebar() and sets sizes, titles etcetc...so this is what the main method looks like:
public static void main(String args[]) {
ChessGUI GUI = new ChessGUI();
Board console = new Board();
do {
console.printBoard();
console.getScore();
console.getMove();
GUI.boardPanel.revalidate();
GUI.sidePanel.revalidate();
GUI.repaint();
} while (true);
}
and drawBoard() incase it makes any difference:
public void drawBoard() {
LayoutManager layout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLS);
boardPanel.setLayout(layout);
boardPanel.setPreferredSize(new Dimension(200, 450));
chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for (int j = 0; j < NUMBER_OF_COLS; j++) {
chessBoard[i][j] = new JPanel();
chessBoard[i][j].setBackground(getColor(i,j));
int index = i * 4 + j;
if (!(boardArray.chessBoard[index].square.isEmpty())) {
Piece piece = (Piece) boardArray.chessBoard[index].square.firstElement();
chessBoard[i][j].add(new JLabel(piece.toString()));
}
boardPanel.add(chessBoard[i][j]);
}
}
}
the repaint and revalidate methods don't seem to be calling at all, even though the console is being updated :(
I don't really understand what you are doing. But it doesn't make sense to recreate the entire board panel every time a move is made. All Swing components can only have a single parent, to the easier solution is to just move the piece from one panel to the other. So the code would be something like:
previousPanel.remove( piece );
currentPanel.add( piece );
previousPanel.revalidate();
previousPanel.repaint();
currentPanel.revalidate();
It looks like you're never actually removing anything from 'boardPanel,' even though you are resetting its LayoutManager.
A safer approach might be to remove 'boardPanel' from its container, then create a new instance for 'boardPanel,' add that to the container, then add the other JPanel pieces to this new 'boardPanel.' Effectively, you would be reconstructing the entire JPanel hierarchy after every move.
As you've noticed, Swing can be quite finicky once you start trying to add/move/remove components after they've been added to containers. For games, often the best approach would be to have 1 JComponent/Component and use Java2D methods to draw on top of it. Swing is typically only used for forms-based applications.
Changing the layout doesn't do anything.
You need to call boardPanel.removeChildren()
However, this is going to be extremely slow.
Really, what you should be doing is have your own JPanel, overwrite paintComponent() and draw the images into the appropriate dimensions using Java Graphics.

Categories