I am trying to make a simple barcode scanner project for fun. And I've run into a slight problem. I am using zXing and Webcam Capture for this.
Even if a Barcode is present in the picture, Java keeps telling me none is found through the NotFoundException. I look for a frame every time webcamImageObtained is run (which I assume is every frame?) and then I look for a barcode in the frame that I captured.
I took this picture with that webcam (Ironically using the code hah):
When I hover over this barcode it reports about 30 images per second and otherwise about 7-8 when it looks at me from my screen (if that means anything).
Whenever I find a code, I want to add it to a JList (not accounting for duplicates and the likes yet).
I call this code every time webcamImageObtained(WebcamEvent we) fires:
#Override
public void webcamImageObtained(WebcamEvent we) {
BufferedImage myImage;
try {
myImage = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(myImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = reader.decode(bitmap);
DefaultListModel dlm = (DefaultListModel) list.getModel();
dlm.addElement(result.toString());
list.setModel(dlm);
} catch (NotFoundException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (ChecksumException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (FormatException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here is the entire class:
package sandbox_webcam;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDiscoveryEvent;
import com.github.sarxos.webcam.WebcamDiscoveryListener;
import com.github.sarxos.webcam.WebcamEvent;
import com.github.sarxos.webcam.WebcamListener;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamPicker;
import com.github.sarxos.webcam.WebcamResolution;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class AdvancedWebcamPanelExample extends JFrame implements Runnable, WebcamListener, WindowListener, UncaughtExceptionHandler, ItemListener, WebcamDiscoveryListener {
private Webcam webcam = null;
private WebcamPanel panel = null;
private WebcamPicker picker = null;
private JButton button = null;
private JList list = null;
private ActionListener buttonListener = null;
private com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader();
#Override
public void run() {
Webcam.addDiscoveryListener(this);
setTitle("Java Webcam Capture POC");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addWindowListener(this);
picker = new WebcamPicker();
picker.addItemListener(this);
webcam = picker.getSelectedWebcam();
if (webcam == null) {
System.out.println("No webcams found...");
System.exit(1);
}
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(AdvancedWebcamPanelExample.this);
panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);
buttonListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (webcam != null) {
BufferedImage image = webcam.getImage();
JFileChooser filechooser = new JFileChooser();
int saveValue = filechooser.showDialog(button, "Save");
if (saveValue == JFileChooser.APPROVE_OPTION) {
try {
File f = filechooser.getSelectedFile();
ImageIO.write(image, "png", new File(f.getAbsolutePath() + ".png"));
System.out.println("Picture saved at: " + f.getAbsolutePath());
} catch (IOException ex) {
System.err.println("Failed to save the picture!");
ex.printStackTrace();
}
}
} else {
System.err.println("no webcam found to take a picture");
}
}
};
button = new JButton("Snap a Picture!");
button.addActionListener(buttonListener);
list = new JList();
list.setMinimumSize(new Dimension(200,this.getHeight()));
add(picker, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
add(list, BorderLayout.EAST);
pack();
setVisible(true);
Thread t = new Thread() {
#Override
public void run() {
panel.start();
}
};
t.setName("example-starter");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}
#Override
public void webcamOpen(WebcamEvent we) {
System.out.println("webcam open");
}
#Override
public void webcamClosed(WebcamEvent we) {
System.out.println("webcam closed");
}
#Override
public void webcamDisposed(WebcamEvent we) {
System.out.println("webcam disposed");
}
#Override
public void webcamImageObtained(WebcamEvent we) {
BufferedImage myImage;
try {
myImage = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(myImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = reader.decode(bitmap);
DefaultListModel dlm = (DefaultListModel) list.getModel();
dlm.addElement(result.toString());
list.setModel(dlm);
} catch (NotFoundException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (ChecksumException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (FormatException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void windowOpened(WindowEvent e) {
// do nothing
}
#Override
public void windowClosing(WindowEvent e) {
// do nothing
}
#Override
public void windowClosed(WindowEvent e) {
webcam.close();
}
#Override
public void windowIconified(WindowEvent e) {
System.out.println("webcam viewer paused");
panel.pause();
}
#Override
public void windowDeiconified(WindowEvent e) {
System.out.println("webcam viewer resumed");
panel.resume();
}
#Override
public void windowActivated(WindowEvent e) {
// do nothing
}
#Override
public void windowDeactivated(WindowEvent e) {
// do nothing
}
#Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println(String.format("Exception in thread #s", t.getName()));
e.printStackTrace();
}
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getItem() != webcam) {
if (webcam != null) {
panel.stop();
remove(panel);
webcam.removeWebcamListener(this);
webcam.close();
webcam = (Webcam) e.getItem();
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(this);
System.out.println("selected " + webcam.getName());
panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);
add(panel, BorderLayout.CENTER);
pack();
Thread t = new Thread() {
#Override
public void run() {
panel.start();
}
};
t.setName("example-stopper");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}
}
}
#Override
public void webcamFound(WebcamDiscoveryEvent event) {
if (picker != null) {
picker.addItem(event.getWebcam());
}
}
#Override
public void webcamGone(WebcamDiscoveryEvent event) {
if (picker != null) {
picker.removeItem(event.getWebcam());
}
}
}
Am I missing something about how this library scans for a barcode?
EDIT
Not sure this helps much..
Mar 02, 2015 10:04:34 PM sandbox_webcam.AdvancedWebcamPanelExample webcamImageObtained
SEVERE: null
com.google.zxing.NotFoundException
Throws exception here:
Result result = reader.decode(bitmap);
There is a different question which has some available answers: Android zxing NotFoundException
As James said, it is a good idea to try with bar codes on different media (paper/screen) if it is not working, and in different circumstances and lighting conditions. Particularly ensure that you have enough light, and that the FPS of the camera is high enough while it is pointed at the barcode.
For debugging, one could also convert the BinaryImage back into a viewable format and check whether the barcode is actually visible after conversion to black-and-white.
Related
I am using ffmpeg java library to convert captured screenshots to video. Video which is generated as output is blurry.
I am using bit rate as 9000, frames per sec as 25 and video size as that of desktop screen size.
Any suggestions on how to solve this issue.
P.S. I cannot use ffmpeg.exe and command line due to certain restrictions and hence I am opting for ffmpeg java library.
Any suggestions on the issue or suggestions on any better approach will be helpful.
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
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.Date;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.OpenCVFrameConverter;
public class ScreenRecorder{
public static boolean videoComplete=false;
public static String inputImageDir="inputImgFolder"+File.separator;
public static String inputImgExt="png";
public static String outputVideo="recording.mp4";
public static int counter=0;
public static int imgProcessed=0;
public static FFmpegFrameRecorder recorder=null;
public static int videoWidth=1920;
public static int videoHeight=1080;
public static int videoFrameRate=3;
public static int videoQuality=0; // 0 is the max quality
public static int videoBitRate=9000;
public static String videoFormat="mp4";
public static int videoCodec=avcodec.AV_CODEC_ID_MPEG4;
public static Thread t1=null;
public static Thread t2=null;
public static JFrame frame=null;
public static boolean isRegionSelected=false;
public static int c1=0;
public static int c2=0;
public static int c3=0;
public static int c4=0;
public static void main(String[] args) {
try {
if(getRecorder()==null)
{
System.out.println("Cannot make recorder object, Exiting program");
System.exit(0);
}
if(getRobot()==null)
{
System.out.println("Cannot make robot object, Exiting program");
System.exit(0);
}
File scanFolder=new File(inputImageDir);
scanFolder.delete();
scanFolder.mkdirs();
createGUI();
} catch (Exception e) {
System.out.println("Exception in program "+e.getMessage());
}
}
public static void createGUI()
{
frame=new JFrame("Screen Recorder");
JButton b1=new JButton("Select Region for Recording");
JButton b2=new JButton("Start Recording");
JButton b3=new JButton("Stop Recording");
JLabel l1=new JLabel("<html><br/>If you dont select a region then full screen recording <br/> will be made when you click on Start Recording</html>");
l1.setFont (l1.getFont ().deriveFont (20.0f));
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(frame, "A new window will open. Use your mouse to select the region you like to record");
new CropRegion().getImage();
} catch (Exception e1) {
// TODO Auto-generated catch block
System.out.println("Issue while trying to call the module to crop region");
e1.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
counter=0;
startRecording();
}
});
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
stopRecording();
System.out.print("Exiting...");
System.exit(0);
}
});
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(l1);
frame.setLayout(new FlowLayout(0));
frame.setVisible(true);
frame.setSize(1000, 170);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void startRecording()
{
t1=new Thread()
{
public void run() {
try {
takeScreenshot(getRobot());
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Cannot make robot object, Exiting program "+e.getMessage());
System.out.println("Cannot make robot object, Exiting program "+e.getMessage());
System.exit(0);
}
}
};
t2=new Thread()
{
public void run() {
prepareVideo();
}
};
t1.start();
t2.start();
System.out.println("Started recording at "+new Date());
}
public static Robot getRobot() throws Exception
{
Robot r=null;
try {
r = new Robot();
return r;
} catch (AWTException e) {
JOptionPane.showMessageDialog(frame, "Issue while initiating Robot object "+e.getMessage());
System.out.println("Issue while initiating Robot object "+e.getMessage());
throw new Exception("Issue while initiating Robot object");
}
}
public static void takeScreenshot(Robot r)
{
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rec=new Rectangle(size);
if(isRegionSelected)
{
rec=new Rectangle(c1, c2, c3-c1, c4-c2);
}
while(!videoComplete)
{
counter++;
BufferedImage img = r.createScreenCapture(rec);
try {
ImageIO.write(img, inputImgExt, new File(inputImageDir+counter+"."+inputImgExt));
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Got an issue while writing the screenshot to disk "+e.getMessage());
System.out.println("Got an issue while writing the screenshot to disk "+e.getMessage());
counter--;
}
}
}
public static void prepareVideo()
{
File scanFolder=new File(inputImageDir);
while(!videoComplete)
{
File[] inputFiles=scanFolder.listFiles();
try {
getRobot().delay(500);
} catch (Exception e) {
}
//for(int i=0;i<scanFolder.list().length;i++)
for(int i=0;i<inputFiles.length;i++)
{
//imgProcessed++;
addImageToVideo(inputFiles[i].getAbsolutePath());
//String imgToAdd=scanFolder.getAbsolutePath()+File.separator+imgProcessed+"."+inputImgExt;
//addImageToVideo(imgToAdd);
//new File(imgToAdd).delete();
inputFiles[i].delete();
}
}
File[] inputFiles=scanFolder.listFiles();
for(int i=0;i<inputFiles.length;i++)
{
addImageToVideo(inputFiles[i].getAbsolutePath());
inputFiles[i].delete();
}
}
public static FFmpegFrameRecorder getRecorder() throws Exception
{
if(recorder!=null)
{
return recorder;
}
recorder = new FFmpegFrameRecorder(outputVideo,videoWidth,videoHeight);
try
{
recorder.setFrameRate(videoFrameRate);
recorder.setVideoCodec(videoCodec);
recorder.setVideoBitrate(videoBitRate);
recorder.setFormat(videoFormat);
recorder.setVideoQuality(videoQuality); // maximum quality
recorder.start();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(frame, "Exception while starting the recorder object "+e.getMessage());
System.out.println("Exception while starting the recorder object "+e.getMessage());
throw new Exception("Unable to start recorder");
}
return recorder;
}
public static OpenCVFrameConverter.ToIplImage getFrameConverter()
{
OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
return grabberConverter;
}
public static void addImageToVideo(String imgPath)
{
try {
getRecorder().record(getFrameConverter().convert(cvLoadImage(imgPath)));
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Exception while adding image to video "+e.getMessage());
System.out.println("Exception while adding image to video "+e.getMessage());
}
}
public static void stopRecording()
{
try {
videoComplete=true;
System.out.println("Stopping recording at "+new Date());
t1.join();
System.out.println("Screenshot thread complete");
t2.join();
System.out.println("Video maker thread complete");
getRecorder().stop();
System.out.println("Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());
JOptionPane.showMessageDialog(frame, "Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());
} catch (Exception e) {
System.out.println("Exception while stopping the recorder "+e.getMessage());
}
}
}
Imagepanel.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
class ImagePanel
extends JPanel
{
private Image img;
public ImagePanel(String img)
{
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g)
{
g.drawImage(this.img, 0, 0, null);
}
}
CropRegion.java
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CropRegion implements MouseListener,
MouseMotionListener {
int drag_status = 0;
int c1;
int c2;
int c3;
int c4;
JFrame frame=null;
static int counter=0;
JLabel background=null;
public void getImage() throws AWTException, IOException, InterruptedException {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
ImagePanel panel = new ImagePanel(img);
frame=new JFrame();
frame.add(panel);
frame.setLocation(0, 0);
frame.setSize(size);
frame.setLayout(new FlowLayout());
frame.setUndecorated(true);
frame.setVisible(true);
frame.addMouseListener(this);
frame.addMouseMotionListener(this);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void draggedScreen() throws Exception {
ScreenRecorder.c1=c1;
ScreenRecorder.c2=c2;
ScreenRecorder.c3=c3;
ScreenRecorder.c4=c4;
ScreenRecorder.isRegionSelected=true;
JOptionPane.showMessageDialog(frame, "Region Selected.Please click on Start Recording button to record the selected region.");
frame.dispose();
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
paint();
this.c1 = arg0.getX();
this.c2 = arg0.getY();
}
public void mouseReleased(MouseEvent arg0) {
paint();
if (this.drag_status == 1) {
this.c3 = arg0.getX();
this.c4 = arg0.getY();
try {
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void mouseDragged(MouseEvent arg0) {
paint();
this.drag_status = 1;
this.c3 = arg0.getX();
this.c4 = arg0.getY();
}
public void mouseMoved(MouseEvent arg0) {
}
public void paint() {
Graphics g = frame.getGraphics();
frame.repaint();
int w = this.c1 - this.c3;
int h = this.c2 - this.c4;
w *= -1;
h *= -1;
if (w < 0) {
w *= -1;
}
g.drawRect(this.c1, this.c2, w, h);
}
}
Basically what I'm doing is creating a swing application that acts as a launcher. All it does is gives the user 3 options they can choose from to open a new java application. The 3 different java applications all have different themes, and one doesn't have a theme at all. I'm trying to get it so when I select an option, my launcher app doesn't repaint it self to what the new program is. I want the launcher to maintain its theme.
I'm probably not using EventQueue right but I'm not sure which one to use.
package wind;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.text.ParseException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel;
public class Launcher {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton btn1, btn2, btn3;
private GridBagConstraints gbc = new GridBagConstraints();
private JMenuBar menuBar;
private JMenu menu, menu2, menu3, menu4;
private JMenuItem menuItem, menuItem2, menuItem3, menuItem4,
menuItem5, menuItem6, menuItem7, menuItem8, menuItem9, menuItem10,
menuItem11, menuItem12, menuItem13, submenu1, submenu2, submenu3, submenu4, submenu5,
submenu6, submenu7, submenu8, submenu9, submenu10;
JFrame frame;
public Launcher() {
JFrame.setDefaultLookAndFeelDecorated(true);
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
try {
UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
frame = new JFrame();
frame.setTitle("Project Wind Client Launcher");
Image icon = getImage("windicon.png");
if (icon != null)
frame.setIconImage(icon);
components();
frame.add(mainPanel());
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setResizable(false);
frame.setVisible(true);
}
});
}
private JPanel mainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
btn1 = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("/resources/gui.png"));
btn1.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}
btn1.setToolTipText("Click here to launch the client with a graphic user interface.");
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
runGUIMode(e);
}
});
//btn1.setBorder(null);
btn1.setOpaque(true);
btn1.setContentAreaFilled(false);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.25;
mainPanel.add(btn1, gbc);
btn2 = new JButton();
try {
Image img2 = ImageIO.read(getClass().getResource("/resources/nogui.png"));
btn2.setIcon(new ImageIcon(img2));
} catch (IOException ex) {
}
btn2.setToolTipText("This will launch the client without a graphic user interface.");
btn2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
runNoGUI(e);
}
});
btn2.setContentAreaFilled(false);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 0.25;
mainPanel.add(btn2, gbc);
btn3 = new JButton();
try {
Image img2 = ImageIO.read(getClass().getResource("/resources/app.png"));
btn3.setIcon(new ImageIcon(img2));
} catch (IOException ex) {
}
btn3.setToolTipText("This will launch the client in application mode.");
btn3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
runApplicationMode(e);
}
});
btn3.setContentAreaFilled(false);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.25;
mainPanel.add(btn3, gbc);
return mainPanel;
}
private void runApplicationMode(ActionEvent e) {
new FrameListener(FrameListener.LaunchMode.APPLICATION);
}
private void runNoGUI(ActionEvent e) {
new FrameListener(FrameListener.LaunchMode.NOGUI);
}
private void runGUIMode(ActionEvent e) {
new FrameListener(FrameListener.LaunchMode.GUI);
}
public void components() {
menuBar = new JMenuBar();
menu = new JMenu("File");
menu2 = new JMenu("Links");
menu3 = new JMenu("Guides");
menu4 = new JMenu("Help");
menuBar.add(menu);
menuBar.add(menu2);
menuBar.add(menu3);
menuBar.add(menu4);
menuItem = new JMenuItem("Exit");
menuItem.setToolTipText("Click here to exit the client launcher.");
menuItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
exitClient(e);
}
});
menuItem2 = new JMenuItem("Update");
menuItem2.setToolTipText("Click here to update your client.");
menuItem2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateClient(e);
}
});
menuItem3 = new JMenuItem("Check Version");
menuItem3.setToolTipText("Click here to check the version of your client.");
menuItem3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
checkVersion(e);
}
});
menuItem13 = new JMenuItem("Hide Launcher");
menuItem13.setToolTipText("Click here to hide the client launcher.");
menuItem13.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
hideLauncher(e);
}
});
menu.add(menuItem3);
menu.add(menuItem);
menu.add(menuItem13);
menu.add(menuItem2);
menuItem4 = new JMenuItem("Home");
menuItem4.setToolTipText("Click here to open up your homepage.");
menuItem4.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
home(e);
}
});
menuItem5 = new JMenuItem("YouTube");
menuItem5.setToolTipText("Click here to open up YouTube.");
menuItem5.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuItem5)
openURL("http://youtube.com");
}
});
menuItem6 = new JMenuItem("Twitter");
menuItem6.setToolTipText("Click here to open up Twitter.");
menuItem6.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuItem6)
openURL("http://twitter.com");
}
});
menuItem10 = new JMenuItem("Twitch");
menuItem10.setToolTipText("Click here to open up Twitch.");
menuItem10.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuItem10)
openURL("http://twitch.tv");
}
});
menu2.add(menuItem4);
menu2.add(menuItem10);
menu2.add(menuItem6);
menu2.add(menuItem5);
menuItem7 = new JMenu("Combat");
menuItem7.setToolTipText("Click here to view more options related to combat.");
submenu1 = new JMenu("Attack");
submenu2 = new JMenu("Strength");
submenu3 = new JMenu("Defence");
submenu4 = new JMenu("Hitpoints");
submenu5 = new JMenu("Prayer");
submenu6 = new JMenu("Ranged");
submenu7 = new JMenu("Magic");
menuItem7.add(submenu1);
menuItem7.add(submenu3);
menuItem7.add(submenu4);
menuItem7.add(submenu5);
menuItem7.add(submenu7);
menuItem7.add(submenu6);
menuItem7.add(submenu2);
menuItem8 = new JMenu("Skilling");
menuItem8.setToolTipText("Click here to view more options about skilling.");
submenu8 = new JMenu("Cooking");
submenu9 = new JMenu("Fishing");
submenu10 = new JMenu("Fletching");
menuItem8.add(submenu8);
menuItem8.add(submenu9);
menuItem8.add(submenu10);
menuItem9 = new JMenu("Money Making");
menuItem9.setToolTipText("Click here to view more options related to money making.");
menu3.add(menuItem7);
menu3.add(menuItem8);
menu3.add(menuItem9);
menuItem11 = new JMenu("Report a Bug");
menuItem11.setToolTipText("See any bugs? Click here and report them.");
menuItem12 = new JMenu("Commands");
menuItem12.setToolTipText("Click here to see which commands are available.");
menu4.add(menuItem11);
menu4.add(menuItem12);
}
private void exitClient(ActionEvent e) {
System.exit(1);
}
private void updateClient(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Client will now update.");
}
private void checkVersion(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Your files are fully updated.");
}
private void hideLauncher(ActionEvent e) {
frame.setState(Frame.ICONIFIED);
}
private void home(ActionEvent e) {
openURL("http://google.com");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Launcher();
}
});
}
public void openURL(String url) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI(url);
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
}
private Image getImage(String name) {
String url = "https://dl.dropboxusercontent.com/u/5173165/icons/" + name;
try {
File f = new File(name);
if (f.exists())
return ImageIO.read(f.toURI().toURL());
Image img = ImageIO.read(new URL(url));
if (img != null) {
ImageIO.write((RenderedImage) img, "PNG", f);
return img;
}
} catch (MalformedURLException e) {
System.out.println("Error connecting to image URL: " + url);
} catch (IOException e) {
System.out.println("Error reading file: " + name);
}
return null;
}
}
This is a class that creates the new application the user selects
package wind;
import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import wind.gui.Application;
import wind.gui.Gui;
import wind.web.WebClient;
public class FrameListener {
static JLabel image;
LaunchMode mode;
public FrameListener(LaunchMode mode) {
this.mode = mode;
initClient();
}
public enum LaunchMode {
APPLICATION,
GUI,
NOGUI;
}
public void initClient() {
switch(mode) {
case APPLICATION:
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Application();
}
});
break;
case GUI:
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
break;
case NOGUI:
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new WebClient(null);
}
});
break;
default:
mode = LaunchMode.GUI;
break;
}
}
}
This Swing based Launcher uses ProcessBuilder to run programs in a separate JVM. You can assess its suitability for your application by running it with a non-default Look & Feel. Note MetalLookAndFeel on the left and AquaLookAndFeel on the right in the illustration below.
$ java -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel \
-cp build/classes gui.Launcher
The following code opens a JInternalFrame when a button is clicked. But I want this window to be opened once, so if the user clicks that button again it will not open another frame instead it would bring to the front the window whether it is iconified, behind another window, etc. I have tried a couple of ways mainly using a counter, but the problems is once the frame is closed it doesn't open it again either. Is there another easy way to do this, cause I am not able to make it work properly. Thanks in advance.
Below is the code I am working on:
public class About implements ActionListener{
private int openFrameCount;
private JDesktopPane desk;
private JTextArea Tarea;
private JScrollPane scroll;
private BufferedReader in ;
int count =0;
MyInternalFrame frame;
public About(JDesktopPane desktop) {
// TODO Auto-generated constructor stub
desk = desktop;
System.out.println(count);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
count += 1;
if(count == 1){
frame = new MyInternalFrame("SAD Imaging");
count +=1;
try {
in = new BufferedReader(new FileReader("SADInfo.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line;
String file = "";
try {
while((line = in.readLine()) != null)
{
System.out.println(line);
file += line;
file +="\n";
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Tarea = new JTextArea();
//System.out.println(file);
Tarea.setText(file);
Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 16);
Tarea.setFont(f);
Tarea.setBackground(Color.white);
Tarea.setAlignmentX(SwingConstants.CENTER);
Tarea.setEditable(false);
JPanel panel = new JPanel();
panel.add(Tarea);
panel.setBackground(Color.white);
//scroll = new JScrollPane(Tarea);
scroll = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(scroll);
frame.setVisible(true);
desk.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
else if(count > 1){
try {
//frame.setIcon(true);
frame.setMaximum(true);
frame.toFront();
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Basically, you just need to check to see if the frame is null or not. If it is, you create an instance, if it's not, you bring it to the front, for example
#Override
public void actionPerformed(ActionEvent arg0) {
if (frame == null || (frame.getParent() == null && !frame.isIconifiable())) {
// Your exitsing code
} else {
frame.setIcon(false);
frame.setSelected(true);
frame.moveToFront();
}
You can also use an InteralFrameListener to the frame so you can detect when the frame is closed, so you null the internal reference, for example...
frame.addInternalFrameListener(new InternalFrameAdapter() {
#Override
public void internalFrameClosing(InternalFrameEvent e) {
frame = null;
}
});
Updated with runnable example
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestInternalFrame {
public static void main(String[] args) {
new TestInternalFrame();
}
private JInternalFrame imageFrame;
private JDesktopPane desktop;
public TestInternalFrame() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JMenu fileMenu = new JMenu("File");
JMenuItem newMenu = fileMenu.add("Show...");
newMenu.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (imageFrame == null || imageFrame.isClosed()) {
imageFrame = new JInternalFrame("Image");
imageFrame.setIconifiable(true);
imageFrame.setMaximizable(true);
imageFrame.setClosable(true);
imageFrame.setResizable(true);
JLabel label = new JLabel(new ImageIcon("..."));
imageFrame.add(label);
imageFrame.pack();
desktop.add(imageFrame);
imageFrame.setLocation(0, 0);
imageFrame.setVisible(true);
}
try {
imageFrame.setIcon(false);
imageFrame.setSelected(true);
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
imageFrame.moveToFront();
}
});
desktop = new JDesktopPane();
JMenuBar mb = new JMenuBar();
mb.add(fileMenu);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(mb);
frame.add(desktop);
frame.setSize(1200, 900);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
I'm making a game which is using sound and images. The weirdest thing is happening. When I load sound, my image won't appear. However, when I don't load my sound, my image does appear. Here is my code:
package com.gbp.chucknorris;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Title extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
public Thread thread;
private BufferedImage logo;
private Clip clip, titleClip;
public Title() {
super();
loadSound();
loadImages();
bind();
setBackground(Color.WHITE);
}
private void loadSound() {
File f = new File("res/sounds/title.wav");
AudioInputStream stream = null;
try {
stream = AudioSystem.getAudioInputStream(f);
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
try {
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException e) {
e.printStackTrace();
}
try {
clip.open(stream);
} catch (LineUnavailableException | IOException e) {
e.printStackTrace();
}
File title = new File("res/sounds/theme.wav");
AudioInputStream titleStream = null;
try {
titleStream = AudioSystem.getAudioInputStream(title);
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
DataLine.Info titleInfo = new DataLine.Info(Clip.class, titleStream.getFormat());
try {
titleClip = (Clip) AudioSystem.getLine(titleInfo);
titleClip.open(titleStream);
} catch (LineUnavailableException | IOException e) {
e.printStackTrace();
}
titleClip.loop(Clip.LOOP_CONTINUOUSLY);
}
private void bind() {
InputMap im = getInputMap();
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke("DOWN"), "down");
am.put("down", new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
}
});
}
private void loadImages() {
try {
logo = ImageIO.read(new File("res/pics/MenuPanel.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (true) {
repaint();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(logo, 0, 0, null);
}
}
Thanks in advance!
You need to learn to use background threading such as a SwingWorker so as not to tie up the event thread, the EDT, when loading and playing sounds. You can read up on how to use this here: Lesson: Concurrency in Swing.
Edit: I wouldn't recommend doing this:
#Override
public void run() {
while (true) {
repaint();
}
}
I am trying to learn more about java. This program is an attempt to understand events as well as serialization. What i am attempting to do is flatten an object when the user closes the JFrame and re-inflate it when the program is started. I know i can create the serialized file but having it take effect again isn't working. Any help in the right direction would be wonderful. Thank you in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt";
public int ToL = 0;
String outT = Integer.toString(ToL);
JLabel jl = new JLabel(outT);
FileOutputStream fos = null;
ObjectOutputStream out = null;
public tempusFugit()
{
Timer timer = new Timer(1000, this);
setBounds(250, 250, 250, 190);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT) );
setVisible(true);
add(jl);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
++ToL;
outT = Integer.toString(ToL);
jl.setText(outT);
validate();
repaint();
}
public static void main(String[] args)
{
tempusFugit tf = new tempusFugit();
tf.addWindowListener( tf );
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
try
{
fos = new FileOutputStream(fileN);
out = new ObjectOutputStream(fos);
out.writeObject(this);
out.close();
}
catch(IOException ex)
{
}
}
public void windowOpened(WindowEvent e)
{
try
{
tempusFugit tf = new tempusFugit();
FileInputStream fis = new FileInputStream(fileN);
ObjectInputStream in = new ObjectInputStream(fis);
tf = (tempusFugit)in.readObject();
this.ToL = tf.ToL;
}
catch(IOException ex)
{
}
catch(ClassNotFoundException ce)
{
}
}
}
I assume i'm trying to recreate the object at the wrong time. Even though the object is serialized correctly i can not access it again with the windowOpened function. Do i need to try to use
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
somehow?
What i end up with is an error saying i am trying to access a Final object (i assume my this). I find that very odd that i cant repopulate my current 'this' with another similar object.
Am i way off base?
Again thank you for your time.
Note that there are much simpler ways to do this than serializing the frame, and much better things to serialize than the frame itself.
See What is the best practice for setting JFrame locations in Java? for an example of storing the location and size of a frame. It would be trivial to adapt that to store the count.
But here is an attempt based on your code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt";
public int ToL = 0;
JLabel jl = new JLabel("" + ToL);
public tempusFugit()
{
Timer timer = new Timer(1000, this);
setBounds(250, 250, 250, 190);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT) );
setVisible(true);
add(jl);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
++ToL;
jl.setText("" + ToL);
validate();
repaint();
}
public static void main(String[] args)
{
tempusFugit tf = new tempusFugit();
tf.addWindowListener( tf );
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e)
{
try
{
FileOutputStream fos = new FileOutputStream(fileN);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(this);
out.flush();
out.close();
setVisible(false);
System.exit(0);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, ex);
System.exit(1);
}
}
public void windowOpened(WindowEvent e)
{
try
{
tempusFugit tf;// = new tempusFugit();
FileInputStream fis = new FileInputStream(fileN);
ObjectInputStream in = new ObjectInputStream(fis);
tf = (tempusFugit)in.readObject();
this.ToL = tf.ToL;
//tf.setVisible(false);
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ce)
{
ce.printStackTrace();
}
}
}