codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintTable extends JPanel
{
static JPanel topPanel = new JPanel();
static JButton annulla = new JButton("Annulla");
static JButton cancella = new JButton("Cancella");
static String[] colorNames = {"Nero","Blu","Verde","Giallo","Rosso","Bianco"};
static JComboBox colors = new JComboBox(colorNames);
static String[] typeNames = {"Linea retta","Rettangolo","Cerchio"};
static JComboBox types = new JComboBox(typeNames);
static JCheckBox rov = new JCheckBox("Figura piena");
private Color[] color = new Color[99999];
private Color[] colorList = {Color.BLACK,Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED,Color.WHITE};
private Point[] points1 = new Point[99999];
private Point[] points2 = new Point[99999];
private Point pointT;
private Point pointT2;
private boolean start = false;
private int countP = 0;
private byte[] type = new byte[99999];
private boolean[] filled = new boolean[99999];
static JPanel bottomPanel = new JPanel();
static JLabel label = new JLabel("Cursore del mouse fuori dal pannello");
public PaintTable()
{
setBackground(Color.WHITE);
for (int i=0;i<99999;i++)
{
filled[i] = false;
}
annulla.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
countP--;
points1[countP].x = 0;
points1[countP].y = 0;
points2[countP].x = 0;
points2[countP].y = 0;
pointT.x = 0;
pointT.y = 0;
pointT2.x = 0;
pointT2.y = 0;
repaint();
}
}
);
cancella.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
countP = 0;
pointT.x = 0;
pointT.y = 0;
pointT2.x = 0;
pointT2.y = 0;
for (int i=0;i<countP;i++)
{
points1[i].x = 0;
points1[i].y = 0;
points2[i].x = 0;
points2[i].y = 0;
}
repaint();
}
}
);
addMouseListener
(
new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
start = true;
points1[countP] = event.getPoint();
pointT = event.getPoint();
}
public void mouseReleased(MouseEvent event)
{
points2[countP] = event.getPoint();
repaint();
countP++;
}
public void mouseExited(MouseEvent event)
{
label.setText("Cursore del mouse fuori dal pannello");
}
}
);
addMouseMotionListener
(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
points2[countP] = event.getPoint();
pointT2 = event.getPoint();
for (int i=0;i<colorNames.length;i++)
{
if (colors.getSelectedIndex() == i)
{
color[countP] = colorList[i];
}
}
repaint();
}
public void mouseMoved(MouseEvent event)
{
label.setText(""+event.getX()+","+event.getY());
}
}
);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (types.getSelectedIndex() == 0)
{
type[countP] = 0;
}
else if (types.getSelectedIndex() == 1)
{
type[countP] = 1;
}
else
{
type[countP] = 2;
}
if (start)
{
g.setColor(color[countP]);
if (type[countP] == 0)
{
g.drawLine(pointT.x,pointT.y,pointT2.x,pointT2.y);
}
else if (type[countP] == 1)
{
g.drawRect(pointT.x,pointT.y,pointT2.x-pointT.x,pointT2.y-pointT.y);
}
else
{
g.drawOval(pointT.x,pointT.y,pointT2.x-pointT.x,pointT2.y-pointT.y);
}
filled[countP] = rov.isSelected() ? true : false;
for (int i=0;i<countP;i++)
{
g.setColor(color[i]);
if (type[i] == 0)
{
g.drawLine(points1[i].x,points1[i].y,points2[i].x,points2[i].y);
}
else if (type[i] == 1)
{
if (!filled[i])
{
g.drawRect(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
}
else
{
g.fillRect(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
}
}
else
{
if (!filled[i])
{
g.drawOval(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
}
else
{
g.fillOval(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
}
}
}
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Paint");
frame.setSize(640,480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PaintTable table = new PaintTable();
topPanel.setLayout(new FlowLayout());
topPanel.add(annulla);
topPanel.add(cancella);
colors.setMaximumRowCount(4);
topPanel.add(colors);
topPanel.add(types);
topPanel.add(rov);
frame.add(topPanel,BorderLayout.NORTH);
frame.add(table,BorderLayout.CENTER);
frame.add(label,BorderLayout.SOUTH);
frame.setVisible(true);
}
}