awt

assign keys for combo box in java

穿精又带淫゛_ 提交于 2019-12-20 06:41:55
问题 I want to add a JComboBox in Swing that is simple but I want to assign the values for each items in combo. I have the following code JComboBox jc1= new JComboBox(); jc1.addItem("a"); jc1.addItem("b"); jc1.addItem("c"); Now what I want is that when click on combo box it should return 1, 2 and 3 correspondingly instead of a ,b, c. Is there any way to assign the key values for each items in combo box? 回答1: You can add an item as an object instead of adding String like this: JComboBox<ItemClass>

Coloring an area of BufferedImage

六眼飞鱼酱① 提交于 2019-12-20 06:28:46
问题 I want to color a sub area of a BufferedImage I have. I am presently doing: public BufferedImage paintSubImage(BufferedImage img, int x, int y, int w, int h) { Graphics g = img.getGraphics(); g.setColor(Color.BLACK); g.fillRect(x,y,w,h); return img; } But I am not able to color it. Am I doing it wrong? 回答1: Given the fact that there is little or no context to go on, it's difficult to know exactly what's wrong. General rule of thumb though, if you create/open it, you should dispose/close it...

Java AWT/Swing: Get notified on change of position/size of my window

时间秒杀一切 提交于 2019-12-20 06:10:05
问题 I've got a window and I would like to get notified when the position or the size has changed. Which type of listener is the best to take? WindowListener didn't work for me. 回答1: You can use a ComponentListener which has the componentResized and componentMoved methods. 回答2: You might want to try the HierarchyBoundsListener myJFrame.getContentPane().addHierarchyBoundsListener(new HierarchyBoundsListener(){ public void ancestorMoved(HierarchyEvent e) { doSomething(); } public void

paintComponent not painting onto JPanel

﹥>﹥吖頭↗ 提交于 2019-12-20 06:07:02
问题 I am working on a homework assignment where I am supposed to make a program that allows you to paint custom shapes and lines, and move them around the screen. Originally I was using public void paint(g) to paint, but the shapes were flickering when I called repaint. Because of that I switched over to paintComponent(g) . However When I try to paint a shape nothing shows up. I believe this is because it isn't painting on top of the JPanel . The frame has 3 panels in a 3 row BorderLayout .

Post overriding the paint method of the components in java

时光毁灭记忆、已成空白 提交于 2019-12-20 05:49:23
问题 In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing). This is usually (maybe allways - I'm not sure) done when you are creating the component for example: JPanel jPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //... my implementation of paint, some transfromations, rotation, etc }

How to increase drawing speed of JPictureBox for large image?

五迷三道 提交于 2019-12-20 05:38:21
问题 I have a JPictureBox extends from a java.awt.Component see code here http://pastebin.com/SAJc6Sht. But it only works well when there is no image stretching. Especially for the big picture, it slows program dramatically. How to increase drawing speed of JPictureBox for large image? @Override public void paint(Graphics g) { super.paint(g); int x = 0; int y = 0; int w = 0; int h = 0; if (image != null) { switch (sizeMode) { case AUTO_SIZE: case NORMAL: w = image.getWidth(); h = image.getHeight()

Importing Font to GUI

耗尽温柔 提交于 2019-12-20 05:24:26
问题 I am trying to change the font for my GUI besides the basic 5 that swing seems to come with. How to import fonts and actually use them in my code? 回答1: There are usually more than 5 available by default, but they change from system to system. This answer examines both the existing fonts, as well as how to load & register new fonts. It uses the 'Airacobra Condensed' font available from Download Free Fonts (obtained by hot-link URL). A font that is in the Jar of your app. is also accessible by

multiple key detection for KeyListener (java)

╄→尐↘猪︶ㄣ 提交于 2019-12-20 04:58:10
问题 How would one implement KeyListener so that I can create a two-player system where one person uses '.' and '/' to control a character, and the other person can use the arrow keys without them interrupting each other? The way I have it now is that when one person holds down the arrow key, their character moves, but the instant you use the other player's controls, the first person's character stops. 回答1: Create a HashMap<Int,Boolean> that marks which keys are currently pressed/depressed. Then

Overlapping AWT lines and Swing JLabels

假装没事ソ 提交于 2019-12-20 04:53:51
问题 I have a problem in my application using line primitives and JLables . I try to explain it: I have to draw a vehicle route using lines to represent roads and JLabels to represent cities. I need the use of JLabels because each JLabel has a Listener that shows a dialog with information about the city. I redefine paint() method of my main JPanel . In that method I first in invoke the super.paint() , then I draw the lines and finally I add the Labels to the JPanel . The problem is that the lines

How to make the main GUI thread 'wait' until a separate pop-up window is disappeared

孤街浪徒 提交于 2019-12-20 04:24:43
问题 I have an object which is invoked from the main GUI thread and shows a separate JFrame for a number of seconds and then disappear (with the use of a timer). Now I want to make the main GUI thread to wait until the pop-up JFrame window disappears. For example, on the main GUI code: // initiate the object and show the pop-up JFrame DisappearingJFrame djf = new DisappearingJFrame (); djf.show (); // now the main GUI thread should wait // and after the pop-up JFrame disappears, the rest of the