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:

Set Antialiasing in Swing

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:

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.