Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Can anyone help trouble shoot my GUI?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a GUI that houses 6 buttons and a new Animator class (basically a label with a timer).

The buttons seem fine, but the Animator will not center horizontally in the remaining grid + the top of the label gets cut off.

Can Anyone see where I've gone wrong?


package darktower;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//import java.awt.Font;
//import TowerCode.*;

/**
*
* @author paul
*/
public class Tower extends JPanel {

JFrame TowerFrame;
final static TowerCode tc = new TowerCode();
final static String btnCaptions[] = {
"Yes/Buy", "Repeat", "No/End", "Haggle", "Bazaar", "Clear", "Tomb",
"Move", "Sanctuary", "DarkTower", "Frontier", "Inventory"};

final static Animator picLabel = new Animator(500);
//final static JLabel towerLCD = new JLabel("P1");


/** Creates a new instance of Tower */
public Tower(JFrame frame) {

super(new BorderLayout());
this.TowerFrame = TowerFrame;
setLayout(new GridLayout(2, 1));

//towerLCD.setHorizontalAlignment(JLabel.CENTER);
//towerLCD.setFont(new Font("SansSerif", Font.BOLD, 24));
//towerLCD.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
//add(towerLCD, BorderLayout.NORTH);
//picLabel.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
//picLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));

add(picLabel, BorderLayout.SOUTH);

JPanel TowerButtonsPanel = CreateTowerButtons();
TowerButtonsPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(TowerButtonsPanel, BorderLayout.SOUTH);

tc.SetUpGame();
}

private JPanel CreateTowerButtons(){
final JButton[] towerButtons = new JButton[btnCaptions.length];

for (int buttonNo=0;buttonNo<towerButtons.length; ++buttonNo) {
final int buttonNo2 = buttonNo;

towerButtons[buttonNo] = new JButton(btnCaptions[buttonNo]);

towerButtons[buttonNo].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (picLabel.picIndex == -1)
tc.ProcessButton(buttonNo2);
else System.out.println("press !OK");
}
});
}
return createPane("Whatever", towerButtons);
}

private JPanel createPane(String description, JButton[] TowerButtons) {
int NumButtons = TowerButtons.length;

JPanel NewBox = new JPanel();
JLabel NewLabel = new JLabel(description);

NewBox.setLayout(new GridLayout(0, 3));

for (int i = 0; i < NumButtons; i++)
NewBox.add(TowerButtons[i]);

return NewBox;
}

private static void CreateAndShowTowerGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
//JDialog.setDefaultLookAndFeelDecorated(true); not using dialogs

//Create and set up the window
JFrame MainTowerFrame = new JFrame("Dark Tower");
MainTowerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the 1st Controls container
Container MainTowerContainer = MainTowerFrame.getContentPane();
MainTowerContainer.setLayout(new GridLayout(1,1));

//Populate the MainContainer control
MainTowerContainer.add(new Tower(MainTowerFrame));

//Diplay The Window
MainTowerFrame.pack();
MainTowerFrame.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Schedule thread to show GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
CreateAndShowTowerGUI();
}
});
}
}






package darktower;

import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;

/**
*
* @author paul
*/
public class Animator extends JLabel {

public int picIndex = -1;
public int playIndex = -1;
public int pauseCycles = 0;
public boolean playOK = false;

public int digVal = 0;

final static TowerAudio ta = new TowerAudio();
final static TowerImages ti = new TowerImages();

public DisplayData[] animatorArray;

private Timer timer;
private int interval = 500;

/** Creates a new instance of Animator */
public Animator() {

}

ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//System.out.println("pauseCycles = " + pauseCycles);

if (pauseCycles > 0) {
pauseCycles--;
}
else {
if ((playOK) && (picIndex >= 0)) Animator.this.advanceFrame();
}
}
};

public Animator(int interval) {
super();

picIndex = -1;
playIndex = -1;

setText("TEST");
setVerticalTextPosition(JLabel.TOP);
setHorizontalTextPosition(JLabel.CENTER);
setFont(new Font("SansSerif", Font.BOLD, 24));

this.animatorArray = new DisplayData[10];
for (int aa = 0; aa < 10; ++aa) {
animatorArray[aa] = new DisplayData();
};
ClearDisplayData();

this.interval = interval;
this.timer = new Timer(interval, action);
this.timer.start();
}

public void advanceFrame() {
String picName;

System.out.println("picIndex = " + picIndex);
System.out.println("playIndex = " + playIndex);

//this.playIndex = (this.playIndex + 1) % images.length;
if (animatorArray[playIndex].soundNum > -1) {

int picNum = animatorArray[this.playIndex].picNum;
int soundNum = animatorArray[this.playIndex].soundNum;
String lCDStr = animatorArray[this.playIndex].lCDStr;


//System.out.println("pauseNum = " + animatorArray[this.playIndex].pauseNum);
//System.out.println("pauseNum = " + animatorArray[this.playIndex].pauseNum);

pauseCycles = animatorArray[this.playIndex].pauseNum*2+1;

if (picNum == -1)
picName = "C:/DT/images/" + "BLACK" + ".jpg";
else picName = "C:/DT/images/" + ti.IMAGE[picNum] + ".jpg";

setText(lCDStr);
ta.PlayAudio(soundNum);
super.setIcon(new ImageIcon(picName));
this.repaint();
playIndex++;
}
else {
picIndex = -1;
playIndex = 0;
ClearDisplayData();
playOK = false;
}
}

/*public static void main(String[] args) {
ImageIcon[] images = new ImageIcon[2];
images[0] = new ImageIcon("C:bazaar.jpg");
images[1] = new ImageIcon("C:beast.jpg");

Animator gif = new Animator(images, 500);
JFrame frame = new JFrame("Animator");
frame.setSize(200, 200);
frame.getContentPane().add(gif);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} */

public DisplayData[] NewDisplayArray(){
DisplayData[] displayArray = new DisplayData[10];

for (int i=0; i<displayArray.length; ++i) {
displayArray[i] = new DisplayData();
}

return displayArray;
}

public void ClearDisplayData() {
for (int td = 0; td < 10; ++td) {
animatorArray[td].soundNum = -1;
};
}


public void AddPic(int soundNum, int picNum, int pauseNum, String lCDStr) {
picIndex++;
if (picIndex == 0) playIndex = 0;

animatorArray[picIndex].lCDStr = lCDStr;
animatorArray[picIndex].picNum = picNum;
animatorArray[picIndex].soundNum = soundNum;
animatorArray[picIndex].pauseNum = pauseNum;
if (soundNum >= 0 )
animatorArray[picIndex].pauseNum += ta.audioLen[soundNum];

System.out.println("!!!");
System.out.println("picNum = " + picNum);
System.out.println("soundNum = " + soundNum);
System.out.println("pauseNum = " + animatorArray[picIndex].pauseNum);
System.out.println("!!!");

playOK = true;
}


public class DisplayData {
public int soundNum;
public int picNum;
public int pauseNum;
public String lCDStr;

}

}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code is, well, all over the place.

I'd suggest starting again, building the gui only.
do it in steps, compiling as you go.
when the gui is OK, add in the game/animation code.

here's your code just the gui part, with comments

 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding this line to your Animator constructor for centering its contents:

In the Tower class constructor, using a GridLayout will make the animator instance and the
buttonPanel the same size. This may be what you want. If not, you could add the
buttonPanel to the south section of a BorderLayout and add the animator instance to the
center. You can call setPreferredSize in the animator constructor or override the
getPreferredSize method in the (Animator) class to set the initial size as desired. This
will avoid resizing/visibility problems later as you animate through your images. Choose a
Dimension that will allow for all the Animator components (using largest image) to be
adequately displayed.
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers!

Cleaned up the code a little and got the label centred without cropping.

Regards

Joe.
 
Popeye has his spinach. I have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic