Aggiunta grafica e ricorsione

This commit is contained in:
2023-05-23 21:49:49 +02:00
parent 2bfc3a410c
commit 76aa0080c6
37 changed files with 4779 additions and 2394 deletions

View File

@@ -1,28 +1,28 @@
package investment;
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance() {
return balance;
}
}
package investment;
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance() {
return balance;
}
}

View File

@@ -0,0 +1,31 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package investment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author radaelli11353
*/
class InterestListener1 implements ActionListener {
private static final double INITIAL_BALANCE = 1000;
private final BankAccount account;
private final double INTEREST_RATE;
public InterestListener1(double interest) {
account = new BankAccount(INITIAL_BALANCE);
INTEREST_RATE = interest;
}
@Override
public void actionPerformed(ActionEvent event) {
double interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
System.out.println("saldo: " + account.getBalance());
}
}

View File

@@ -0,0 +1,35 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package investment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
/**
*
* @author radaelli11353
*/
class InterestListener2 implements ActionListener {
private static final double INITIAL_BALANCE = 1000;
private BankAccount account;
private final double INTEREST_RATE;
private final JLabel label;
public InterestListener2(double interest, JLabel label) {
account = new BankAccount(INITIAL_BALANCE);
INTEREST_RATE = interest;
this.label = label;
}
@Override
public void actionPerformed(ActionEvent event) {
double interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
label.setText("Saldo: " + account.getBalance());
System.out.println(label.getText());
}
}

View File

@@ -1,39 +1,28 @@
package investment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class InvestmentViewer1 {
private static final int FRAME_WIDTH = 120;
private static final int FRAME_HEIGHT = 60;
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BankAccount account = new BankAccount(INITIAL_BALANCE);
JButton button = new JButton("Aggiungi interessi");
frame.add(button);
class InterestListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
System.out.println("saldo: " + account.getBalance());
}
}
ActionListener listener = new InterestListener();
button.addActionListener(listener);
frame.setVisible(true);
}
}
package investment;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class InvestmentViewer1 {
private static final int FRAME_WIDTH = 120;
private static final int FRAME_HEIGHT = 60;
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Aggiungi interessi");
frame.add(button);
ActionListener listener = new InterestListener1(INTEREST_RATE);
button.addActionListener(listener);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,32 @@
package investment;
import javax.swing.JButton;
import javax.swing.JFrame;
public class InvestmentViewer1Lambda {
private static final int FRAME_WIDTH = 120;
private static final int FRAME_HEIGHT = 60;
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BankAccount account = new BankAccount(INITIAL_BALANCE);
JButton button = new JButton("Aggiungi interessi");
frame.add(button);
button.addActionListener(event -> {
double interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
System.out.println("Saldo: " + account.getBalance());
});
frame.setVisible(true);
}
}

View File

@@ -1,46 +1,38 @@
package investment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class InvestmentViewer2 {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 100;
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BankAccount account = new BankAccount(INITIAL_BALANCE);
JButton button = new JButton("Aggiungi interessi");
JLabel label = new JLabel("Saldo: " + account.getBalance());
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);
class InterestListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
label.setText("Saldo: " + account.getBalance());
}
}
ActionListener listener = new InterestListener();
button.addActionListener(listener);
frame.setVisible(true);
}
}
package investment;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class InvestmentViewer2 {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 100;
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BankAccount account = new BankAccount(INITIAL_BALANCE);
JButton button = new JButton("Aggiungi interessi");
JLabel label = new JLabel("Saldo: " + account.getBalance());
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);
ActionListener listener = new InterestListener2(INTEREST_RATE, label);
button.addActionListener(listener);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,44 @@
package investment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class InvestmentViewer2Lambda {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 100;
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BankAccount account = new BankAccount(INITIAL_BALANCE);
JButton button = new JButton("Aggiungi interessi");
JLabel label = new JLabel("Saldo: " + account.getBalance());
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);
ActionListener listener = new InterestListener2(INTEREST_RATE, label);
button.addActionListener(event -> {
double interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
label.setText("Saldo: " + account.getBalance());
System.out.println(label.getText());
});
frame.setVisible(true);
}
}

View File

@@ -1,53 +1,53 @@
package shape;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
/**
*
* @author radaelli11353
*/
public class Circle extends Shape {
private double r;
public Circle(double xc, double yc, double r) {
super(xc-r, yc-r);
this.r = r;
}
public void setRadius(double r) {
this.r = r;
}
@Override
public double width() {
return r*2;
}
@Override
public double height() {
return r*2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + (int) (Double.doubleToLongBits(this.r) ^ (Double.doubleToLongBits(this.r) >>> 32));
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Circle c = (Circle) o;
return r == c.r;
}
@Override
public void draw(Graphics2D g2) {
Ellipse2D.Double e = new Ellipse2D.Double(posX(), posY(), width(), height());
g2.draw(e);
}
}
package shape;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
/**
*
* @author radaelli11353
*/
public class Circle extends Shape {
private double r;
public Circle(double xc, double yc, double r) {
super(xc-r, yc-r);
this.r = r;
}
public void setRadius(double r) {
this.r = r;
}
@Override
public double width() {
return r*2;
}
@Override
public double height() {
return r*2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + (int) (Double.doubleToLongBits(this.r) ^ (Double.doubleToLongBits(this.r) >>> 32));
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Circle c = (Circle) o;
return r == c.r;
}
@Override
public void draw(Graphics2D g2) {
Ellipse2D.Double e = new Ellipse2D.Double(posX(), posY(), width(), height());
g2.draw(e);
}
}

View File

@@ -1,47 +1,47 @@
package shape;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Graphics2D;
public class Geometria {
private ArrayList<Shape> elementi;
public Geometria() {
elementi = new ArrayList<>();
}
public void riempiACaso(int size) {
Random r = new Random();
for(int i = 0; i < size; i++) {
int s = r.nextInt(3);
double x = r.nextDouble()*300;
double y = r.nextDouble()*300;
switch(s) {
case 0:
//circle
double raggio = r.nextDouble()*30;
elementi.add(new Circle(x, y, raggio));
break;
case 1:
//square
double lato = r.nextDouble()*40;
elementi.add(new Square(x, y, lato));
break;
case 2:
//triangolo
double base = r.nextDouble()*30;
double altezza = r.nextDouble()*30;
elementi.add(new TriangoloRettangolo(x, y, base, altezza));
break;
}
}
}
public void draw(Graphics2D g2) {
for(Shape s : elementi) s.draw(g2);
}
}
package shape;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Graphics2D;
public class Geometria {
private ArrayList<Shape> elementi;
public Geometria() {
elementi = new ArrayList<>();
}
public void riempiACaso(int size) {
Random r = new Random();
for(int i = 0; i < size; i++) {
int s = r.nextInt(3);
double x = r.nextDouble()*300;
double y = r.nextDouble()*300;
switch(s) {
case 0:
//circle
double raggio = r.nextDouble()*30;
elementi.add(new Circle(x, y, raggio));
break;
case 1:
//square
double lato = r.nextDouble()*40;
elementi.add(new Square(x, y, lato));
break;
case 2:
//triangolo
double base = r.nextDouble()*30;
double altezza = r.nextDouble()*30;
elementi.add(new TriangoloRettangolo(x, y, base, altezza));
break;
}
}
}
public void draw(Graphics2D g2) {
for(Shape s : elementi) s.draw(g2);
}
}

View File

@@ -1,32 +1,32 @@
package shape;
import java.awt.Graphics2D;
/**
*
* @author radaelli11353
*/
public abstract class Shape implements Comparable<Shape> {
private double x, y;
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
public abstract double width();
public abstract double height();
public double posX() {
return x;
}
public double posY() {
return y;
}
public abstract void draw(Graphics2D g2);
@Override
public int compareTo(Shape o) {
return Double.compare(width() * height(), o.width() * o.height());
}
}
package shape;
import java.awt.Graphics2D;
/**
*
* @author radaelli11353
*/
public abstract class Shape implements Comparable<Shape> {
private double x, y;
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
public abstract double width();
public abstract double height();
public double posX() {
return x;
}
public double posY() {
return y;
}
public abstract void draw(Graphics2D g2);
@Override
public int compareTo(Shape o) {
return Double.compare(width() * height(), o.width() * o.height());
}
}

View File

@@ -1,22 +1,22 @@
package shape;
import javax.swing.JFrame;
/**
*
* @author radaelli11353
*/
public class ShapesViewer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("Circles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tester component = new Tester();
frame.add(component);
frame.setVisible(true);
}
}
package shape;
import javax.swing.JFrame;
/**
*
* @author radaelli11353
*/
public class ShapesViewer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("Circles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tester component = new Tester();
frame.add(component);
frame.setVisible(true);
}
}

View File

@@ -1,50 +1,50 @@
package shape;
import java.awt.Graphics2D;
import java.awt.Rectangle;
/**
*
* @author radaelli11353
*/
public class Square extends Shape {
private double l;
public Square(double x, double y, double l) {
super(x, y);
this.l = l;
}
@Override
public double width() {
return l;
}
@Override
public double height() {
return l;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + (int) (Double.doubleToLongBits(this.l) ^ (Double.doubleToLongBits(this.l) >>> 32));
hash = 97 * hash + super.hashCode();
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Square s = (Square) o;
return l == s.l;
}
@Override
public void draw(Graphics2D g2) {
Rectangle r = new Rectangle((int)posX(), (int)posY(), (int)width(), (int)height());
g2.draw(r);
}
}
package shape;
import java.awt.Graphics2D;
import java.awt.Rectangle;
/**
*
* @author radaelli11353
*/
public class Square extends Shape {
private double l;
public Square(double x, double y, double l) {
super(x, y);
this.l = l;
}
@Override
public double width() {
return l;
}
@Override
public double height() {
return l;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + (int) (Double.doubleToLongBits(this.l) ^ (Double.doubleToLongBits(this.l) >>> 32));
hash = 97 * hash + super.hashCode();
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Square s = (Square) o;
return l == s.l;
}
@Override
public void draw(Graphics2D g2) {
Rectangle r = new Rectangle((int)posX(), (int)posY(), (int)width(), (int)height());
g2.draw(r);
}
}

View File

@@ -1,21 +1,21 @@
package shape;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
*
* @author radaelli11353
*/
public class Tester extends JComponent{
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Geometria geom = new Geometria();
geom.riempiACaso(10);
geom.draw(g2);
}
}
package shape;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
*
* @author radaelli11353
*/
public class Tester extends JComponent{
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Geometria geom = new Geometria();
geom.riempiACaso(10);
geom.draw(g2);
}
}

View File

@@ -1,62 +1,62 @@
package shape;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
/**
*
* @author radaelli11353
*/
public class TriangoloRettangolo extends Shape {
private double b;
private double h;
public TriangoloRettangolo(double x, double y, double b, double h) {
super(x, y);
this.b = b;
this.h = h;
}
@Override
public double width() {
return b;
}
@Override
public double height() {
return h;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (int) (Double.doubleToLongBits(this.b) ^ (Double.doubleToLongBits(this.b) >>> 32));
hash = 53 * hash + (int) (Double.doubleToLongBits(this.h) ^ (Double.doubleToLongBits(this.h) >>> 32));
hash = 53 * hash + super.hashCode();
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
TriangoloRettangolo t = (TriangoloRettangolo) o;
if (Double.doubleToLongBits(b) != Double.doubleToLongBits(t.b)) {
return false;
}
return Double.doubleToLongBits(h) != Double.doubleToLongBits(t.h);
}
@Override
public void draw(Graphics2D g2) {
Line2D.Double altezza = new Line2D.Double(posX(), posY(), posX(), posY()+height());
Line2D.Double base = new Line2D.Double(posX(), posY()+height(), posX()+width(), posY()+height());
Line2D.Double ipotenusa = new Line2D.Double(posX(), posY(), posX()+width(), posY()+height());
g2.draw(altezza);
g2.draw(base);
g2.draw(ipotenusa);
}
}
package shape;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
/**
*
* @author radaelli11353
*/
public class TriangoloRettangolo extends Shape {
private double b;
private double h;
public TriangoloRettangolo(double x, double y, double b, double h) {
super(x, y);
this.b = b;
this.h = h;
}
@Override
public double width() {
return b;
}
@Override
public double height() {
return h;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (int) (Double.doubleToLongBits(this.b) ^ (Double.doubleToLongBits(this.b) >>> 32));
hash = 53 * hash + (int) (Double.doubleToLongBits(this.h) ^ (Double.doubleToLongBits(this.h) >>> 32));
hash = 53 * hash + super.hashCode();
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
TriangoloRettangolo t = (TriangoloRettangolo) o;
if (Double.doubleToLongBits(b) != Double.doubleToLongBits(t.b)) {
return false;
}
return Double.doubleToLongBits(h) != Double.doubleToLongBits(t.h);
}
@Override
public void draw(Graphics2D g2) {
Line2D.Double altezza = new Line2D.Double(posX(), posY(), posX(), posY()+height());
Line2D.Double base = new Line2D.Double(posX(), posY()+height(), posX()+width(), posY()+height());
Line2D.Double ipotenusa = new Line2D.Double(posX(), posY(), posX()+width(), posY()+height());
g2.draw(altezza);
g2.draw(base);
g2.draw(ipotenusa);
}
}

View File

@@ -0,0 +1,33 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.DateViewer;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Date;
import javax.swing.JComponent;
/**
*
* @author radaelli11353
*/
public class DateComponent extends JComponent {
private Date now;
public DateComponent() {
now = new Date();
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
recalculateDate();
g2.drawString(now.toString(), 100, 100);
}
public void recalculateDate() {
now = new Date();
}
}

View File

@@ -0,0 +1,33 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.DateViewer;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
*
* @author radaelli11353
*/
public class DateFrame extends JFrame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
private DateComponent scene;
public DateFrame() {
scene = new DateComponent();
add(scene);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
final int DELAY = 100;
Timer t = new Timer(DELAY, e -> {
scene.repaint();
});
t.start();
}
}

View File

@@ -0,0 +1,22 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.DateViewer;
import javax.swing.JFrame;
/**
*
* @author radaelli11353
*/
public class DateViewer {
public static void main(String[] args) {
JFrame frame = new DateFrame();
frame.setTitle("Date and Hour");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,41 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.MovingRectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
/**
*
* @author radaelli11353
*/
public class RectangleComponent extends JComponent{
private static final int BOX_X = 100;
private static final int BOX_Y = 100;
private static final int BOX_WIDTH = 20;
private static final int BOX_HEIGHT = 30;
private Rectangle box;
public RectangleComponent() {
box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(box);
}
public void moveRectangleBy(int dx, int dy) {
box.translate(dx, dy);
repaint();
}
}

View File

@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.MovingRectangle;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
*
* @author radaelli11353
*/
public class RectangleFrame extends JFrame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
private RectangleComponent scene;
public RectangleFrame() {
scene = new RectangleComponent();
add(scene);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
final int DELAY = 100;
Timer t = new Timer(DELAY, e -> {
scene.moveRectangleBy(1, 1);
});
t.start();
}
}

View File

@@ -0,0 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.MovingRectangle;
import timer.MovingRectangleBorders.RectangleFrame;
import javax.swing.JFrame;
/**
*
* @author radaelli11353
*/
public class RectangleViewer {
public static void main(String[] args) {
JFrame frame = new RectangleFrame();
frame.setTitle("An animated rectangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,47 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.MovingRectangleBorders;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
/**
*
* @author radaelli11353
*/
public class RectangleComponent extends JComponent{
private static final int BOX_X = 100;
private static final int BOX_Y = 100;
private static final int BOX_WIDTH = 20;
private static final int BOX_HEIGHT = 30;
private Rectangle box;
private int xMov = 1;
private int yMov = 1;
public RectangleComponent() {
box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(box);
}
public void moveRectangle() {
double x = box.getX();
double y = box.getY();
xMov = (x + box.getWidth() < getWidth()) ? xMov : -1;
yMov = (y + box.getHeight() < getHeight()) ? yMov : -1;
xMov = (x >= 0) ? xMov : 1;
yMov = (y >= 0) ? yMov : 1;
box.translate(xMov, yMov);
repaint();
}
}

View File

@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.MovingRectangleBorders;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
*
* @author radaelli11353
*/
public class RectangleFrame extends JFrame {
public static final int FRAME_WIDTH = 300;
public static final int FRAME_HEIGHT = 400;
private RectangleComponent scene;
public RectangleFrame() {
scene = new RectangleComponent();
add(scene);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
final int DELAY = 100;
Timer t = new Timer(DELAY, e -> {
scene.moveRectangle();
});
t.start();
}
}

View File

@@ -0,0 +1,22 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timer.MovingRectangleBorders;
import javax.swing.JFrame;
/**
*
* @author radaelli11353
*/
public class RectangleViewer {
public static void main(String[] args) {
JFrame frame = new RectangleFrame();
frame.setTitle("An animated rectangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}