I am working on a school project on limited time, leading to obfuscated code and much confusion. I am trying to have two jFrames open, one displaying a chat interface, the other displaying an image. Upon a call to actionPerformed()
from the first window, I would like to call a method which changes the image that is on the second window, several times, with some delays in between. The image, however, does not change.
I think my issue has arose due to using a piece of example code for the text window, and trying to incorporate my own modifications, while not entirely understanding the former. In trying to look up this question, I only found people updating their jFrame based off of a timer and in actionPerformed()
, which is not really the behavior I desire.
Here is the offending code:
package main;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class Runner extends JPanel implements ActionListener {
JFrame fFrame = new JFrame("Leonardo");
protected JTextField textField;
protected JTextPane textArea;
private final static String newline = "\n";
Dictionary dict = new Dictionary();
StyleContext uContext = new StyleContext();
StyleContext rContext = new StyleContext();
Style uStyle;
Style rStyle;
JLabel lbl=new JLabel();
public Runner() {
super(new GridBagLayout());
styleInit();
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextPane();
textArea.setEditable(false);
textArea.setBackground(Color.BLACK);
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
StyledDocument doc = textArea.getStyledDocument();
try {
doc.insertString(doc.getLength(), "You: " + text + newline, uStyle);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textArea.setStyledDocument(doc);
try { //this is the section which is not working properly
changeLeonardo("Leonardo_Thinking.jpg");
repaint();
TimeUnit.SECONDS.sleep(1);
for(int i = 0; i<3; i++){
changeLeonardo("Leonardo_Talking1.jpg");
TimeUnit.SECONDS.sleep(1);
changeLeonardo("Leonardo_Talking2.jpg");
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
doc.insertString(doc.getLength(), "Leonardo: " + Logic.respondIntelligent(text, dict)+ newline, rStyle);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textArea.setStyledDocument(doc);
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private void styleInit(){
uStyle = uContext.addStyle("User", null);
uStyle.addAttribute(StyleConstants.Foreground, Color.WHITE);
uStyle.addAttribute(StyleConstants.FontFamily, Font.SANS_SERIF);
uStyle.addAttribute(StyleConstants.FontSize, new Integer(16));
rStyle = rContext.addStyle("Robot", null);
rStyle.addAttribute(StyleConstants.Foreground, Color.GREEN);
rStyle.addAttribute(StyleConstants.FontFamily, Font.MONOSPACED);
rStyle.addAttribute(StyleConstants.FontSize, new Integer(20));
}
private void changeLeonardo(String imgName){
BufferedImage img = null;
try {
img = ImageIO.read(new File("C:\\resources\\" + imgName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("C:\\resources\\" + imgName);
ImageIcon icon=new ImageIcon(img);
lbl.setIcon(icon);
revalidate();
repaint();
}
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Leonardo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fFrame.setLayout(new FlowLayout());
changeLeonardo("Leonardo_Idle.jpg");
frame.add(new Runner());
frame.pack();
frame.setVisible(true);
fFrame.pack();
fFrame.setVisible(true);
fFrame.add(lbl);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Runner runner = new Runner ();
runner.createAndShowGUI();
}
});
}
}
Some things to note:
Please excuse any odd names, such as fFrame
this is done to make the code reflect the school project better. All classes outside this, such as Logic or Dictionary work fine. The code throws no errors. I edited the filenames of the images for privacy, but they are full in the code.
I'm sure there are other issues abundant in this code, as I am typically not a graphics expert, but if you could please focus on the update issue, I would greatly appreciate it.
Thank you.