awt

How to use KeyListener

我的梦境 提交于 2019-11-26 17:47:39
问题 I am currently trying to implement a keylistener in my program so that it does an action when I pressed an arrow key, the object in my program either moves left or right. Here is the moving method in my program public void moveDirection(KeyEvent e) { int move = 0; int r = K.getRow(); int c = K.getCol(); if (e.getKeyCode() == 39) move = 1; //KeyEvent.VK_RIGHT if (e.getKeyCode() == 37) move = 2; //KeyEvent.VK_LEFT //if (e.getKeyCode() == KeyEvent.VK_DOWN) move = 3; switch (move) { case 1: if

JComponents disappearing after calling mouseClicked()

喜夏-厌秋 提交于 2019-11-26 17:29:17
问题 I am writing a Java GUI program with Swing. The interface looks like this: When the user clicks on one of the pictures on the right hand side, I want a small preview of it to show up in the orange area in the upper left corner. I pull all of the image files from a directory on my computer via a SwingWorker thread. In the SwingWorker's done() method, I add a mouseClicked listener to each object (a TaggableImage object from a class I created. It has a BufferedImage and extends JComponent,

How to do 2D shadow casting in Java?

大憨熊 提交于 2019-11-26 17:20:45
问题 I'm currently trying to implement a 2D shadow casting method in Java by following this tutorial: http://ncase.me/sight-and-light/ I want to stick to Line2D and Polygon objects. Here is the main part of my code so far: for (Polygon p : Quads.polygons) { for (int i = 0; i < p.npoints; i++) { osgCtx.setStroke(new BasicStroke(0.1f)); Line2D line = new Line2D.Double(mousePos.getX(), mousePos.getY(), p.xpoints[i], p.ypoints[i]); osgCtx.draw(line); } osgCtx.setStroke(new BasicStroke(1.0f)); osgCtx

How to count the number of lines in a JTextArea, including those caused by wrapping?

时间秒杀一切 提交于 2019-11-26 17:12:41
问题 I have a JTextArea for which I have set word-wrap and wrap-style-word to true. I want to "pack" the JTextArea to the minimum possible height given a specified width. To do this, I'm planning calculating the height of the font using... Font font = jTextArea.getFont(); FontMetrics fontMetrics = jTextArea.getFontMetrics(font); int lineHeight = fontMetrics.getAscent() + fontMetrics.getDescent(); ...and then multiply this by the number of lines used in the JTextArea. The problem is that JTextArea

Simulate a key held down in Java

寵の児 提交于 2019-11-26 17:09:49
问题 I'm looking to simulate the action of holding a keyboard key down for a short period of time in Java. I would expect the following code to hold down the A key for 5 seconds, but it only presses it once (produces a single 'a', when testing in Notepad). Any idea if I need to use something else, or if I'm just using the awt.Robot class wrong here? Robot robot = null; robot = new Robot(); robot.keyPress(KeyEvent.VK_A); Thread.sleep(5000); robot.keyRelease(KeyEvent.VK_A); 回答1: Thread.sleep() stops

AWT custom rendering - capture smooth resizes and eliminate resize flicker

不想你离开。 提交于 2019-11-26 17:07:36
问题 I've been looking at this for several months and so far this is the best I have come up with. The structure (render outside of EDT) is not up for debate, as our application operates this way and will not be rewritten. The application has a layout model and a scripting model which are integrated and drive rendering, so the render must be performed outside of the AWT paint model. What I am trying to arrive at is the optimal and reliable way to perform custom rendering. The following SSCCE works

Getting a HeadlessException: No X11 DISPLAY variable was set

早过忘川 提交于 2019-11-26 16:52:42
问题 Exception in thread "main" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it. at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159) at java.awt.Window.(Window.java:432) at java.awt.Frame.(Frame.java:403) at javax.swing.JFrame.(JFrame.java:202) at DrawGUI.(DrawGUI.java:15) at ShapeCreator.(ShapeCreator.java:31) at ShapeCreator.main(ShapeCreator.java:138) What does this error message mean? And how can I

JFrame: get size without borders?

谁说胖子不能爱 提交于 2019-11-26 16:49:15
问题 In Java, is it possible to get the Width and Height of the JFrame without the title and other borders? frame.getWidth() and frame.getHeight()1 seems to return the width including the border. Thanks. 回答1: frame.getContentPane().getSize(); 回答2: frame.pack(); System.out.println("frame width : "+getWidth()); System.out.println("frame height: "+getHeight()); System.out.println("content pane width : "+getContentPane().getWidth()); System.out.println("content pane height: "+getContentPane()

java multiple graphics [closed]

别说谁变了你拦得住时间么 提交于 2019-11-26 16:48:48
Okay so I have been working on this code for awhile that shows how the sorting algorithms work. Right now I have it working where it sorts multiple graphs with the same sort but I need each graph to do a different sort at the same time. I have been researching and trying to solve this for days and now I just have tunnel vision. I'll post my code in case my explanation was confusing. I feel like this could benefit a lot of people working with java graphics and any help would be appreciated. import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.Random; import

Character display/search for Unicode characters

て烟熏妆下的殇ゞ 提交于 2019-11-26 16:46:36
问题 While answering another question about these parts, I became curious about the glyphs that might be obtained from the Unicode characters as displayed by the available fonts on the system. Of course there are 65,536 Unicode chars, and over 250 fonts on a typical machine! Leaves one wondering where to start. What would be wonderful is an app. that allows us to browse tables of the code points of Unicode as displayed in ..any font selected in a list. It would be nice to also have: A spinner that