1
import javax.swing.*;
import java.awt.*;

public class Test1 {
int x = 70;
int y = 70;

public static void main (String[] args) {
    Test1 gui = new Test1 ();
    gui.go();
}
public void go() {
    JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyDrawPanel drawPanel = new MyDrawPanel();
    frame.getContentPane().add(drawPanel);
    frame.setSize(300,300);
    frame.setVisible(true);

    for (int i = 0; i < 130; i++) {
        x++; y++;
        drawPanel.repaint();
        try { Thread.sleep(50);
        } catch(Exception ex) { } }
}// close go() method

class MyDrawPanel extends JPanel {
    public void paintComponent(Graphics g) {
        g.setColor(Color.green);
        g.fillOval(x,y,40,40);
    }
} // close inner class
} // close outer class

page1page2

According to the page 2, the circle should be smeared in the frame... but actually, when I ran it, it just moved without smearing. Why was that? btw, if these codes were not able to make a smearing circle, how could I make a smearing one?

cheers

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Eddie Hong
  • 13
  • 1
  • 4

1 Answers1

2

As shown here, "If you do not honor the opaque property you will likely see visual artifacts." Indeed, running your example on Mac OS X with Java 6 produces a series of circles that appear "smeared."

image

How could I make a smearing one?

Code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test2 {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test2()::display);
    }

    public void display() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final MyDrawPanel drawPanel = new MyDrawPanel();
        frame.add(drawPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class MyDrawPanel extends JPanel {

        private int x = 30;
        private int y = 30;
        private final List<Shape> list = new ArrayList<>();

        public MyDrawPanel() {
            new Timer(50, (new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    x++;
                    y++;
                    list.add(new Ellipse2D.Double(x, y, 40, 40));
                    repaint();
                }
            })).start();
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.green);
            for (Shape s : list) {
                g2d.fill(s);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
}

Emphasis mine.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045