package framepaneltest;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class MyEmptyPanel extends JPanel {
private static final int WIDTH=400;
private static final int HEIGHT=300;
private static final long serialVersionUID = 1L;
public MyEmptyPanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.yellow);
}
}
package framepaneltest;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyFramePanel extends JFrame implements ActionListener {
private JButton fine;
private MyEmptyPanel disegno;
private static final long serialVersionUID = 1L;
public MyFramePanel () {
setTitle("Finestra con area con disegno");
setLayout(new BorderLayout());
fine = new JButton("Fine");
disegno = new MyEmptyPanel();
fine.addActionListener(this);
add(disegno, BorderLayout.CENTER);
add(fine, BorderLayout.SOUTH);
pack();
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == fine) {
dispose();
}
}
}
package frameformetest;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MyPanelForme extends JPanel {
private static final int WIDTH=400;
private static final int HEIGHT=300;
private static final int NUMFIORME=7;
private static final long serialVersionUID = 1L;
private int forma;
public MyPanelForme() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(new Color(0xff, 0xff, 0xcc));
forma = 0;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
switch(forma) {
case 0:
rettangoloVuoto(g);
break;
case 1:
rettangoloPieno(g);
break;
case 2:
tortaVuota(g);
break;
case 3:
tortaPiena(g);
break;
case 4:
poligonoVuoto(g);
break;
case 5:
poligonoPieno(g);
break;
case 6:
testo(g);
break;
}
}
private void testo(Graphics g) {
g.drawString("Testo", WIDTH/2, HEIGHT/2);
}
private void poligonoPieno(Graphics g) {
int[] xPoints = {WIDTH/3, 2*WIDTH/3,WIDTH/3 };
int[] yPoints = {HEIGHT/3, 2*HEIGHT/3, 2*HEIGHT/3 };
g.fillPolygon(xPoints, yPoints,3);
}
private void poligonoVuoto(Graphics g) {
int[] xPoints = {WIDTH/3, 2*WIDTH/3,WIDTH/3 };
int[] yPoints = {HEIGHT/3, 2*HEIGHT/3, 2*HEIGHT/3 };
g.drawPolygon(xPoints, yPoints,3);
}
private void tortaPiena(Graphics g) {
g.fillArc(WIDTH/3, HEIGHT/3, WIDTH/3, HEIGHT/3, 0, 270);
}
private void tortaVuota(Graphics g) {
g.drawArc(WIDTH/3, HEIGHT/3, WIDTH/3, HEIGHT/3, 0, 270);
}
private void rettangoloPieno(Graphics g) {
g.fillRect(WIDTH/3, HEIGHT/3, WIDTH/3, HEIGHT/3);
}
private void rettangoloVuoto(Graphics g) {
g.drawRect(WIDTH/3, HEIGHT/3, WIDTH/3, HEIGHT/3);
}
public void eseguiAzione1() {
if (forma < NUMFIORME - 1) {
forma++;
} else {
forma = 0;
}
repaint();
}
public void eseguiAzione2() {
if (forma > 0) {
forma--;
} else {
forma = NUMFIORME - 1;
}
repaint();
}
}
package frameformetest;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrameForme extends JFrame implements ActionListener {
private JButton fine;
private JButton b1;
private JButton b2;
private MyPanelForme disegno;
private static final long serialVersionUID = 1L;
public MyFrameForme () {
setTitle("Finestra con area con disegno");
setLayout(new BorderLayout());
fine = new JButton("Fine");
b1 = new JButton("Prossima");
b2 = new JButton("Precedente");
disegno = new MyPanelForme();
fine.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
add(disegno, BorderLayout.CENTER);
JPanel bottoni = new JPanel();
bottoni.setLayout(new FlowLayout());
bottoni.add(b1);
bottoni.add(b2);
bottoni.add(fine);
add(bottoni, BorderLayout.SOUTH);
pack();
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == b1) {
disegno.eseguiAzione1();
} else if (evt.getSource() == b2) {
disegno.eseguiAzione2();
} else if (evt.getSource() == fine) {
dispose();
}
}
}
package framelinetest;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MyPanelLine extends JPanel {
private static final int WIDTH=400;
private static final int HEIGHT=300;
private static final long serialVersionUID = 1L;
public MyPanelLine() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(new Color(0xff, 0xff, 0xcc));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(0, 0, WIDTH, HEIGHT);
g.setColor(Color.blue);
g.drawLine(WIDTH, 0, 0, HEIGHT);
}
}
package frameimmagine;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import javax.swing.JPanel;
public class MyPanelImmagine extends JPanel implements ImageObserver {
private static final int WIDTH=400;
private static final int HEIGHT=300;
private static final long serialVersionUID = 1L;
private Image immagine;
private int x;
private int y;
public MyPanelImmagine() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(new Color(0xff, 0xff, 0xcc));
x = WIDTH / 2;
y = HEIGHT / 2;
immagine = Toolkit.getDefaultToolkit().getImage("palla.png");
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(immagine, x - immagine.getWidth(this) / 2,
y - immagine.getHeight(this) / 2, this);
}
public void eseguiAzione1() {
if (x > immagine.getWidth(this) / 2 &&
y > immagine.getHeight(this) / 2) {
x--;
y--;
}
repaint();
}
public void eseguiAzione2() {
if (x < WIDTH - immagine.getWidth(this) / 2 &&
y < HEIGHT - immagine.getHeight(this) / 2) {
x++;
y++;
}
repaint();
}
@Override
public boolean imageUpdate(Image arg0, int infoFlags, int x, int y,
int width, int heignt) {
repaint();
return (infoFlags & ALLBITS) != ALLBITS;
}
}
package frameanimazione;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class MyPanelAnimazione extends JPanel implements Runnable {
private static final int WIDTH=400;
private static final int HEIGHT=300;
private static final long serialVersionUID = 1L;
private Image immagine;
private int x;
private int y;
private int dx;
private int dy;
private Thread animazione;
public MyPanelAnimazione() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(new Color(0xff, 0xff, 0xcc));
x = WIDTH / 2;
y = HEIGHT / 2;
dx = 1;
dy = 1;
immagine = Toolkit.getDefaultToolkit().getImage("palla.png");
animazione = new Thread(this);
animazione.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(immagine, x - immagine.getWidth(this) / 2,
y - immagine.getHeight(this) / 2, this);
}
public void stop() {
animazione = null;
}
public void eseguiAzione1() {
dx *= -1;
}
public void eseguiAzione2() {
dy *= -1;
}
@Override
public boolean imageUpdate(Image arg0, int infoFlags, int x, int y,
int width, int heignt) {
repaint();
return (infoFlags & ALLBITS) != ALLBITS;
}
@Override
public void run() {
while (Thread.currentThread() == animazione) {
x += dx;
y += dy;
if (x < immagine.getWidth(this) / 2) {
dx = 1;
} else if (x > WIDTH - immagine.getWidth(this) / 2) {
dx = -1;
}
if (y < immagine.getHeight(this) / 2) {
dy = 1;
} else if (y > HEIGHT - immagine.getHeight(this) / 2) {
dy = -1;
}
repaint();
try {
Thread.sleep(20);
} catch (Exception e) {
}
}
}
}