awt

The Need To Restore Graphics Original State When Overwritten paint or paintComponent

a 夏天 提交于 2019-12-03 02:59:52
I realize most of the Java code to overwritten paint or paintComponent, most of them doesn't restore the old state of graphics object, after they had change the state of graphics object. For example, setStroke, setRenderingHint... I was wondering whether it is a good practice that we restore back the old state of graphics object, before returning from the method. For example public void paintComponent(Graphics g) { super.paintComponet(g); Stroke oldStroke = g.getStroke(); g.setStroke(newStroke); // Do drawing operation. g.setStroke(oldStroke); } Is this a good practice? Or it is over done? You

Making a JScrollPane automatically scroll all the way down

拜拜、爱过 提交于 2019-12-03 02:49:30
I am trying to implement a JScrollPane with a JTextArea. The JTextArea is being appended to, and I want the JScrollPane to keep scrolling down as more text is added. How can this be achieved? camickr For (what I think is) a simpler answer check out: Text Area Scrolling . Prior to JDK5, you would have to manually change the caret's position after each append. You can now give this behaviour as a default like this : JTextArea textArea = new JTextArea(); DefaultCaret caret = (DefaultCaret)textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); The advantage of this is that you don

Is it possible to know whether the copied content in clipboard is mp3 file using awt.Toolkit and Clipboard in java

十年热恋 提交于 2019-12-02 23:48:35
问题 I am trying to write a code which runs at background and monitors the copy actions for copying a .mp3 file or a folder containing a .mp3 file { Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); if (cb.isDataFlavorAvailable(DataFlavor.javaFileListFlavor)) { try { String name = ""+cb.getData(DataFlavor.javaFileListFlavor); boolean found = false; if (name.toLowerCase().endsWith(".mp3]")) { System.out.println("Is MP3"); found = true; } if (!found) { System.out.println("Is not MP3")

Any way to embed a Swing JFrame in the AWT Frame from the SWT_AWT bridge?

三世轮回 提交于 2019-12-02 23:22:26
问题 I'm integrating a Swing application into an Bioclipse/Eclipse plug-in, and since Eclipse uses SWT, I have to use the SWT_AWT bridge somehow. The problem is that the starting point I get from the SWT_AWT bridge is an AWT Frame, like so: java.awt.Frame awtFrame = SWT_AWT.new_Frame(composite); ... (which can be filled with AWT components), but the situation is that the Swing application contains much of it's functionality in it's main JFrame component. Now, a Swing JFrame can not be integrated

AlphaComposite.CLEAR not working

左心房为你撑大大i 提交于 2019-12-02 23:04:27
问题 I have two JPanels on a JLayeredpane. One of them displays a pdf and the overlapping one has a transparent background (I have used setOpaque(false)). Now I can add drawings to the transparent panel such that it seems I'm actually annotating the pdf. I want to have a eraser tool to erase these annotations. I tried using the following code @Override public void draw(Graphics2D g2) { g2.setPaint(Color.WHITE); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); g2.setBackground(new

issue with my main method in my connect4 game

▼魔方 西西 提交于 2019-12-02 22:40:51
问题 Im having a problem with my main method, whenevr I run it my code cant compile because of this problem : "Exception in thread "main" java.lang.NullPointerException at UI.ConnectFourFrame.refreshUI(ConnectFourFrame.java:96) at UI.ConnectFourFrame.(ConnectFourFrame.java:43) at main.main(main.java:11)" I am trying to see what the issue is but I cant figure it out, can someone help me package Game; import java.util.ArrayList; public class ConnectFourBoard { // This constant is used to indicate

What is the difference between listeners and adapters?

让人想犯罪 __ 提交于 2019-12-02 22:15:10
I'm trying to differentiate between listeners and adapters. Are they pretty much the same but in listeners you have to implement all the methods in the interface, but with adapters you have an option to only implement the methods you need so the code is cleaners and easier to read? I also got told that adapters enable instantiation with only one implementation and you can't instantiate listeners, I don't fully understand this. Can someone please explain which one is better to use and things you can do with one but you can't with the other? WindowListener is interface which force you to

Use of KeyListener to change string background and set it back when the key is release | Type Tutor program

陌路散爱 提交于 2019-12-02 20:17:29
问题 I have been stock for a couple of hours now lol, not fun but this is why.. I have an app for typing which is to set the background of the button keyboard to a different colour lest say yellow (like highlighted) when typing or key is pressed each button will be highlighted and it will set the background back to white when release.. Well, my KeyListener I think that it doesn't has any focus but I seem to be losing something or not seen it where is that happening.. I had to comment out the

Navigating between multiple panels

孤者浪人 提交于 2019-12-02 19:08:07
问题 Can anyone tell me how to go about coding for navigation between multiple JPanel classes taking the event trigger from JButton from the objects (panels) themselves? I have read about CardLayout . The panel can be swapped from the events happening in the parent panel. What I want to achieve is on click of a button embedded in the panel, it should should disappear or a desired panel should be displayed. Can't seem to find a solution. 回答1: There is nothing about CardLayout that prevents

Graphics - How do I use the method create(int x, int y, int width, int height) and translate(int x, int y)?

冷暖自知 提交于 2019-12-02 18:07:46
问题 I was trying to do my computer science homework but I am stuck as I was trying to use the following methods. public Graphics create(int x,int y,int width,int height) That Creates a new Graphics object based on this Graphics object, but with a new translation and clip area. Parameters: x - the x coordinate. y - the y coordinate. width - the width of the clipping rectangle. height - the height of the clipping rectangle. public abstract void translate(int x,int y) That Translates the origin of