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.drawImage(bufferedImage, 0, 0, null); //1X graphics2D = (Graphics2D)g.create(); graphics2D.translate(30, 0); graphics2D.drawImage(bufferedImage, 0, 0, null); //0.5X graphics2D = (Graphics2D)g.create(); graphics2D.translate(60, 0); 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
Translate image using Graphics2D.translate()
Example to draw bufferedImage on translated Graphics2D:
標籤:
code snap,
Graphics2D,
Swing
Scale image using Graphics2D.scale()
Example to draw scaled bufferedImage on Graphics2D:
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); } }
標籤:
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
Wednesday, October 17, 2012
Create and rotate Graphics2D object
In this example, we create a new Graphics2D object (graphics2D) from original Graphics object (g), by calling it's create() method. Then rotate the Graphics2D object, by calling its rotate() method. After used, dispose it.
package javaswing; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; 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{ Dimension myDimension = new Dimension(500, 500); @Override public Dimension getPreferredSize() { return myDimension; } @Override public Dimension getMaximumSize() { return myDimension; } @Override public Dimension getMinimumSize() { return myDimension; } @Override protected void paintComponent(Graphics g) { //fill background g.setColor(Color.gray); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.darkGray); g.fillRect(50, 50, 150, 150); //rotate about 0, 0 Graphics2D graphics2D = (Graphics2D)g.create(); graphics2D.rotate(Math.toRadians(45)); graphics2D.setColor(Color.red); graphics2D.fillRect(50, 50, 150, 150); //rotate about 100, 100 graphics2D = (Graphics2D)g.create(); graphics2D.rotate(Math.toRadians(45), 100, 100); graphics2D.setColor(Color.blue); graphics2D.fillRect(50, 50, 150, 150); //dispose Graphics2D object graphics2D.dispose(); } } public static class JFrameWin extends JFrame{ public JFrameWin(){ this.setTitle("java-buddy.blogspot.com"); this.setSize(500, 500); 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 9, 2012
Watch the full-length replay of JavaOne 2012 keynotes
Visit http://medianetwork.oracle.com/media/channels/2012-javaone/1866721757001 to watch the full-length replay of JavaOne 2012 keynotes.
Convert int to String in hexadecimal
To convert int to String in hexadecimal format, you can use the code:
Integer.toHexString(int_number)
Integer.toHexString(int_number)
Sunday, October 7, 2012
Set Antialiasing in Swing Graphics2D object
To set Antialiasing On/Off in Swing Graphics2D object, call the method setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON)/setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF).
See the effect, the line in middle is set VALUE_ANTIALIAS_ON:
See the effect, the line in middle is set VALUE_ANTIALIAS_ON:
package javaswing; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; 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{ Dimension myDimension = new Dimension(300, 500); @Override public Dimension getPreferredSize() { return myDimension; } @Override public Dimension getMaximumSize() { return myDimension; } @Override public Dimension getMinimumSize() { return myDimension; } @Override protected void paintComponent(Graphics g) { //Create a Graphics2D object from g Graphics2D graphics2D = (Graphics2D)g; //Default Antialiasing graphics2D.drawLine(50, 50, 250, 250); //Antialiasing ON graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.drawLine(50, 150, 250, 350); //Antialiasing OFF graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); graphics2D.drawLine(50, 250, 250, 450); } } public static class JFrameWin extends JFrame{ public JFrameWin(){ this.setTitle("java-buddy.blogspot.com"); this.setSize(300, 500); 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); } }
Custom JComponent
javax.swing.JComponent is the base class for all Swing components except top-level containers.
Example to create custom JComponent:
Example to create custom JComponent:
package javaswing; import java.awt.Dimension; import java.awt.Graphics; 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{ Dimension myDimension = new Dimension(300, 100); @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.drawLine(0, 0, getWidth()-1, getHeight()-1); g.drawLine(0, getHeight()-1, getWidth()-1, 0); g.drawRect(0, 0, getWidth()-1, getHeight()-1); } } public static class JFrameWin extends JFrame{ public JFrameWin(){ this.setTitle("java-buddy.blogspot.com"); this.setSize(350, 250); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyComponent myComponent1 = new MyComponent(); MyComponent myComponent2 = new MyComponent(); Box verticalBox = Box.createVerticalBox(); verticalBox.add(myComponent1); verticalBox.add(myComponent2); this.add(verticalBox); } } public static void main(String[] args){ Runnable doSwingLater = new Runnable(){ public void run() { jFrameWindow = new JFrameWin(); jFrameWindow.setVisible(true); } }; SwingUtilities.invokeLater(doSwingLater); } }
Tuesday, October 2, 2012
Beginning Programming with Java For Dummies, 3rd Edition
Java is a popular language for beginning programmers, and earlier editions of this fun and friendly guide have helped thousands get started. Now fully revised to cover recent updates for Java 7.0, Beginning Programming with Java For Dummies, 3rd Edition is certain to put more first-time programmers and Java beginners on the road to Java mastery.
- Explores what goes into creating a program, putting the pieces together, dealing with standard programming challenges, debugging, and making the program work
- Offers new options for tools and techniques used in Java development
- Provides valuable information and examples for the would-be programmer with no Java experience
- All examples are updated to reflect the latest changes in Java 7.0
Beginning Programming with Java For Dummies, 3rd Edition offers an easy-to-understand introduction to programming through the popular, versatile Java 7.0 language.
Subscribe to:
Posts (Atom)