awt

Serialize a java.awt.geom.Area

半世苍凉 提交于 2019-12-04 04:20:15
问题 I have the need to serialize an Area object (java.awt.geom.Area) in a socket. However it doesn't seem to be serializable. Is there a way to do such thing? Maybe by converting it into a different object? Thanks in advance 回答1: I found this workaround: AffineTransform.getTranslateInstance(0,0).createTransformedShape(myArea) This results in a shape that can be serialized. 回答2: Use XStream to trivially convert it to/from XML. You don't need your object to implement particular interfaces, and the

Java - Create a shape from border around image

六月ゝ 毕业季﹏ 提交于 2019-12-04 04:09:23
问题 i have a class that draws a shape from a png image, so that i can use the shape to draw the border of custom buttons that i need for my project. here's the code for the class to draw the shape of an image: public class CreateShapeClass { public static Area createArea(BufferedImage image, int maxTransparency) { Area area = new Area(); Rectangle rectangle = new Rectangle(); for (int x = 0; x < image.getWidth(); x++) { for (int y = 0; y < image.getHeight(); y++) { int rgb = image.getRGB(x, y);

java.awt.HeadlessException thrown from HeadlessGraphicsEnvironment.getDefaultScreenDevice

牧云@^-^@ 提交于 2019-12-04 04:05:55
问题 I need to do some image processing on a java server (Debian with java version "1.6.0_12"), and I am receiving java.awt.HeadlessException from my code: java.awt.HeadlessException at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:64) at WaxOn.getDefaultConfiguration(WaxOn.java:341) Even when java.awt.headless is set to true (as evident by this code printing so): if (!java.awt.GraphicsEnvironment.isHeadless()) { logger.warn("Headless mode is not

How to import a custom java.awt.Font from a Font family with multiple TTF files? (An example is included)

痴心易碎 提交于 2019-12-04 03:49:32
I know that you can import a Font in Java with something like this: File file = new File(fontPath); Font font = Font.createFont(Font.TRUETYPE_FONT, file); // alternative: // Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(file)); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(font); Then you would use for example font.deriveFont(Font.PLAIN, 20); to get the desired style and size. Example But now let's look as an example at the font Consolas , there you have four TTF files: consola.ttf (Plain) consolab.ttf (Bold) consolai.ttf

Fastest way to create a Java message dialog (swing/awt/other)?

我的梦境 提交于 2019-12-04 02:53:39
问题 I'm creating a Java application that will do some processing then needs to display a message to give the user feedback. However, it appears to be incredibly slow - taking over two seconds to return. I stripped the source down to the apparent culprit, and here is the code used: package SwingPlay; import javax.swing.JFrame; public class Dialog { public static void main( String[] args ) { JFrame frame = new JFrame( "DialogDemo" ); } } I'm executing this from the command line with: java

Java - Screen turns black, when setting a JFrame to Fullscreen

爷,独闯天下 提交于 2019-12-04 02:18:00
问题 I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen. Before the screen turns black I shortly can see the pink background of the canvas. Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly. Here is my code: public class FullscreenTest extends Canvas { private JFrame

best way to combine guava eventbus and AWT Event thread handling

牧云@^-^@ 提交于 2019-12-04 01:17:59
When you have a asynchronous event bus, and fire events, lets say within the model which get catched in the UI you have probably the following problem: The registered handler gets executed in a worker thread, but all UI swing changes need to be executed within the AWT event thread. This means you need to envelope all your handler clode in EventQueue.invokeLater(...) . This looks like a lot of boiler plate code. I wonder if there is a smarter solution for that problem. What about an extension to the guava event bus that marks a handler for execution within a special thread? This could be marked

How to close the window in AWT?

人走茶凉 提交于 2019-12-04 00:44:06
问题 I am creating a small application using AWT. When I try to close the window, the "close" button doesn't work. Here's my code: import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; class ButtonDemo1 implements ActionListener { Button b1; TextField tf; Frame f; ButtonDemo1(String s) { f = new Frame(s); b1 = new Button("OK"); tf = new TextField(10); f.setSize(200, 250); f.setVisible(true); b1.addActionListener(this); f.add(tf); f.add(b1); f.addWindowListener(new

Can you increase line thickness when using Java Graphics for an applet? I don't believe that BasicStroke works [duplicate]

拜拜、爱过 提交于 2019-12-04 00:40:35
This question already has an answer here: Java2D: Increase the line width 2 answers I am having trouble adjusting line thickness. Can I do that in Graphics or do i have to do it in Graphics2D? If so, how do I alter the program to make it run? Thanks! import java.applet.Applet; import java.awt.*; public class myAppletNumberOne extends Applet { public void paint (Graphics page) { //Something here??? } } Yes you have to do it in Graphics2D, but that's hardly an issue, as every Graphics in Swing is a Graphics2D object (it just keeps the old interface for compatibility reasons). public void

Draw rectangle border thickness

独自空忆成欢 提交于 2019-12-03 22:23:52
Is it possible to do draw a rectangle with a given border thickness in an easy way? If you are drawing on a Graphics2D object, you can use the setStroke() method: Graphics2D g2; double thickness = 2; Stroke oldStroke = g2.getStroke(); g2.setStroke(new BasicStroke(thickness)); g2.drawRect(x, y, width, height); g2.setStroke(oldStroke); If this is being done on a Swing component and you are being passed a Graphics object, you can downcast it to a Graphics2D . Graphics2D g2 = (Graphics2D) g; Hatto Here's how to do this : Border with colored line with thickness 5. Border linebor = BorderFactory