awt

Turn an array of pixels into an Image object with Java's ImageIO?

眉间皱痕 提交于 2019-11-27 19:03:08
I'm currently turning an array of pixel values (originally created with a java.awt.image.PixelGrabber object) into an Image object using the following code: public Image getImageFromArray(int[] pixels, int width, int height) { MemoryImageSource mis = new MemoryImageSource(width, height, pixels, 0, width); Toolkit tk = Toolkit.getDefaultToolkit(); return tk.createImage(mis); } Is it possible to achieve the same result using classes from the ImageIO package(s) so I don't have to use the AWT Toolkit? Toolkit.getDefaultToolkit() does not seem to be 100% reliable and will sometimes throw an

Global Event Listeners with AWTEventListener & how to pull MouseEvent's from it

妖精的绣舞 提交于 2019-11-27 18:49:47
问题 The following question is based on the following information. Scroll down to see the actual question - it refers to the console output specifically. I have stripped out everything, and provided a simple program to reproduce the output below: import java.awt.AWTEvent; import java.awt.Toolkit; import java.awt.event.AWTEventListener; import javax.swing.JFrame; public class Main { static Toolkit tk = Toolkit.getDefaultToolkit(); static long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent

Export JPanel to vector graphics

限于喜欢 提交于 2019-11-27 18:46:55
问题 I would like to export the image in my JPanel to a vector graphics file so it can be edited and printed at a higher-than-screen resolution. Essentially I want its paint() function to be called with a destination Graphics that saves the drawing commands into a vector graphic file. What is a good, simple way to do this? What libraries are recommended? Which vector format would be best, and why? 回答1: Have a look at The Java EPS Graphics2D package. Many Java programs use Graphics2D to draw stuff

Java - Draw a ruler (line with tick marks at 90 degree angle)

♀尐吖头ヾ 提交于 2019-11-27 18:20:34
问题 I'm using Java AWT to draw lines on a panel ( Line2D and Graphics2D.drawLine() ) and I'm wondering how I can draw a line with tick marks, similar to: |----|----|----|----|----| I know the positions I'd like to draw the ticks at in advance. The lines could be in any position, so the ticks must be drawn at an angle releative to the line itself. My basic geometry & ability to apply it in Java is failing me. :) 回答1: I suggest you implement a ruler-drawing-method that draws a simple horizontal

Position Image in any Screen Resolution

拟墨画扇 提交于 2019-11-27 16:28:24
I have a problem with my program. Every time when I change my screen resolution, my image starts to move out of position. Any suggestions on how to make my image stay in the same location even though I change to any other resolution? p2 = new JPanel(); p2.setLayout(new FlowLayout()); ImageIcon img2 = new ImageIcon("C:\\Drum\\Invisible4.png"); jbtn2 = new JLabel(img2); p2.add(jbtn2); add(jbtn2); jbtn2.setSize(jbtn2.getPreferredSize()); jbtn2.setLocation(140, 380); MadProgrammer I prefer to always try and work within boundaries of the framework where I can, it makes life generally easier in the

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

淺唱寂寞╮ 提交于 2019-11-27 15:33:21
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.getLineCount() counts the number of line returns ignoring the wrapped lines. How do I count the number

Why is paint()/paintComponent() never called?

為{幸葍}努か 提交于 2019-11-27 15:11:38
For the last two days I have tried to understand how Java handles graphics, but have failed miserably at just that. My main problem is understanding exactly how and when paint() (or the newer paintComponent() ) is/should be called. In the following code I made to see when things are created, the paintComponent() is never called, unless I manually add a call to it myself or calls to JFrame.paintAll()/JFrame.paintComponents(). I renamed the paint() method to paintComponent() in hoping that would fix my problem of it never being called (even at repaint()), but no luck. package jpanelpaint; import

Getting a HeadlessException: No X11 DISPLAY variable was set

…衆ロ難τιáo~ 提交于 2019-11-27 14:36:24
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 solve it? I assume you're trying to tunnel into some unix box. Make sure X11 forwarding is enabled in

JFrame: get size without borders?

一世执手 提交于 2019-11-27 14:35:53
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. frame.getContentPane().getSize(); 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().getHeight()); System.out.println("width of left + right borders: "+(getWidth()-getContentPane ().getWidth())); System.out

Character display/search for Unicode characters

五迷三道 提交于 2019-11-27 14:30:30
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 allows us to jump to any code point of interest. The names of the less obscure Unicodes in a list,