Working in Java Swing I would like to be able to dynamically change the icon
of the JFrame as events happen in the code without having any .pngs. Is there any way to draw the image internally and have it then used as the icon, so that I can do something like the below?
public void Icon(Graphics g){
ImageIcon img = g.getImageIcon();
myFrame.setIconImage(img.getImage());
}
Thanks to input from Gilbert Le Blanc, Andrew Thompson I was able to find a way to code this by using buffed image and Graphics2D
public void Icon(){
BufferedImage icon = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = icon.createGraphics();
generateImage(g);
myFrame.setIconImage(icon);
}
Where generateImage is some function or another to paint the graphics onto g.
Related
I’ve been reading the API for Graphics2D and have seen examples of all the available composite modes (that are similar to photoshop blend modes) but I can’t see a way to draw a source image to a target buffered image In a colour that I have specified, for example my source image is a white opaque circle on a fully transparent background, how do I draw using this to a buffer so a coloured circle is drawn.
I would prefer not to construct an intermediate image for performance reasons, is this possible with the api?
EDIT: I have added an image that hopefully helps to show the operation I am trying to describe. This is a common way to draw sprites in open GL etc and I am just wondering how to use the Graphics2D API to do the same thing.
Is is possible using the API but you have to write your own ImageProducer subclass similar to FilteredImageSource but with two input images instead of one. But because of that the end result will require more lines of code than a manual implementation and won't be any more efficient. Alternatively you can use the existing FilteredImageSource and write an ImageFilter subclass that wraps the 2nd image and does the hard work.
Poke me if you decide you want to go with any of these routes.
Specify the location of your image in the imageName below.
public class ColoredCircle extends JPanel {
JFrame frame = new JFrame();
BufferedImage buf;
String imageName = "F://ngc_1300_spiral.jpg";
public static void main(String[] args) {
new ColoredCircle().start();
}
int scale = 10;
public void start() {
try {
buf = ImageIO.read(new File(imageName));
}
catch (IOException ioe) {
ioe.printStackTrace();
}
setPreferredSize(
new Dimension(buf.getWidth() / scale, buf.getHeight() / scale));
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(buf,
0,
0,
buf.getWidth() / scale,
buf.getHeight() / scale,
null);
g2d.dispose();
}
}
I try to create an Image View Program and have a problem in Java Image's zoom in and zoom out :D
I create a JPanel and using BufferedImage to display an image in my computer. After clicking a button, it should be zoom .
But the problem in here that, I overload the method paintComponent() in the JPanel to display a image as I want. After searching in the Google, I think I should use Graphic2D to deal with this problem. Follow on this post, the line
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
should be put in the overloaded method paintComponent(). Howerver, in my case, I want to zoom the image after clicking a button, so, how can i access to the paintComponent() to do the zoom ??
public class MiddlePanel extends JPanel {
private BufferedImage img;
private JButton jbtZoom = new JButton("Zoom");
public MiddlePanel(int width){
img = ImageIO.read(new FileInputStream(new File("C:\\Picture\\pic1.jpg")));
this.setPreferredSize(new Dimension(800,460));
}
public void paintComponent(Graphics g) {
g.drawImage(img......);
}
public void addComponentActionListener(){
jbtZoom.addActionListener(new ActionListener{
public void actionPerformed(){
//What should I do in here to zoom the image....
}
});
}
Thank for your help!
You need to change your design like so:
Store in a variable your zoom state and then your
overridden paintComponent method should look at that variable to
decide if/how much to zoom.
Your ActionListener will update the variable then call repaint() on
the panel.
I'm making my first simple game, that looks like Space Invaders.
I have used paint for drawing my hero on my JPanel. Now I guess if it's possible, in a simple way, to add a background image on my JPanel.
ImageIcon img = new ImageIcon(this.getClass().getResource("back.gif"));
Image image = img.getImage();
setDoubleBuffered(true);
hero = new Hero("hbarrel.gif",350,500);
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(hero.getImage(), hero.getX(), hero.getY(), this);
g2d.drawImage(image,0,0,this);
// Toolkit.getDefaultToolkit().sync();
g.dispose();
}
So, this is it: I tried to use my background img as hero img, and it works! but when I use the code above it only paints my hero img.... so it isn't a resource position issue.
Override paintComponent(Graphics) method and use Graphics.drawImage() to do that in your own custom component.
Well, try to paint the background image first and then the hero so that you have hero above background image
I'm a new poster to stackoverflow, but I've always read the posts for inspiration and I am happy to be a part of the community.
I am drawing an image to a JPanel and then I wish to save that image to a file. The painting on the JPanel goes fine, but when I look at the image it is either all white or all black. I don't know why the image is not saving the way it looks on the JPanel. I guess it is possible I am not correctly referencing the Panel when drawing the image to the buffer and saving it? Its almost like the bufferedImage is blank. I don't have a lot of experience with awt so I have a feeling I'm making a really stupid mistake.
I only overwrite the paintComponent() method once, and in it I do my drawing(which shows up flawlessly on the JPanel) and then at the bottom of it I call the saveImage() method which is supposed to save the image to a file. But as I metioned before, its always a blank image. I use the repaint() method in the constructor.
I won't bog this post down with the entire code. Its a very simple code and the relevant pieces are below.
class drawingBarcode extends JPanel
public drawingBarcode(){
repaint();
try{
Thread.sleep(999);
}catch(InterruptedException e){
e.printStackTrace();
}
public void saveImage() {
BufferedImage bi = new BufferedImage(350, 150, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();//creates and returns a graphics 2d for drawing into buffer
// g2.setColor(color1);
super.paintComponent(g2);
g2.dispose();
try
{
ImageIO.write(bi, "jpg", new File("test.jpg\\"));
}
catch(IOException ioe)
{
System.out.println("Something went wrong");
ioe.printStackTrace();
}
public void paintComponent(Graphics g){
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
setStrokeWithPen1(g2D);
drawAsterix(g2D);//draw asterix(start digit) always
/* some drawing takes place here using g2D. */
g2D.dispose();
saveImage();
}
}
Any help that can be offered or advice would be more than greatly appreciated!
Your save image routine calls super.paintComponent, missing out all your custom paint code when painting to the image graphics!
I would refactor your code - you dont want the file to be saved every time the UI paints do you?
I'm trying to integrate some drawing functionality into my program.
I have a JLabel that has an image set on it.
I want to write a method to return my image:
public Graphics getImage(){
Graphics g = currentImage;
return g
}
But I don't know how to convert it from a JLabel to a graphics object. Then as a simple test I want to draw a line on this image:
public void paint(Graphics g) {
g.drawLine(20, 500, 700, 600);
}
Some help with getting started on this would be great.
Override paintComponent(Graphics g) method of JLabel and place all the drawing code there.
I have a JLabel that has an image set on it.
Create a copy of the image (BufferedImage image2..) and put image2 in the label.
When you need to draw, call image2.getGraphics() for a Graphics object, or image2.createGraphics() for a Graphics2D object.
See this answer for examples of creating and using images.