Save drawing in JComponent into Tiff file - java

How to save drawing in JComponent into tiff format? I only know how to save the whole Java file but I dont know how to save specific Jcomponent. Help me :-(
EDITED:
Thanks guys, now I am able to save my drawing to Jpeg.
However I just wanted to save one of the component? The c.paintAll(bufferedImage.getGraphics()); seem to save the whole component. However, I just want to save this component c.add(new PaintSurface(), BorderLayout.CENTER); without panel.add(saveBtn); How can I do that? Thanks.
Container c = getContentPane();
c.setLayout(new BorderLayout());
Panel panel = new Panel();
panel.add(saveBtn);
c.add("South", panel);
c.setBackground(Color.WHITE);
c.add(new PaintSurface(), BorderLayout.CENTER);

This is essentially identical to broschb's solution only using correct syntax and actually calling the appropriate JAI routines.
public void saveComponentAsTiff(Component c, String filename, boolean subcomp) throws IOException {
saveComponentTiff(c, "tiff", filename, subcomp);
}
public void saveComponent(Component c, String format, String filename, boolean subcomp) throws IOException {
// Create a renderable image with the same width and height as the component
BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
if(subcomp) {
// Render the component and all its sub components
c.paintAll(image.getGraphics());
}
else {
// Render the component and ignoring its sub components
c.paint(image.getGraphics());
}
// Save the image out to file
ImageIO.write(image, format, new File(filename));
}
Documentation for the various functions can be found here:
Component.paint(Graphics)
Component.paintAll(Graphics)
BufferedImage(int, int, int)
BufferedImage.getGraphics()
ImageIO.write(RenderedImage, String, File)
If you want to save in a format other than tiff you can use ImageIO.getWriterFormatNames() to obtain a list of all image output formats currently loaded by the JRE.
UPDATE: If you are not interested in painting sub components you can substitute the call to Component.paintAll(...) with Component.paint(...). I have altered the example code to reflect this. Setting subcomp to true with render the subcompnents and setting it to false will ignore them.

The ScreenImage class allows you to save an image of any component.

you can get a Buffered Image of the component, or the panel that contains the drawing by creating a buffered image the size of the panel. You can then paint the panels contents onto the buffered image. You can then use JAI(Java Advanced Imaging) library to save the buffered image as a tiff. You'll have to check the docs on that here.
JComponent component; //this is your already created component
BufferedImage image = new BufferedImage(component.getWidth(),
component.getHeight(),
Bufferedimage.TYPERGB)
Graphics g = image.getGraphics;
component.paintComponent(g);
The syntax may be slightly off, i'm not at an idea, but that is the general idea. You can then use JAI and convert the buffered image to a TIFF.

Related

How to set size of custom button?

I've created a GUI program with custom icons for buttons. I'm unable, however, to set the size of these buttons in Java, so they remain their original size, 230x227. I'm trying to get them to be around 20x20 so I used the following code:
classAlcBtn.setPreferredSize(new Dimension(20,20));
classAlcBtn.setIcon(new ImageIcon(getClass().getResource("Alchemist.png")));
classAlcBtn.setBorder(null);
classAlcBtn.setBorderPainted(false);
classAlcBtn.setContentAreaFilled(false);
classAlcBtn.setPressedIcon(
new ImageIcon(getClass().getResource("alchemistClicked.png")));
classAlcBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
Is there a way to force these icons to size down, or do I have to size down the actual icon file? Thanks for any help.
Assuming you can size down the buttons, without icons. So use the following method to size down the image, without changing the size of original file:
ImageIcon icon = new ImageIcon("whatever.jpg");
Image img = icon.getImage() ;
Image newImg = img.getScaledInstance( NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH ) ;
icon = new ImageIcon( newImg );
...
classAlcBtn.setIcon(icon);
And if Button resizing itself is not working, then you can try using setMaximumSize() instead of setPreferredSize() method as following:
classAlcBtn.setMaximumSize(new Dimension(100,100));
See this for more info. about sizes. Hope this Helps:)

Problems with the alpha parameter when setting a background color for JTextFields

I am writing a chat program and I was trying to make it more interesting by adding a background image to the main JPanel (the one on which everything is built). When I try to change the transparency of a JTextField or JTextArea 50% using the code
MainPanel = new JPanel(){
#Override
public void paintComponent(Graphics g) {
ImageIcon im = new ImageIcon("background.jpg");
Image i = im.getImage();
g.drawImage(i,0,0,this.getSize().width,this.getSize().height,this);
}
};
...
outputWindow.setBackground(new Color(0,0,0,128));
where 'outputWindow' is a JTextArea added to 'MainPanel', I get a lot of garble once I display some text in the area.
Screenshot of the problem
I typed in "hi" and "hello world" in that order, which you can see at the top of the output screen, but is there any way to get rid of the extra garble?
For each Swing component which is partially transparent, be sure you call setOpaque(false);
https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/

How do you Scale and display specific sections of an image using Java

So I took it upon myself to learn Java.
A decision I regret more with every crash of Elipse.
Fortunately I have actually managed to get this block of my self inflicted project to actually 'work' but obviously being self taught I am quite sure I have made a lot of errors in my layout and
In total, the program will create a JFrame then stick a JscrollPane inside that into which it inserts a a JPanel (wrapPage). It then loops through a process that generates an array of TMTile Objects which are extended JPanels containing the tile images which are drawn from a source folder of jpg images. Once that has finished it places that array as a grid using the GridBagLayout within the wrapPage Jpanel resulting in a nice little perfect maze.
That all works perfectly, but the big let down is that the size of the image used to create the tiles is dictating everything. I can't for the life of me figure out how to scale the image and efforts to find a suitable process have only got me methods of creating new image files or alternating between stretching and tiling images to fit a within their containing component or suggestions I just couldn't follow to save my life.
Fortunately. The image handling is part of the TMTile class file! This means I can show you the entire relevant bit of script;
The following are imported for use in this file
from java.awt: Color, GridBagConstraints, GridBagLayout, Insets
from javax.swing: ImageIcon, JLabel, JPanel
public class TMTile extends JPanel
{
private static final long serialVersionUID = 1L;
private int paths; // values 0 to 15, uses bitwise & | to set or check open paths
private JLabel tileWrap; // to contain the ImageIcon
private ImageIcon tileImg; // the image to be used
GridBagConstraints bag;
public TMTile( int inDir ) // called by maze constructor
{
paths = inDir;
this.setBackground( Color.RED ); // so I can tell if the image didn't load right
this.setLayout( new GridBagLayout() ); // GridBagLayout is probably overkill but it what I am most familiar with now.
bag = new GridBagConstraints();
bag.insets = new Insets( 0, 0, 0, 0 );
tileImg = tileImage( paths );
tileWrap = new JLabel( "", tileImg, JLabel.CENTER );
this.add( tileWrap, bag );
}
public void open( int inDir ) // called by maze constructor when tile value changes resulting from the perfect maze backtrack method
{
paths = paths | inDir;
tileImg = tileImage( paths );
tileWrap.setIcon( tileImg );
}
private ImageIcon tileImage( int paths ) // created to cut down on duplicate code and make updating easier
{
String inEnd;
if(paths < 10)
{
inEnd = "0"+ paths;
}
else
{
inEnd = ""+ paths;
}
ImageIcon tileImg = new ImageIcon( "imgs/MAZE_"+ inEnd +".jpg" );
System.out.println( "imgs/MAZE_"+ inEnd +".jpg" );
Image newimg = tileImg.getImage().getScaledInstance( 40, 40, java.awt.Image.SCALE_DEFAULT );
tileImg = new ImageIcon( newimg );
return tileImg;
}
public int getOpen()
{
return paths;
}
}
Thanks to nachokk and MadProgrammer I now once again have a working maze program and the maze tiles are scalable. That just leaves the final goal of doing away with individual tile .jpgs and switching to a single image file with all 16 stored within in.
What I would love to have is the ability to utilize a single large image file which is divided into 16 sections, 1 section for each tile value. I started out working toward this goal but had to abandon it fairly quickly as I couldn’t figure out how to only display the section of the image needed which would also need to be scaled in the way described above.
Since I am very much still learning Java advice on any alternatives is welcome but ideally I would love to know how to accomplish this as planned.

Java Graphics2D transparent background

I have a Graphics2D object and I want to set up the background of the object. It has a setBackground method, which has a Color parameter. This way I can set the color of the background.
My question is: how can I set the transparency of the background of the object? Can I somehow tell it to be completely transparent? Can I somehow tell it to be completely opaque? Can I somehow tell it to have 0.8 transparency/opacity? How can I set these values?
I have seen that there are int predefined values called TRANSLUCENT and OPAQUE, but I am not sure how can I use them.
Maybe the correct usage is to call the constructor of Color with an int parameter?
You can construct a Color object by specifying a transparency. For example the following code constructs a RED color with 50% transparency
Color c=new Color(1f,0f,0f,.5f );
You can call the constructor of Color in the following way:
Color c = new Color(r,g,b,a);
where a is the alpha (transparency) value.
As with all Java classes, you can find this information in the official API: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
It's a really good resource and can spare you waiting for an answer on here.
You may try this if you are using a JPanel :
jPanel1.setOpaque(false);
jPanel1.setBackground(new Color(0,0,0,200));
/*This will put a transparent black color on a panel, the important part of the code is: .setBackground(new Color(0,0,0,200));*/
Java is actually pretty good at this stuff, you can achieve transparency and much more. Here's some code for a simple transparent window I copied from oracle:
package misc;
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class TranslucentWindowDemo extends JFrame {
public TranslucentWindowDemo() {
super("TranslucentWindow");
setLayout(new GridBagLayout());
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add a sample button.
add(new JButton("I am a Button"));
}
public static void main(String[] args) {
// Determine if the GraphicsDevice supports translucency.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
//If translucent windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println(
"Translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TranslucentWindowDemo tw = new TranslucentWindowDemo();
// Set the window to 55% opaque (45% translucent).
tw.setOpacity(0.55f);
// Display the window.
tw.setVisible(true);
}
});
}
}
Look here for more information.
If what you want is to give a transparent effect use the Color properties to 4 variables:
this.setBackground (new Color (0,0,0, .5f));
this gives the background the RGB color of the first three parameters (*new Color (** 0,0,0, **. 5f)*) and the fourth is used to determine the percentage of opacity (opaque )
If you want the background not to be displayed, use the value null
this.setBackground (null);
Many use setOpaque (false); but that takes away the padding not the background.
Use the constructor of the color like this:
Color color = new Color(152,251,152, 50);
The value 50 is for the transparency.

Make JCheckbox bigger..?

i want to make my JCheckboxes in a JTable bigger (for Touchscreen), but it doesn't change the size.
I tried it with
setPrefferedSize
setSize
What should I do?..
I assume you mean you want a bigger check box. If so then you need to create images to represent the unselected and selected icons of the check box. Then you can create a renderer and editor using these icons. Finally you would need to increase the height of each row in the table. The code might look something like:
Icon normal = new ImageIcon(...);
Icon selected = new ImageIcon(...);
JTable table = new JTable(...);
table.setRowHeight(...);
TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
JCheckBox checkBoxRenderer = (JCheckBox)renderer;
checkBoxRenderer.setIcon( normal );
checkBoxRenderer.setSelectedIcon( selected );
DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Boolean.class);
JCheckBox checkBoxEditor = (JCheckBox)editor.getComponent();
checkBoxEditor.setIcon( normal );
checkBoxEditor.setSelectedIcon( selected );
IMPORTANT NOTE: This was only tested with the default 'Metal' look and feel. I do not guarantee that this will work for any other look and feel. Also I am not entirely sure how it works because it is admittedly a bit of a hack.
I was able to solve this in a slightly different way.
I wanted to use the existing images and just apply a scale to it. I am already scaling the font of my application using the UI defaults and so I have a rather large font. I wondered if I could leverage that and scale the check boxes accordingly.
After scouring the internet and trying a bunch of things I came up with this method:
public static void scaleCheckBoxIcon(JCheckBox checkbox){
boolean previousState = checkbox.isSelected();
checkbox.setSelected(false);
FontMetrics boxFontMetrics = checkbox.getFontMetrics(checkbox.getFont());
Icon boxIcon = UIManager.getIcon("CheckBox.icon");
BufferedImage boxImage = new BufferedImage(
boxIcon.getIconWidth(), boxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
);
Graphics graphics = boxImage.createGraphics();
try{
boxIcon.paintIcon(checkbox, graphics, 0, 0);
}finally{
graphics.dispose();
}
ImageIcon newBoxImage = new ImageIcon(boxImage);
Image finalBoxImage = newBoxImage.getImage().getScaledInstance(
boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
);
checkbox.setIcon(new ImageIcon(finalBoxImage));
checkbox.setSelected(true);
Icon checkedBoxIcon = UIManager.getIcon("CheckBox.icon");
BufferedImage checkedBoxImage = new BufferedImage(
checkedBoxIcon.getIconWidth(), checkedBoxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
);
Graphics checkedGraphics = checkedBoxImage.createGraphics();
try{
checkedBoxIcon.paintIcon(checkbox, checkedGraphics, 0, 0);
}finally{
checkedGraphics.dispose();
}
ImageIcon newCheckedBoxImage = new ImageIcon(checkedBoxImage);
Image finalCheckedBoxImage = newCheckedBoxImage.getImage().getScaledInstance(
boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
);
checkbox.setSelectedIcon(new ImageIcon(finalCheckedBoxImage));
checkbox.setSelected(false);
checkbox.setSelected(previousState);
}
What it does is get the size of the font from the checkbox's font metrics. Then using that it derives a new icon based on the icon found in the 'Look and Feel'.
One odd thing that I am not able to explain is how the icon for the checkbox in its 'un-selected' or default state, changes to the 'selected' icon, when I am accessing the same property to get each one.
I start by saving the state of the control so I can restore it at the end. This is done because in order for the icons to be set properly, the state needs to be unchecked when you first request the icon from the UIManager and then it will need to be checked when you request the icon the second time to get the 'selected' icon.
I am not entirely sure how the UIManager works or why the checkbox icon changes when we call the same property just by setting the 'selected' value of a single checkbox, but that is what is required in order to get both the necessary icons.
If you did not want to base the size on the font you could easily just pass in the height and width as parameters and use them instead of the font's height when setting the buffered image size.
I might mention that this same methodology works with radiobuttons

Categories

Resources