awt

How can I convert an Icon to an Image

為{幸葍}努か 提交于 2019-11-27 23:29:21
I'm trying to convert an Icon ( javax.swing.Icon ) to an Image ( java.awt.Image ) using this code: private Image iconToImage(Icon icon) { if(icon instanceof ImageIcon) { return ((ImageIcon) icon).getImage(); } else { BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); icon.paintIcon(null, image.getGraphics(), 0, 0); return image; } } The thing is, the paintIcon function throws a NullPointerException on the image.getGraphics() . For the record, the icon value is the default CheckBox icon (obtained via UIManager.getIcon("CheckBox.icon")

How can I programmatically generate keypress events? [duplicate]

霸气de小男生 提交于 2019-11-27 22:59:08
This question already has an answer here: How to simulate keyboard presses in java? 1 answer What the java program should do is it should trigger keyboard press on some condition without a person pressing a keyboard key. So any program running in windows and in focus which requires keyboard input will get the input without a person actually pressing the keyboard. I found these related questions here : question 1 , question 2 I was wondering if there is any method to do this in Java. Use the Robot class. Code snippet: import java.awt.Robot; import java.awt.KeyEvent; Robot r = new Robot(); int

What is the best way to cut, copy, and paste in Java?

蓝咒 提交于 2019-11-27 22:51:35
问题 I've created an application using Swing with a text area (JTextArea). I want to create an "edit" menu, with options to cut and copy text from the text area, and paste text from the clipboard into the text area. I've seen a couple of ways to do this, but I wanted to know what the best way is. How should I implement the cut/copy/paste? 回答1: I would personally opt for re-using the standard cut, copy and paste actions. This is all explained in the Swing drag-and-drop tutorial: adding cut, copy

How can I catch AWT thread exceptions in Java?

浪尽此生 提交于 2019-11-27 22:49:20
We'd like a trace in our application logs of these exceptions - by default Java just outputs them to the console. shemnon There is a distinction between uncaught exceptions in the EDT and outside the EDT. Another question has a solution for both but if you want just the EDT portion chewed up... class AWTExceptionHandler { public void handle(Throwable t) { try { // insert your exception handling code here // or do nothing to make it go away } catch (Throwable t) { // don't let the exception get thrown out, will cause infinite looping! } } public static void registerExceptionHandler() { System

Is there a simple way to compare BufferedImage instances?

北慕城南 提交于 2019-11-27 22:09:41
I am working on part of a Java application that takes an image as a byte array, reads it into a java.awt.image.BufferedImage instance and passes it to a third-party library for processing. For a unit test, I want to take an image (from a file on disk) and assert that it is equal to the same image that has been processed by the code. My expected BufferedImage is read from a PNG file on disk using ImageIO.read(URL) . My test code reads the same file into a BufferedImage and writes that to a byte array as PNG to provide to the system under test. When the system under test writes the byte array to

Is Java Swing still in use?

半腔热情 提交于 2019-11-27 21:52:46
I am planning on making a Java Swing application and was wondering if Swing is still used or if it has been replaced with something else. Thanks in advance! We still use it. Not everything is a web app, so far there have been some tentative replacements (such as SWT, which eclipse is written in) SWT has a native layer that wraps the underlying calls to the native windowing layer. It only works for a limited set of platforms and of course requires some third party shared libraries. I would venture to say that there are far fewer SWT apps than Swing apps. Swing is still in use.... but there is

Cannot load font in JRE 8

£可爱£侵袭症+ 提交于 2019-11-27 21:47:24
问题 I cannot load a font from an S3 Inputstream in JRE 8. I do not have issue if a system is installed with JRE 7, JDK 7, or even JDK 8. val fontInputStream = s3Client.getObject(bucketName, objectKey).getObjectContent val customFont = Font.createFont(Font.TRUETYPE_FONT, fontInputStream).deriveFont(Font.TRUETYPE_FONT, 20F) The error that I got is Exception in thread "main" java.io.IOException: Problem reading font data. at java.awt.Font.createFont0(Font.java:1000) at java.awt.Font.createFont(Font

How to determine if GraphicsEnvironment exists

徘徊边缘 提交于 2019-11-27 21:30:28
I have an application that needs user input for password. What i want to do is to either read the password from console (if the OS supports one e.g unix) or display a JOptionPane and ask the user to enter his password (if the OS supports graphical interface e.g windows). Some people may argue that a console will always be available in both the above cases so a console input will be sufficient. But problem is if the Java app starts using javaw.exe then a console is not available. Thus, i need a way to determine if i can do either case. My problem is how to determine if the environment that the

Drawing in Java using Canvas

孤者浪人 提交于 2019-11-27 20:14:38
I want to draw in Java's Canvas but can't get it work because I don't know what I'm doing. Here's my simple code: import javax.swing.JFrame; import java.awt.Canvas; import java.awt.Graphics; import java.awt.Color; public class Program { public static void main(String[] args) { JFrame frmMain = new JFrame(); frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmMain.setSize(400, 400); Canvas cnvs = new Canvas(); cnvs.setSize(400, 400); frmMain.add(cnvs); frmMain.setVisible(true); Graphics g = cnvs.getGraphics(); g.setColor(new Color(255, 0, 0)); g.drawString("Hello", 200, 200); } } Nothing

How to make a splash screen for GUI?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 19:11:50
问题 Hi there I'm new to GUIs in Java and was trying to make a splash screen or an image appear for 3 seconds. Then after that it it will go onto my main program. Does anyone have an ideas how to do this or can link me to any tutorials? So far I have done this but not sure where to go from here. public static void main(String[] args) { splashInit(); // initialize splash overlay drawing parameters appInit(); // simulate what an application would do } 回答1: Simplest one , is to create JFrame and add