(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.
Related
I have a problem with the optimisation of adding about 4 thousand newly created JPanel components to an already existing panel.
The problem is that, surprisingly, it's quite slow. It takes nearly 10 seconds to finish adding them all and that is quite slow for me.
public static void main(String[] args) {
JMenuItem mntmGenerateRandom = new JMenuItem("Generate random int");
mntmGenerateRandom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Random rand = new Random();
for(int i = 0; i < 4000; i++){
intPositions.add(rand.nextInt(4000));
System.out.println(i);
}
quicksort(0, intPositions.size()-1, intPositions);
long start = System.currentTimeMillis();
repaintPanels();
long stop = System.currentTimeMillis();
System.out.println("Done in " + (stop-start) + "ms");
}
});
}
private void repaintPanels(){
panelArray.clear();
ExecutorService service = Executors.newWorkStealingPool();
service.submit(new Runnable() {
public void run() {
for(int i = 0; i < intPositions.size(); i++){
panelArray.add(i, new JPanel());
panelArray.get(i).setBounds(intPositions.get(i), 1, 1, panelParent.getHeight()-2);
panelParent.add(panelArray.get(i), 1);
}
}
});
panelParent.repaint();
panelParent.revalidate();
}
Here's the simplified code, removed everything unnecessary. The slowest part is when I call panelParent.add(panelArray.get(i), 1);
Could anyone helped me make it any faster (if it's even possible)?
If Joop is going to guess at a solution, then so will I.
As I noted in comments a JPanel is typically used as a container as a component that holds other components, and you don't appear to be using it to this purpose but rather (to my eyes -- but I'm not 100% sure until you clarify your problem for us), to draw an image. If so, if what you're trying to do is to draw a changing image of blocks or links, one that changes as your sorting algorithm (or whatever you're trying to do in your model) changes, then why not instead use a single drawing JPanel, one whose paintComponent has been overridden, and whose paintComponent draws the state of the model as the model changes. This too is much lighter weight than what you're trying to achieve.
Use a JTable instead. It can use one single JComponent child (like a JPanel) to draw all cells, using a CellRenderer and a CellEditor.
This is a fly weight pattern, you also might implement yourself: drawing in one single JPanel all "panels."
I'm new to swing and I've been trying some new things. I'd like to create and place JLabels on a JPanel, but as many as I want, using a loop. I'm trying to make a snake game, btw.
/*Up here I set the number of parts (JLabels) I want and instantiate my ArrayList
to store them later.*/
snakeBodyParts = 3;
snakeBody = new ArrayList<>();
/*This is the part I'm struggling with. First, bodyPartIndex is going to go from 0
to my desired number of parts. On each time it's going to store an int for the X position
of the JLabel and an int for the Y position. That way I'm going to get
JLabels on a horizontal line, each time an unit farther away.*/
for (int bodyPartIndex = 0; bodyPartIndex < snakeBodyParts; bodyPartIndex++) {
snakePositionsX[bodyPartIndex] = 50 - (bodyPartIndex * UNIT_SIZE);
snakePositionsY[bodyPartIndex] = 50;
/*Then, I add a JLabel to my list, and get it back to my "bodyPart" JLabel.*/
snakeBody.add(new JLabel());
bodyPart = snakeBody.get(bodyPartIndex);
/*Here I set the location on my "bodyPart" JLabel, using the coordinates I created before,
and set its icon (it's out of the loop)*/
bodyPart.setLocation(snakePositionsX[bodyPartIndex], snakePositionsY[bodyPartIndex]);
bodyPart.setIcon(imgIconBodyPart);
/*And finally I replace the old bodyPart with the recently modified one, I then add it to my JPanel (this.add) */
snakeBody.set(bodyPartIndex, bodyPart);
this.add(bodyPart, new org.netbeans.lib.awtextra.AbsoluteConstraints(snakePositionsX[bodyPartIndex], snakePositionsY[bodyPartIndex], UNIT_SIZE, UNIT_SIZE));
}
I'm pretty sure I've done a lot of things wrong, I'm still trying to understand what I'm doing, but the end result is a blank screen, can anyone help me? I really need to know how to do this.
I need to write a strategic game which contains awt panel.
Scenario:-
Firstly, I need to read from a txt file(my map) and draw my images based on the txt file. (i did it in the code below).
public class temp extends JApplet implements Runnable,MouseListener {
public void init() {
setSize(1400,800 );
setBackground(Color.BLACK);
}
#Override
public void start() {
tilemap = new int[60][75];
filereader();
Thread thread = new Thread(this);
thread.start();
}
#Override
public void paint(Graphics g) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int mod_i = 100*i;
int mod_j = 50*j;
g.setColor(Color.red);
String str = String.valueOf(tilemap[i][j]);
g.drawString(str,mod_i,mod_j);
switch (tilemap[i][j]) {
case 1:
g.drawImage(asfalt,mod_i,mod_j,this);
break;
case 2://it means everywhere u read 2, so draw a tree
g.drawImage(tree,mod_i,mod_j,this);
break;
}
}
}
Secondly , i have to divide my window into 3 parts! (one biggest part, for main background, and 2 small part (one for mini map that will show where I am in the big map and one for showing information) )
Question:-
How can I divide it? Should i use panels or borders??
I also have to use containers, as my map is too big and I have to scroll it.
Any suggestions for this scenario? Thanks
It's probably not the best idea to write everything in one class. You should definitely store your Filereader in another class.
By the way, do not mix awt and Swing. Either use JApplet and JPanel or Applet and Panel.
Why do you want to use borders? You can use Borders additionally, but that has nothing to do with your structure.
Make a new JPanel where you do all your "main background"-paintig stuff. Call setContentPane of your JApplet to add this JPanel.
Use for example BorderLayout for this JPanel. Create 2 more JPanels for map and information and simply add these JPanels to the main JPanel.
Use the setOpaque - Method of JPanel, if you need transparent background for a JPanel.
If your map (or your mini-map) is static, it might be a good idea, to draw your Images once to a BufferedImage and just draw the BufferedImage.
I am making a program where you choose a selection sort or a merge sort using JButtons and it sorts an int array in the form of a bar graph using Graphics where each element in the array is a bar.
but for some reason the compiler isn't receiving my button presses, i have tried to use
(selection.equals(e.getSource()) in the if statement but it isnt working, am i missing something obvious or what?
public class Animation extends Canvas implements ActionListener{
JPanel panel;
JButton Selection;
JButton Merge;
boolean selection, merge;
int[] random = new int[25];
Sorter sort = new Sorter();
public Animation(){
Selection = new JButton("Selection sort");
Selection.addActionListener(this);
Selection.setActionCommand("select");
Merge = new JButton("Merge sort");
Merge.addActionListener(this);
Merge.setActionCommand("merge");
panel = new JPanel();
panel.add(Selection);
panel.add(Merge);
setBackground(Color.WHITE);
selection=false;
merge=false;
}
public void actionPerformed(ActionEvent e) {
if("select".equals(e.getActionCommand())){
selection = true;
repaint();
}
else if("merge".equals(e.getActionCommand())){
merge = true;
repaint();
}
}
public void paint (Graphics window){
Random r = new Random();
for(int i=0; i<random.length; i++){
int randomInt = r.nextInt(100) + 1;
random[i] = randomInt;
}
window.setColor(Color.MAGENTA);
if(selection==true){
for(int i=0; i< random.length-1; i++){
int smallest = i;
for(int j = i+1; j< random.length; j++){
if(random[j] < random[smallest])
smallest = j;
}
if( smallest != i) {
int least = random[smallest];
random[smallest] = random[i];
random[i] = least;
drawIt(random, window);
window.setColor(Color.WHITE);
drawIt(random, window);
window.setColor(Color.MAGENTA);
}
}
}
drawIt(random, window);
}
public void drawIt (int[] a, Graphics window1){
int x=128;
int height = 200;
for(int i=0; i<a.length; i++){
window1.drawLine(x, 200, x, height-a[i]);
window1.drawLine(x+1, 200, x+1, height-a[i]);
window1.drawLine(x+2, 200, x+2, height-a[i]);
x+=20;
}
try {
Thread.currentThread().sleep(100);
} catch(Exception ex) {
}
}
}
heres the main class to run it:
public class AnimationRunner extends JFrame{
private static final int WIDTH = 800;
private static final int HEIGHT = 250;
JButton Selection;
JButton Merge;
public AnimationRunner()
{
super("Sorting Animation");
setSize(WIDTH,HEIGHT);
Animation a = new Animation();
Merge = new JButton("Merge sort");
Selection = new JButton("Selection sort");
Merge.setSize(120, 30);
Selection.setSize(120,30);
Merge.setLocation(200, 30);
Selection.setLocation(400, 30);
this.add(Merge);
this.add(Selection);
((Component)a).setFocusable(true);
getContentPane().add(new Animation());
setVisible(true);
}
public static void main( String args[] )
{
AnimationRunner run = new AnimationRunner();
}
}
You create a button for each action in your main class and add these to your JFrame. You also create a two instances of your Animation class. One which you create, setfocusable then do nothing with. Then another which you create and add to the contentPane of the JFrame.
In your Animation constructor you again create a button for each action, this time setting the action commands. You then add these to a panel. This panel is never added to anything and so these buttons will never be seen.
The buttons you see are not the buttons that you have defined the action commands for.
Also you should avoid using setSize() and Use Layout Managers to defines the sizes of your components.
There are a series of cascading problems...
In the AnimationRunner class you create two JButtons called Merge sort and Selection sort and add these to the main frame. This is what's actually on the screen. This buttons have no listeners attached, therefore never notify any body when they are clicked...
In the Animation class you create two JBttonss called Merge sort and Selection sort and add these to panel (and instance of JPanel), which is never added to anything. This means you can never possibly click them...
You don't seem to have an understanding of how painting works in Swing and seem to be assuming that you control the paint process in some way.
Painting is controlled by the paint sub system in Swing, which schedules and performs paint cycles when and where it sees fit. This means that your paint method may be called for any number of reasons, many of which you don't control.
Remove the logic of the sort out of the paint process and place into some kind of model, whose state you can control. Then use the custom painting capabilities to render the state of the model.
paint is an inappropriate method to be using for custom painting and you should be using paintComponent. You have broken the paint chain which may prevent the component from rendering child components and/or introduce series paint artifacts into your program
Take a look at Performing Custom Painting and Painting in AWT and Swing for more details
Swing is a single threaded framework work. That means that anything that blocks the Event Dispatching Thread will prevent it from process new repaint requests or events into the system. This will cause your program to look like it's "hung". In your case, you will most likely only ever see the end result of the painting process...after a short delay.
Instead, consider using a javax.swing.Timer to introduce a safe delty and update the model each time it ticks.
Take a look at Concurrency in Swing and How to use Swing Timers for more details
Swing programs are expected to run on a variety of hardware and software platforms, all with there own notions of DPI and font rendering approaches. This makes it very difficult to accommodate your design to all the possible needs of these systems.
Swing makes this easier by providing a layout management API, which takes the fiddle work out of making these decisions. Take a look at Laying Out Components Within a Container for more details
You should also take a look at Code Conventions for the Java Programming Language, it will make it eaiser for people to read your code.
You might find this example of some benifit
So i was wondering I'm new to java but I know my way around it but I wanted to make a 2d tile game.
Now I heard that you can do this with a 2d Array to make the map.
But how do you make the map appear on the screen, JFrame, as pictures?
So an example of the array/map here:
1111111111
1011011001
1001100011
0000100011
0000000000
2222222222
0 = blueSky.png
1 = cloud.png
2 = grass.png
Thanks!
EDIT 2
So i have now this:
import javax.swing.*;
import java.awt.*;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
ImageIcon sky = new ImageIcon ("/Users/pro/Desktop/sky.png");
JPanel grid = new JPanel();
grid.setLayout(new GridLayout(25, 25));
for (int i = 0; i < 25; i++) {
for (int n = 0; n < 25; n++) {
grid.add(new JLabel(sky));
}
}
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setPreferredSize(new Dimension(640, 400));
frame.add(grid);
frame.pack();
frame.setVisible(true);
}
}
this prints some tiles with the sky picture but how do i make the bottom row an other picture?
I'd consider the 2D array as the non-GUI model, that the data itself would likely be held in some data file, perhaps a text file, that there would be methods for reading the data in to be held by a 2D array of perhaps ints, perhaps custom Cell classes (again, still all non-GUI). Then the GUI would have to interpret the model and display the data in some logical way. This could perhaps be done by creating a 2D grid of JLabels held by a JPAnel that uses GridLayout, and then use ImageIcons to hold the images, and set the icon of each JLabel based on the state of the model.
Edit 1
So possible classes used include:
TileType: an enum that associates the tile concept with the numbers held by the data file
TileCell: non-GUI class, holds a TileType field, also may hold a List of items that can be found on the cell (if the game needs this). May have information about its neighbors.
TileCellGrid: non-GUI class holding a 2D grid of TileCells.
GridDataIO: utility class to read in and write out grid data to a file.
GameGrid: GUI class that would hold a GridLayout using JPanel that holds JLabels whose ImageIcons display the images you list in your OP.
Edit 2
regarding your question:
Alright how can i set the right picture for everyLabel ?
I would use an observer/observable pattern and add a listener to the model. Whenever the model changes it should thus notify the GUI or view. Then the view would request the data array, would iterate through it and would change the image icons that need changing as it loops through the array.
I suggest you use JLabels with icons and lay them out using GridLayout.
Related question / answer with sample code and screen shot:
Tiled images in swing