Commit 8c0b8117 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch14] First updates to SimpleAnimation

parent 6517c409
Loading
Loading
Loading
Loading
+10 −2
Changes for src/main/java/ch14/MyDrawPanel.java: 10 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,13 +2,21 @@ package ch14;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

class MyDrawPanel extends JPanel {

  public void paintComponent(Graphics g) {
    g.setColor(Color.orange);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());

    g.fillRect(20, 50, 100, 100);
    Random random = new Random();
    int red = random.nextInt(256);
    int green = random.nextInt(256);
    int blue = random.nextInt(256);

    Color randomColor = new Color(red, green, blue);
    g.setColor(randomColor);
    g.fillOval(70, 70, 100, 100);
  }

  public static void main(String[] args) {
+8 −8
Changes for src/main/java/ch14/SimpleAnimation.java: 8 added lines, 8 removed lines.
Original line number Diff line number Diff line
package ch14;

import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.*;
import java.util.concurrent.TimeUnit;

public class SimpleAnimation {
  private int x = 70;
  private int y = 70;
  private int xPos = 70;
  private int yPos = 70;

  public static void main(String[] args) {
    SimpleAnimation gui = new SimpleAnimation();
@@ -24,13 +24,13 @@ public class SimpleAnimation {
    frame.setVisible(true);

    for (int i = 0; i < 130; i++) {
      x++;
      y++;
      xPos++;
      yPos++;

      drawPanel.repaint();

      try {
        Thread.sleep(50);
        TimeUnit.MILLISECONDS.sleep(50);
      } catch (Exception e) {
        e.printStackTrace();
      }
@@ -40,7 +40,7 @@ public class SimpleAnimation {
  class MyDrawPanel extends JPanel {
    public void paintComponent(Graphics g) {
      g.setColor(Color.green);
      g.fillOval(x, y, 40, 40);
      g.fillOval(xPos, yPos, 40, 40);
    }
  } 
} 
 No newline at end of file
+1 −1
Changes for src/main/java/ch14/TwoButtons.java: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ public class TwoButtons {
    colorButton.addActionListener(event -> frame.repaint());

    label = new JLabel("I’m a label");
    RandomColorPanel drawPanel = new RandomColorPanel();
    MyDrawPanel drawPanel = new MyDrawPanel();

    frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
    frame.getContentPane().add(BorderLayout.CENTER, drawPanel);