Swing/AWT/SWT :: Rotating Buffered Image And Then Return It
Mar 7, 2010
I wanted to write a method which gets a buffered image as parameter, rotates it 90 degrees clockwise around its top left corner, and then returns it. This method will be call from another method which then draws the rotated image on a rather large background which consists of many other images.
Here is the code I have so far:
public static BufferedImage rotate(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage newImage = new BufferedImage(width, height, img.getType());
Graphics2D g2 = newImage.createGraphics();
g2.rotate(Math.toRadians(90), w/2, h/2);
g2.drawImage(img,null,0,0);
return newImage;
}
This method does rotate an image 90 degrees, but when the calling method recieves this image and displays it on the bigger frame, parts of the rotated image is cut. I think because the frame holding the returned image is not big enough. I've tried playing around with the code a lot, chaging the sizes of different images, and trying AffineTransform features, but I have had no luck.
How would I go about erasing the first buffered image before displaying the second one and eventually a third and fourth so that it appears the image is moving across the screen?
I am trying to read an image I have in the location of my project, So I do this:
When I read it in the try/catch like: BufferedImage image = ImageIO.read(new file(""));
And try to access it after the try/catch, it does not know that image exists, so I need to declare it as a global variable for that class first, and then it works.
public class Gui extends JFrame { private BufferedImage image1; public Gui() { super("MyApp"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); initUI();
I am writing a java program that takes a FROM image, a TO image, and a ratio (this is a slide bar in the GUI). Here's my code: Java Code:
public static BufferedImage rollUp (BufferedImage from, BufferedImage to, double ratio) { BufferedImage finalBufferedImage = new BufferedImage(from.getWidth(), from.getHeight(), from.getType()); int packedColor = 0; for (int r = 0; r < from.getHeight(); ++r) { for (int c = 0; c < from.getWidth() ; ++c) {
[code]...
So from 0 to 1 (ratio is a double between 0 and 1) the image will "roll up". The effect works completely in the GUI, but the console freaks out at about 0.33 ratio.. This program runs for testing in a class Main and uses a class Splittinimage.. this method is in class TwixPix. When the main class is run, a box pops up with a combo box and a slider. You pick an effect (in this case, roll up) and then slide the slider to set the ratio. The image below those two things performs the effect that was selected. Imagine a PowerPoint presentation slide effect.
private void createTextBox() { Graphics g = Game.getG(); Font font = new Font(fontName, fontStyle, fontSize); g.setFont(font); height = BORDER_WIDTH*2 + g.getFontMetrics().getHeight(); int stringWidth = g.getFontMetrics().stringWidth(dialog); width = BORDER_WIDTH*2 + stringWidth;
[Code] .....
This gets called when ever I want to create my Textbox object however what gets created is a purple box (i know that I have set the array elements to purple to begin with) however when I try to use the drawstring method to "draw a string" I get an image which looks like the gif below. I have changed the text size to various sizes but without success. The string is supposed to say "This is a test" but obviously it doesn't.
How can I write a BufferredImage to an 8-bit .bmp using indexed colors stored in a <String,Color> Hashtable?
I've never used Hashtables before, and I didn't know color indexing existed until now, but I can do most other things in java fairly well.
I'm not looking for code, just the concept, as I really don't know how Hashtables work (although I could figure it out), and how color indexing does. I know how to write image files, just not indexed or with a specific number of bits. I am using Hashtables generated from GIMP.
EDIT: I mainly want to know how to save a BufferedImage as an indexed .bmp.
So what I do normally is draw the bottom layer of grass, and when I draw the second layer with trees, the tree layer has a shadow, that shadow doesn't go ontop of the grass layer, but instead it overwrites the grass layer aswell and I've checked the color codes, the shadow has combined with white, not the first layer.
it's obvious that that's not how alpha works then, how do I do it?
Sir, I'am new to Swing Programming. I have to create an application where an image is displayed on one Label. The same image has to be split in parts and stored in database pert wise. Later the user has to retrieve the entire image by viewing a small part of the image. I have already displayed the full image on the Label, now i don't know how to split the image and store it part wise in the database.
I have a java program which rotates a rectangular polygon. My program is having a problem. As soon as it starts rotating, it shrinks. The polygon is rotating perfectly though. I am trying but unable to figure out the cause.Main class which has an infinite loop (game loop) to rotate polygon continuously :-Java Code:
After i draw line between 2 fixed points on two different rectangles i need to rotate them. The problem is that my line is not updated, it stays on the same x1,y1 x2,y2. How to make line to follow rectangle when they are moving or rotating? I wrote some example code to demonstrate this.
Object that i want to draw:
[public class Object { private int xPos; private int yPos; private int width; private int height; float xDistance = 0f; float yDistance = 0f; double angleToTurn = 0.0;
I am having a problem rotating a shape I am using the transformation rotation matrix and don't seem to be getting the results I expected. Now I know a lot of programmers will just tell me to use the Graphics2D rotate(theta) Method.But I would like to create this ability through writing the code.
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1: public class date { public int day; public int month; public int year; }
// File 2: public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
I am making a java swing program. I have a main window that opens a JForm window when the user clicks a menu option. Then I have a button on the JForm that closes the window on click. However, I would also somehow like to make the JForm return values to the main window (according to the states of the selector components) when it is closed. How would I go about this? Currently my jForm is a seperate class called "NewGame", and inside the menu item mouse clicked method, I have the following code:
//open new game jform window NewGame newGameWindow = new NewGame(); newGameWindow.setVisible(true); newGameWindow.setLocationRelativeTo(null); //center window
Is there something I can add here to get the object's values upon the window's close? Or is there a way I can add that into the button clicked method on the actual jForm?
1. The human user of my application asks it to fire up a scanner which scans a paper document. The document is a filled in form. 2. The application then displays the scanned image to the human user.The image is displayed in a JScrollPane since it may be larger than the scroll pane. 3. The human user needs to locate a certain item in the form, for example a social security number, and enter it into a field in a data entry screen.Note that the image and the field appear on the same screen.
Of-course the social security number always appears in the same location in the image. Also the image needs to be the appropriate magnification and resolution to allow the human user to easily read and understand the item he is searching for in the image.
What is the best way to present the scanned image to the user such that he immediately sees the item he requires and can enter it into the data entry field.
I have added the image in the src and bin directories and cross-checked that the name of the image file is correct..Here is the main class
import javax.swing.*; public class apples { public static void main(String args[]) { JFrame frame = new JFrame(); MyDrawPanel wid = new MyDrawPanel(); frame.add(wid); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(300,300); } }
and here is the class that does the image adding part
import java.awt.*; import javax.swing.*;
public class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) {
Image image = new ImageIcon("b.png").getImage(); g.drawImage(image,20, 20, this); } }
My database contains path of images that I want my jComboBox to diaplay. I have written the following code but it does not work for comboBox but works fine when I display image on jLabel etc. How should I go about this
try { ByteArrayInputStream in = new ByteArrayInputStream( transferWorker.bOutput .toByteArray()); BufferedImage bufferedImage = ImageIO .read(in);
[Code] ....
But nothing is happening in the GUI. I have stepped through and bufferedimage contains the data, but its not being displayed. Whats happening is, an image is being transmitted to serial port of laptop, so i'm taking it in and storing it in a BufferOutputStream and then trying to open it in my GUI class.
I have a GUI with several buttons and I'm using NetBeans GUI Builder to do. At the click of one of these I would like for it to open another frame containing a picture. So I associate a listener (actionPerformed) the button and when clicked it opens actually post the new frame.
In the new frame I waxed a JLabel and then I associate the image of the label. I saw that to do that NetBeans generates this code:
My problem is that the picture is overwritten several times during the execution of the program is not changed yet in the frame. That is, in the new frame is always displayed an old version of the image.
I have an image that is created every time I click on the button (it always has the same name and same path). Basically I have a generic tree on which I perform the operations (add, remove, etc..). When I click on the button I call a method that generates the image of the tree (using Graphviz). So I have one image that changes over the time...
How can I do so that the image is always up to date?
The Code:
package View; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JLabel; public class AlberoIpotesi extends javax.swing.JFrame {
Any way to resize an ImageIcon when placing it on a Button so that I don't have to physically rezize the image file?? I am writing an Application, not an applet, so the getImage method won't work.
I can't find any resource on the net about a simple Java code just to move a gif image left or right. I've already accomplished the up, down, and center and they're working fine, thus, I'm still struggling with moving the image left or right. Here's the code.
public class MoveIt extends Applet implements ActionListener { private Image cup; private Panel keypad; public int top = 10; public int left = 10;
[Code] ....
I remember in Visual BASIC it's easily achieved by NameOfImage.left = NameOfImage.left - 10 to move left and NameOfImage.left = NameOfImage.left + 10.