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
No comments:
Post a Comment