awt

Call a method when application closes

别等时光非礼了梦想. 提交于 2019-11-29 17:11:59
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? 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 method, then set the frame invisible and dispose of it. E.G. import java.awt.*; import java.awt.event.*; import

How to draw custom rounded rectangles in java?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 16:49:20
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 : 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; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;

How to disable java.awt.Graphics.fillRect(int x, int y, int width, int height)'s effect?

醉酒当歌 提交于 2019-11-29 16:46:39
It's the original image: I use java.awt.Graphics.fillRect(int x, int y, int width, int height) to add a coloured rectangle on the image. Graphics imageGraphics = image.createGraphics(); Color color = new Color(0,0,0,100); imageGraphics.setColor(color); imageGraphics.fillRect(0, 0, 800, 600); So the image has been inverted and looks like this: After that,I want to clear the black transparent rectangle partly and show the original image. imageGraphics.clearRect(100,100,100,100); But the effect is like this: What my requirement is: I want to know why it doesn't work and is there any other way to

How java swing draws to screen from different operating systems?

孤人 提交于 2019-11-29 16:46:08
I know Swing use Java2d which extends AWT to draw on screen but, what low level toolkit is used by swing to render? GTK or QT for example? Concrete implementations of Graphics2D on each supported platform direct painting to a heavyweight peer component provided by the host, e.g. Quartz on Mac OS X, Graphics Device Interface on Windows, and Xlib/XCB, part of the X Window System often used on Linux. 来源: https://stackoverflow.com/questions/29440313/how-java-swing-draws-to-screen-from-different-operating-systems

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

人走茶凉 提交于 2019-11-29 16:25:21
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, regardless of whether or not A is still being held down or it has been released. But this is not working,

Flipping shape (not image)

℡╲_俬逩灬. 提交于 2019-11-29 16:09:51
Solved: Thanks @MadProgrammer I replaced g2.rotate(Math.toRadians(180.0)); by g2.scale(1, -1); thanks^^ I wrote program to show Digital Clock with mirror (Vertical Flip) This is my code import java.awt.*; import java.awt.font.GlyphVector; import javax.swing.*; import java.util.*; public class DigitalClock extends JFrame implements Runnable { /** * @author HASSAN */ Thread runner; // declare global objects Font clockFont; Shape mirror; public DigitalClock() { super("Digital Clock - Hassan Sharaf 12MCMB33"); setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);

getting Exception : java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

那年仲夏 提交于 2019-11-29 16:06:25
I am implementing browser kind of project and I am getting an exception. import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.Insets; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import chrriis.dj.nativeswing.swtimpl.NativeInterface; import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser; public class WebPageDisplay extends JPanel{ public WebPageDisplay() { super(new BorderLayout()); try{ JPanel webBrowserPanel = new JPanel(new BorderLayout()); // webBrowserPanel.setBorder

About the EDT (Java)

萝らか妹 提交于 2019-11-29 15:38:29
I have read a number of articles on the internet about when something should run in the EDT, and when it shouldn't. But I'm still not sure I understand, so I'd like to ask a few question about this: What pieces of code are going to run by default inside the EDT? What pieces of code are going to be run be default outside the EDT? When should I use InvokeLater() so something that by default would run outside the EDT, will run inside it? When should I prevent a piece of code from running (by default) inside the EDT, by creating a new thread and putting that code inside it? Thanks All the code

Is calling repaint from paintComponent a good practice

跟風遠走 提交于 2019-11-29 15:33:55
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(); } } } Is this a good practice (especially in terms of performance / efficiency / CPU usage)? Is it

Centering the entire window Java

丶灬走出姿态 提交于 2019-11-29 15:23:25
My question is quite simple I guess ... I would like to have my java Frame centered when I run my program. I used the following code : setLocationRelativeTo(null); Problem : This is the top left corner of the frame which is centred but not the entire frame. How can I correct this please, and having the full frame centred? Thank you for your help! Andrew Thompson setLocationRelativeTo(null); ... This is the top left corner of the frame which ... It seems the call is being made at the wrong time, before the frame has assumed the natural size. To fix that, do it in this order. Add all the