Title: Star Demos
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Star Demos

This demo uses things we have covered so far: mainly transformations. The following images are snapshots of my moving star. We are going to do 3 transforms:

1. Translation to the center
2. Rotation
3. Scaling

I have two versions of this demo: StarDemo1 and StarDemo2. StarDemo1 uses the technique we have been using all along for Graphics2D. Unfortunately a lot of flicker shows up. Run StarDemo1 to see the effect for yourself. From other cps261 videos I have shown how you can eliminate flicker by drawing into an Image buffer.

Instead of doing the Image creation approach we have used in the past, we are going to instead do our drawing into a JPanel that is inside of a JFrame. This is the approach in StarDemo2. The following images are snap shots of the executing program.

StarDemo1.java


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;
import javax.swing.Timer;



public class StarDemo1 extends JFrame implements ActionListener{


    private final int points[][] = {
            {0, 85}, {75, 75}, {100, 10}, {125, 75},
            {200, 85}, {150, 125}, {160, 190}, {100, 150},
            {40, 190}, {50, 125}, {0, 85}
    };

    private Timer timer;
    private double angle = 0;
    private double scale = 1;
    private double delta = 0.1;

    private final int DELAY = 120;

    StarDemo1()
    {
        setSize(500, 500);
        setTitle("StarDemo1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        timer = new Timer(DELAY, this);
        timer.start();
    }

    public void paint(Graphics g)
    {
        int h = getHeight();
        int w = getWidth();


        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, w, h);
        g2d.setColor(Color.blue);

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g2d.translate(w / 2, h / 2);
        GeneralPath star = new GeneralPath();
        star.moveTo(points[0][0], points[0][1]);

        for (int k = 1; k < points.length; k++) {

            star.lineTo(points[k][0], points[k][1]);
        }

        g2d.rotate(angle);
        g2d.scale(scale, scale);
        g2d.fill(star);

        g2d.dispose();

    }
    private void step() {

        if (scale < 0.01) {

            delta = -delta;
        } else if (scale > 0.99) {

            delta = -delta;
        }

        scale += delta;
        angle += 0.1;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        step();
        repaint();
    }

    public static void main(String[] args)
    {
        StarDemo1 sd = new StarDemo1();
    }


}


Using a JPanel to get smooth drawing

- Run StarDemo2 as it is.
- To help you understand the transformations, do the following:
  - comment out translate, rotate, and scale ... run it again
  - uncomment the translate line and run it again.
  - uncomment rotate and run it again
  - uncomment scale but comment rotate again and run it again
- Take a look at the new JPanel class that contains the overall logic.
- Note that the JPanel is constructed and added to the JFrame.
- The underlying architecture now, will automatically clear the screen .... note that we commented out the clearing of the screen
- The underlying JPanel will call paintComponent which then calls paint. Note that I can put my logic in paint which you can see I have by default because it calls doDrawing.
- Or I can remove my paint method and just call doDrawing from paintComponent.
- I will be using the JPanel approach more for my other demos which also would have a problem with flicker.

StarDemo2.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class StarDemoPanel extends JPanel implements ActionListener
{
    private final int points[][] = {
            {0, 85}, {75, 75}, {100, 10}, {125, 75},
            {200, 85}, {150, 125}, {160, 190}, {100, 150},
            {40, 190}, {50, 125}, {0, 85}
    };

    private Timer timer;
    private double angle = 0;
    private double scale = 1;
    private double delta = 0.01;

    private final int DELAY = 10;

    StarDemoPanel()
    {
        timer = new Timer(DELAY, this);
        timer.start();
    }
//    @Override
//    public void paintComponent(Graphics g) {
//
//        super.paintComponent(g);
//        doDrawing(g);
//    }
    public void paint(Graphics g)
    {
        super.paint(g);
        doDrawing(g);
    }
    public void doDrawing(Graphics g)
    {
        int h = getHeight();
        int w = getWidth();


        Graphics2D g2d = (Graphics2D) g.create();
//        g2d.setColor(Color.white);
//        g2d.fillRect(0, 0, w, h);
        g2d.setColor(Color.blue);

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g2d.translate(w / 2, h / 2);
        GeneralPath star = new GeneralPath();
        star.moveTo(points[0][0], points[0][1]);

        for (int k = 1; k < points.length; k++) {

            star.lineTo(points[k][0], points[k][1]);
        }

        g2d.rotate(angle);
        g2d.scale(scale, scale);
        g2d.fill(star);

        g2d.dispose();

    }


    private void step() {

        if (scale < 0.01) {

            delta = -delta;
        } else if (scale > 0.99) {

            delta = -delta;
        }

        scale += delta;
        angle += 0.01;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        step();
        repaint();
    }
}


public class StarDemo2 extends JFrame {



    StarDemo2()
    {
        add(new StarDemoPanel());
        setSize(500, 500);
        setTitle("StarDemo2");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }



    public static void main(String[] args)
    {
        StarDemo2 sd = new StarDemo2();
    }


}



Last Updated: June 21, 2015 0:50 AM