awt

Is calling repaint from paintComponent a good practice

主宰稳场 提交于 2019-12-18 09:09:04
问题 For some UI components in our application, we override paintComponent , which under some conditions "recursively" calls itself by invoking repaint . We use this approach to achieve high refresh rate of animations in the component. For instance, a progress bar we use looks something like: public class SimpleProgressBar extends JPanel { private boolean inProgress; .... @Override protected void paintComponent(Graphics g) { if (inProgress) { paintBar(g); repaint(); } else { doSomeOtherThings(); }

Clearing contents of TextField using setText does not work in AWT

不问归期 提交于 2019-12-18 08:57:15
问题 I am having problems clearing contents of TextField in AWT using setText() method. Apparently, setText("") does not clear the contents of the TextField on pressing the 'Reset' button. Here's my program: import java.awt.*; import java.awt.event.*; public class form extends Frame { Label lbl = new Label("Name:"); TextField tf = new TextField(); Button btn = new Button("Reset"); public form() { tf.setColumns(20); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {

How to draw custom rounded rectangles in java?

北城以北 提交于 2019-12-18 07:17:15
问题 I know how to draw a rounded rectangle but I want to define roundness for each corner separately and draw something like the image below : 回答1: There's probably a few ways to achieve this, but the easiest I can think of would be to, as Andrew has already hinted, would be to define your own Shape import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Path2D; import javax.swing.JFrame;

Using Java's Robot to hold a key down

别来无恙 提交于 2019-12-18 07:15:18
问题 Currently i'm trying to have java hold down a key like follows: Robot rob; rob.keyPress(KeyEvent.VK_ENTER); Thread.sleep(3000); rob.keyRelease(KeyEvent.VK_ENTER); This should hold enter down for 3 seconds, causing the repeating effect after a second or so. In other words, if you were to manually hold the "r" key, it would first type r, and then after about a second it would go like rrrrrrrr. I want this effect from the robot. I also tried: curTime = System.currentTimeMillis(); while(System

Using Java's Robot to hold a key down

天涯浪子 提交于 2019-12-18 07:15:04
问题 Currently i'm trying to have java hold down a key like follows: Robot rob; rob.keyPress(KeyEvent.VK_ENTER); Thread.sleep(3000); rob.keyRelease(KeyEvent.VK_ENTER); This should hold enter down for 3 seconds, causing the repeating effect after a second or so. In other words, if you were to manually hold the "r" key, it would first type r, and then after about a second it would go like rrrrrrrr. I want this effect from the robot. I also tried: curTime = System.currentTimeMillis(); while(System

ImageIcons on JButton are not showing up in Runnable JAR file

試著忘記壹切 提交于 2019-12-18 06:48:24
问题 I have 6 JButtons on my GUI all have images on it, when I compile and run the code, all images on JButtons show up perfectly but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem? I used this method in my code to show icons on JButtons ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png"); jb1 = new JButton(SettingsIc); jb1.setFocusPainted( false ); //jb1.setBorderPainted(false); jb1.setContentAreaFilled(false); This is how my GUI looks when I

Java search for on-screen text field

为君一笑 提交于 2019-12-18 05:25:08
问题 I am trying to create a program that automatically searches for a text field on the screen and types a word repetitively into that text field. Is there any class that can find a text field? Or is there any way in which a text field can be found? Because I know that the Robot class can type text, I just need to either get the cursor onto the text field and use the mousePress() and mouseRelease() methods. Thanks 回答1: I can't directly give you a solution, but I messed around with some code and

java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt

谁都会走 提交于 2019-12-18 05:17:04
问题 I have some errors atm while im coding with JAVA, I have been trying to fix this for along time, also trying to find oterh ppl who have same problem and fixed it but nothing work... Well.. here is the code package ca.vanzeben.game; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import javax.swing

Why does my icon handling code throw a NullPointerException?

≡放荡痞女 提交于 2019-12-18 04:23:13
问题 I have added an image for my button,but when I run that frame this exception will be thrown .why?please help me. init: deps-jar: compile-single: run-single: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:138) at ClientGUI.IdAndPasswordFrame.initComponents(IdAndPasswordFrame.java:91) at ClientGUI.IdAndPasswordFrame.<init>(IdAndPasswordFrame.java:22) at ClientGUI.IdAndPasswordFrame$4.run(IdAndPasswordFrame.java:200) at java

Create an image from a non-visible AWT Component?

纵饮孤独 提交于 2019-12-18 03:55:14
问题 I'm trying to create an image (screen-shot) of a non-visible AWT component. I can't use the Robot classes' screen capture functionality because the component is not visible on the screen. Trying to use the following code: BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); component.paintAll(g); Works sometimes, but does not work if the component contains things such as a text box or button, or some sort of OpenGL / 3D