awt

String length in pixels in Java

巧了我就是萌 提交于 2019-12-19 05:17:06
问题 Is there a way to calculate the length of a string in pixels, given a certain java.awt.Font object, that does not use any GUI components? 回答1: that does not use any GUI components? It depends on what you mean here. I'm assuming you mean you want to do it without receiving a HeadlessException . The best way is with a BufferedImage . AFAIK, this won't throw a HeadlessException : Font font = ... ; BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); FontMetrics fm = img

Adding actionListener to jCalendar

别来无恙 提交于 2019-12-19 04:10:31
问题 How would I add an actionListener to the jDayChooser component of an existing jCalendar placed using netbeans? I would like to only trigger an event only when the day buttons are clicked. as the propertyChange in jCalendar listens to even the jMonthChooser and jYearChooser P.S. using toedter's jCalendar 回答1: Alternatively, you can listen for the specific propertyName , "day" . JDayChooser jdc = new JDayChooser(); jdc.addPropertyChangeListener("day", new PropertyChangeListener() { @Override

Why do I have to call GraphicsEnvorinment.registerFont() even if I my Font were created from file?

三世轮回 提交于 2019-12-19 04:05:30
问题 I'm developing a web application which use JFreeChart to render chart. However, when server dose not have any Chinese font installed, JFreeChart dose not display Chinese character even if I have set the font. Then I write a small testing code and find out that add this line of code before drawing the chart can solve the problem. GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font); So my questions are - Why do I have to register font into JVM even if I create my font from File

KeyPressed event in java

落花浮王杯 提交于 2019-12-19 03:24:34
问题 I have just created a java tic-tac-toe game i would like to figure out how to run a method once the enter key is pressed during a certain condition an example is below... if(/*condition is met*/){ //keyListener } 回答1: Depending on where you want to trap the "enter" key, you could use an ActionListener (on such components such as text components or buttons) or attach a key binding to you component public class MyPanel extends JPanel { public MyPanel() { InputMap im = getInputMap(WHEN_FOCUSED);

Unregister font with GraphicsEnvironment?

房东的猫 提交于 2019-12-19 02:52:11
问题 I recently found out how to register a TTF font with the local GraphicsEnvironment, s.t., for my use case (SVG-to-PNG transcoding), Apache Batik may recognize the font: import java.awt.Font; import java.awt.FontFormatException; import java.awt.GraphicsEnvironment; // [...] GraphicsEnvironment lge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile); lge.registerFont(font); } catch (FontFormatException e) { logger.warn(e

How to hide the arrow buttons in a JScrollBar

十年热恋 提交于 2019-12-18 15:49:47
问题 I need to hide the arrow buttons of java.awt.Scrollbar(VERTICAL) in an AWT application. Does anyone know how this can be achieved? I saw an example here, but the code just hides the buttons. The vacant space for the buttons still remains; it is not occupied by the scroll bar. To be more exact, here is the screenshot of what I should achieve. I am not sure which direction to go about it. Update : I was looking for a solution in AWT. But now I am open to suggestions in Swing as well. 回答1: Try

Handling the Event Dispatch Thread

…衆ロ難τιáo~ 提交于 2019-12-18 13:39:22
问题 I have a question about the 'Event Dispatch Thread'. I have a Main class that is also a JFrame. It initialises the rest of the components in the code, some of them do not involve Swing and some of them do. Is it enough to simply initialise the Main class using the EDT like this?... public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new Main(); } }); } This way everything would run on the Event Dispatcher thread. 回答1: That is

Implementing Polygon2D in Java 2D

大兔子大兔子 提交于 2019-12-18 11:56:03
问题 I'm creating a 2D game in Java using the Java2D library for drawing, and I really need a float-precision Polygon object that I can use both to draw game objects and to do collision detection on them. Unfortunately, Java's Polygon object comes in int precision only, and there is no equivalent Polygon2D like there is with Rectangle and Rectangle2D. I've already done enough research to see that I have a few options, but none of them seem very good. Use Path2D . According to a Java developer

Call a method when application closes

流过昼夜 提交于 2019-12-18 09:34:16
问题 In my Swing chat application I have a logout button which is used to logout the user and it works fine. Now I need to logout the user when I close the Swing application window. I did this in web application when closing browser using JavaScript, but now I need to do this in Swing application. How can I achieve this? 回答1: Call JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) Add a WindowListener to the frame. Override the appropriate method of the listener to call your closing

Make a KeyEvent in Java only happen once even when key is held

有些话、适合烂在心里 提交于 2019-12-18 09:23:44
问题 I need to know if it is possible to have an action only happen once when a key is pressed, even if that key is held down for some time, and if it is possible, how? This is the code I have for that now: if(e.getKeyCode() == KeyEvent.VK_A) { attack = true; moveX = -5; draw(moveX, moveY); players.get(username).setImageIcon("attack-left"); } This is within the keyPressed method, and in the keyReleased I set moveX to 0. So if A is pressed the image is supposed to 5 units to the left and stop,