awt

ImageIO saves back to original size

和自甴很熟 提交于 2019-12-02 13:35:02
问题 I've been searching for some solutions from the internet yet I still haven't found an answer to my problem. I've been working or doing a program that would get an image file from my PC then will be edited using Java Graphics to add some text/object/etc. After that, Java ImageIO will save the newly modified image. So far, I was able to do it nicely but I got a problem about the size of the image. The original image and the modified image didn't have the same size. The original is a 2x3 inches

How to convert Image to BufferedImage in Java?

馋奶兔 提交于 2019-12-02 13:12:51
How to convert Image to BufferedImage in Java? Note, that existing answer is apparently not correct, because it uses methods getWidth(null) and getHeight(null) , which can return -1 if image is not loaded yet ( Image is, by definition, an asynchronous object). Please, provide either more correct answer, or more proofs that existing answer is already correct. If it's important to you, you can use a MediaTracker to "wait" for the image to be loaded, then you don't need to care about supplying a ImageObserver try { MediaTracker mt = new MediaTracker(new JPanel()); Image image = Toolkit

Get a color from the user as a String and use it in a method that accepts enum values?

谁说我不能喝 提交于 2019-12-02 13:03:57
How to get a color from the user as a String and use it in a method that accepts Color enum values? The idea is to get a color that the user chooses and pass the value (or handle the situation any other way) to a method element.setBackground(java.awt.Color) . I would create a Map<String, Color> and populate it with what String color names map to which Color objects. You can use java.awt.Color 's own static Color constants , e.g. colorMap.put("BLACK", Color.BLACK); , or you can insert your own mappings. Then you can take the user input and perform a lookup with get to get the proper Color

Shapes combination in Java?

我们两清 提交于 2019-12-02 12:43:38
问题 Does java Shape interface contract and library routines allow combining multiple shapes into one object extending Shape interface? For example, may I define class Flower which will consist of several ovals for petals and core? Or the Shape supposes only one continuous outline? If so then is there any class in Java for holding multiple shapes, may be some class for vectorized graphics? 回答1: To manipulate shapes in Java like you're describing, you want to use the Area class, which has these

GraphicalEnvironment does not update screen devices after switching off second screen

浪尽此生 提交于 2019-12-02 12:21:51
问题 I have two monitors I write very small Swing Java code to collect info of all screen devices combine changing display mode with one or two display screen by setting Display in Control Panel. And code like below: import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String[] args) { final JFrame frame = new

Using CardLayout for multiple JPanels and nothing displays

冷暖自知 提交于 2019-12-02 12:00:48
I'm making a simple (and bogus) computer power consumption calculator. I'm using a card layout to put multiple panels in but when I run it, there's just a small window not displaying anything. Here's my long code, I put it all in one class. package my.Project; import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.text.DecimalFormat; import javax.swing.*; public class MainProject extends JFrame { CardLayout cl; int motherboardP, oddP, hddP, ssdP, fanP, cpuP, gpuP, monitorP, hoursint; int ramNum, hddNum,

Cast jzy3d.canvas to awt.component

痞子三分冷 提交于 2019-12-02 11:57:26
I need cast the jzy3d canvas to java.awt.component, I want to display the chart in my frame with JCombobox and button but when I want to cast canvas to component, the program was dropped. Thank you for your answers. I have try this and don't help me. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jzy3d.plot3d.rendering.canvas.VoidCanvas cannot be cast to java.awt.Component at cz.bia.Launcher.lambda$setComponents$0(Launcher.java:63) at java.awt.Button.processActionEvent(Button.java:409) at java.awt.Button.processEvent(Button.java:377) Launcher.java: package cz.bia;

How do I get a Raster from an Image in java?

房东的猫 提交于 2019-12-02 11:34:32
问题 I'm trying to load a gif image from a url into a java.util.image.Raster so I can manipulate it. The only method for loading and decompressing an image I could find was Toolkit.getImage, which returns a java.awt.Image. I need to turn that into a Raster so I can work with it. Suggestions? 回答1: Load your Image into a Buffered Image and then get the data from it BufferedImage img = null; try { img = ImageIO.read(new File ("c:/imageFile.gif")); } catch(Exception e) {} Raster R=img.getData(); 回答2:

Java AWT/Swing: Get notified on change of position/size of my window

妖精的绣舞 提交于 2019-12-02 11:25:41
I've got a window and I would like to get notified when the position or the size has changed. Which type of listener is the best to take? WindowListener didn't work for me. camickr You can use a ComponentListener which has the componentResized and componentMoved methods. You might want to try the HierarchyBoundsListene r myJFrame.getContentPane().addHierarchyBoundsListener(new HierarchyBoundsListener(){ public void ancestorMoved(HierarchyEvent e) { doSomething(); } public void ancestorResized(HierarchyEvent e) { doSomethingElse(); } see http://java.sun.com/docs/books/tutorial/uiswing/events

How do I call Graphics Method inside KeyListener?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 11:24:20
问题 Ahead of time, I would like to say there have been many posts similar to this, however, none of them apply to me or none of them actually have any answer at all, and are outdated, meaning there could be new java features that could help me solve my problem. Anyway, I wanted to make a game where there is tennis rackets. Of course, they would have to rotate. In order to rotate, I must call my Graphics2D method inside my KeyListener. How would I do this WITHOUT adding a new Graphics2D variable