package javaswing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaTestSwing {
static JFrameWin jFrameWindow;
public static class MyComponent extends JComponent{
BufferedImage bufferedImage = null;
Dimension myDimension = new Dimension(50, 50);
public MyComponent() {
try {
bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
int imageWidth = bufferedImage.getWidth();
int imageHeight = bufferedImage.getHeight();
myDimension = new Dimension(imageWidth * 2, imageHeight * 2);
} catch (IOException ex) {
Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public Dimension getPreferredSize() {
return myDimension;
}
@Override
public Dimension getMaximumSize() {
return myDimension;
}
@Override
public Dimension getMinimumSize() {
return myDimension;
}
@Override
protected void paintComponent(Graphics g) {
//g.drawImage(bufferedImage, 0, 0, null);
Graphics2D graphics2D;
//2X
graphics2D = (Graphics2D)g.create();
graphics2D.scale(2, 2);
graphics2D.drawImage(bufferedImage, 0, 0, null);
//1X
graphics2D = (Graphics2D)g.create();
graphics2D.drawImage(bufferedImage, 0, 0, null);
//0.5X
graphics2D = (Graphics2D)g.create();
graphics2D.scale(0.5, 0.5);
graphics2D.drawImage(bufferedImage, 0, 0, null);
//dispose Graphics2D object
graphics2D.dispose();
}
}
public static class JFrameWin extends JFrame{
public JFrameWin(){
this.setTitle("java-buddy.blogspot.com");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyComponent myComponent = new MyComponent();
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(myComponent);
this.add(horizontalBox);
}
}
public static void main(String[] args){
Runnable doSwingLater = new Runnable(){
public void run() {
jFrameWindow = new JFrameWin();
jFrameWindow.setVisible(true);
}
};
SwingUtilities.invokeLater(doSwingLater);
}
}
Tuesday, October 23, 2012
Scale image using Graphics2D.scale()
Example to draw scaled bufferedImage on Graphics2D:
標籤:
code snap,
Graphics2D,
Swing
Friday, October 19, 2012
Example to draw BufferedImage in custom JComponent
package javaswing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaTestSwing {
static JFrameWin jFrameWindow;
public static class MyComponent extends JComponent{
BufferedImage bufferedImage = null;
Dimension myDimension = new Dimension(50, 50);
public MyComponent() {
try {
bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
myDimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
} catch (IOException ex) {
Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public Dimension getPreferredSize() {
return myDimension;
}
@Override
public Dimension getMaximumSize() {
return myDimension;
}
@Override
public Dimension getMinimumSize() {
return myDimension;
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(bufferedImage, 0, 0, null);
}
}
public static class JFrameWin extends JFrame{
public JFrameWin(){
this.setTitle("java-buddy.blogspot.com");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyComponent myComponent = new MyComponent();
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(myComponent);
this.add(horizontalBox);
}
}
public static void main(String[] args){
Runnable doSwingLater = new Runnable(){
public void run() {
jFrameWindow = new JFrameWin();
jFrameWindow.setVisible(true);
}
};
SwingUtilities.invokeLater(doSwingLater);
}
}
Related post:
- Example to display BufferedImage as ImageIcon
Example to display BufferedImage as ImageIcon
package javaeximage;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaTestImage {
static JFrameWin jFrameWindow;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(runJFrameLater);
}
public static class JFrameWin extends JFrame{
public JFrameWin(){
this.setTitle("java-buddy.blogspot.com");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
} catch (IOException ex) {
Logger.getLogger(JavaTestImage.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel jLabel = new JLabel(new ImageIcon(bufferedImage));
JPanel jPanel = new JPanel();
jPanel.add(jLabel);
this.add(jPanel);
}
}
static Runnable runJFrameLater = new Runnable() {
@Override
public void run() {
jFrameWindow = new JFrameWin();
jFrameWindow.setVisible(true);
}
};
}
Related post:
- Example to draw BufferedImage in custom JComponent
Subscribe to:
Posts (Atom)

