// (by Dalubar)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
interface misure {
int SMALL = 1, MEDIUM = 2, LARGE=3;
int QUADRATINO = 1, CERCHIETTO = 2;
}
class myFrame extends JFrame {
Dimension dScr = Toolkit.getDefaultToolkit().getScreenSize();
myPanel mainPanel;
public myFrame() {
setSize(dScr.width/2, dScr.height/2);
setLocation((dScr.width - getSize().width) /2, (dScr.height - getSize().height) /2);
setTitle("Un frame in compagnia di Marco...");
addWindowListener( new
WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
mainPanel = new myPanel();
getContentPane().add(mainPanel);
new menuprovaDM(this);
}
}
//pannello
class myPanel extends JPanel {
centro disegno;
JPanel fasciaSx, fasciaDx;
public myPanel() {
super();
setLayout(new BorderLayout());
fasciaSx = new JPanel();
fasciaDx = new JPanel();
add(fasciaSx, BorderLayout.WEST);
add(fasciaDx, BorderLayout.EAST);
disegno = new centro();
add(disegno, BorderLayout.CENTER);
fasciaSx.addMouseListener( new
MouseAdapter() {
public void mouseClicked(MouseEvent e) {
disegno.performedMoveToLeft();
}
}
);
fasciaDx.addMouseListener( new
MouseAdapter() {
public void mouseClicked(MouseEvent e) {
disegno.performedMoveToRight();
}
}
);
}
}
class centro extends JPanel implements misure {
//var. private di istanza
private int forma, size;
private Color colore;
private int horizzontalOffset;
private Dimension centro;
//costruttore
public centro() {
setBackground(Color.WHITE);
colore = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
size = MEDIUM;
forma = QUADRATINO;
horizzontalOffset = 0;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(colore);
centro = new Dimension((getSize().width / 2), getSize().height / 2);
int w = centro.width + horizzontalOffset;
if (forma == QUADRATINO) {
Rectangle2D rect = new Rectangle2D.Double();
rect.setFrameFromCenter(w, centro.height, w + getLato(), centro.height + getLato());
g2.fill(rect);
}
else {
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(w, centro.height,
w + getLato(), centro.height + getLato());
g2.fill(circle);
}
}
private int getLato() {
return centro.width / (5 - size);
}
// interfaccia pubblica
public void setForma(int v) {
forma = v;
repaint();
}
public void setColore(Color c) {
colore = c;
repaint();
}
public void setSize(int v) {
size = v;
repaint();
}
public void performedMoveToRight() {
if (centro.width + horizzontalOffset + 1 + getLato() > getSize().width) {
JOptionPane.showMessageDialog(null,
"Impossibile eseguire, la figura uscirebbe a Destra!",
"Informazione", JOptionPane.INFORMATION_MESSAGE);
}
else
horizzontalOffset++;
repaint();
}
public void performedMoveToLeft() {
if (centro.width + horizzontalOffset - 1 - getLato() < 0) {
JOptionPane.showMessageDialog(null,
"Impossibile eseguire, la figura uscirebbe a Sinistra!",
"Informazione", JOptionPane.INFORMATION_MESSAGE);
}
else
horizzontalOffset--;
repaint();
}
}
class menuprovaDM implements ActionListener, misure {
private JMenuItem forma_1, forma_2, colore_1, colore_2, size_1, size_2, size_3;
private myFrame frame;
public menuprovaDM(myFrame f) {
JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu forma = new JMenu("Forma");
JMenu colore = new JMenu("Colore");
JMenu size = new JMenu("Misura");
forma_1 = forma.add("Quadratino");
forma_2 = forma.add("Cerchietto");
colore_1 = colore.add("Rosso");
colore_2 = colore.add("Blu");
size_1 = size.add("Small");
size_2 = size.add("Medium");
size_3 = size.add("Large");
mb.add(forma); mb.add(colore); mb.add(size);
forma_1.addActionListener(this);
forma_2.addActionListener(this);
colore_1.addActionListener(this);
colore_2.addActionListener(this);
size_1.addActionListener(this);
size_2.addActionListener(this);
size_3.addActionListener(this);
frame = f;
}
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(forma_1)) frame.mainPanel.disegno.setForma(QUADRATINO);
if (event.getSource().equals(forma_2)) frame.mainPanel.disegno.setForma(CERCHIETTO);
if (event.getSource().equals(colore_1)) frame.mainPanel.disegno.setColore(Color.RED);
if (event.getSource().equals(colore_2)) frame.mainPanel.disegno.setColore(Color.BLUE);
if (event.getSource().equals(size_1)) frame.mainPanel.disegno.setSize(SMALL);
if (event.getSource().equals(size_2)) frame.mainPanel.disegno.setSize(MEDIUM);
if (event.getSource().equals(size_3)) frame.mainPanel.disegno.setSize(LARGE);
}
}
public class provaDM {
public static void main(String[] args) {
JFrame f = new myFrame();
f.show();
}
}