Posted by : Unknown
Friday, 19 June 2015
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ToolBarDemo extends JPanel
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ToolBarDemo extends JPanel
{
public static void main(String[] args)
public static void main(String[] args)
{
JFrame window = new JFrame("ToolBarDemo");
ToolBarDemo content = new ToolBarDemo();
window.setContentPane(content);
window.pack();
window.setResizable(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private class Display extends JPanel implements MouseListener, MouseMotionListener
JFrame window = new JFrame("ToolBarDemo");
ToolBarDemo content = new ToolBarDemo();
window.setContentPane(content);
window.pack();
window.setResizable(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private class Display extends JPanel implements MouseListener, MouseMotionListener
{
private BufferedImage OSC; // Off-screen canvas.
private Color currentColor = Color.RED; // Current drawing color.
private int prevX, prevY; // Previous mouse position, during mouse drags.
private BasicStroke stroke; // Stroke used for drawing.
Display() { // constructor.
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(300,300));
stroke = new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
}
void setCurrentColor(Color c) { // change current drawing color
currentColor = c;
}
void clear() { // clear the drawing area by filling it with white
if (OSC != null) {
Graphics g = OSC.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.dispose();
repaint();
}
}
public void paintComponent(Graphics g) { // just copies OSC to screen
checkImage();
g.drawImage(OSC,0,0,null);
}
void checkImage() { // create or resize OSC if necessary
if (OSC == null) {
// Create the OSC, with a size to match the size of the panel.
OSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
clear();
}
else if (OSC.getWidth() != getWidth() || OSC.getHeight() != getHeight()) {
BufferedImage newOSC;
newOSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = newOSC.getGraphics();
g.drawImage(OSC,0,0,getWidth(),getHeight(),null);
g.dispose();
OSC = newOSC;
}
}
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
}
public void mouseDragged(MouseEvent e) {
Graphics2D g2 = (Graphics2D)OSC.getGraphics();
g2.setColor(currentColor);
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(prevX,prevY,e.getX(),e.getY());
g2.dispose();
repaint();
prevX = e.getX();
prevY = e.getY();
}
public void mouseReleased(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
private Display display;
private BufferedImage OSC; // Off-screen canvas.
private Color currentColor = Color.RED; // Current drawing color.
private int prevX, prevY; // Previous mouse position, during mouse drags.
private BasicStroke stroke; // Stroke used for drawing.
Display() { // constructor.
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(300,300));
stroke = new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
}
void setCurrentColor(Color c) { // change current drawing color
currentColor = c;
}
void clear() { // clear the drawing area by filling it with white
if (OSC != null) {
Graphics g = OSC.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.dispose();
repaint();
}
}
public void paintComponent(Graphics g) { // just copies OSC to screen
checkImage();
g.drawImage(OSC,0,0,null);
}
void checkImage() { // create or resize OSC if necessary
if (OSC == null) {
// Create the OSC, with a size to match the size of the panel.
OSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
clear();
}
else if (OSC.getWidth() != getWidth() || OSC.getHeight() != getHeight()) {
BufferedImage newOSC;
newOSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = newOSC.getGraphics();
g.drawImage(OSC,0,0,getWidth(),getHeight(),null);
g.dispose();
OSC = newOSC;
}
}
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
}
public void mouseDragged(MouseEvent e) {
Graphics2D g2 = (Graphics2D)OSC.getGraphics();
g2.setColor(currentColor);
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(prevX,prevY,e.getX(),e.getY());
g2.dispose();
repaint();
prevX = e.getX();
prevY = e.getY();
}
public void mouseReleased(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
private Display display;
public ToolBarDemo() {
setLayout(new BorderLayout(2,2));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
display = new Display();
add(display, BorderLayout.CENTER);
JToolBar toolbar = new JToolBar();
add(toolbar, BorderLayout.NORTH);
ButtonGroup group = new ButtonGroup();
toolbar.add( makeColorRadioButton(Color.RED,group,true) );
toolbar.add( makeColorRadioButton(Color.GREEN,group,false) );
toolbar.add( makeColorRadioButton(Color.BLUE,group,false) );
toolbar.addSeparator(new Dimension(20,20));
toolbar.add( makeClearButton() );
}
private JRadioButton makeColorRadioButton(final Color c, ButtonGroup grp, boolean selected) {
BufferedImage image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(1, 1, 24, 24, true);
g.dispose();
Icon unselectedIcon = new ImageIcon(image);
/* Create an ImageIcon for the selected state of the button. */
image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(3, 3, 24, 24, false);
g.dispose();
Icon selectedIcon = new ImageIcon(image);
/* Create and configure the button. */
JRadioButton button = new JRadioButton(unselectedIcon);
button.setSelectedIcon(selectedIcon);
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// The action for this button sets the current drawing color
// in the display to c.
display.setCurrentColor(c);
}
});
grp.add(button);
if (selected)
button.setSelected(true);
return button;
} // end makeColorRadioButton
private JButton makeClearButton() {
BufferedImage image = new BufferedImage(24,24,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.LIGHT_GRAY);
g2.fillRect(0,0,24,24);
g2.setStroke( new BasicStroke(3));
g2.setColor(Color.BLACK);
g2.drawLine(4,4,20,20);
g2.drawLine(4,20,20,4);
g2.dispose();
Icon clearIcon = new ImageIcon(image);
Action clearAction = new AbstractAction(null,clearIcon) {
public void actionPerformed(ActionEvent evt) {
display.clear();
}
};
clearAction.putValue(Action.SHORT_DESCRIPTION, "Clear the Display");
JButton button = new JButton(clearAction);
return button;
}
}
setLayout(new BorderLayout(2,2));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
display = new Display();
add(display, BorderLayout.CENTER);
JToolBar toolbar = new JToolBar();
add(toolbar, BorderLayout.NORTH);
ButtonGroup group = new ButtonGroup();
toolbar.add( makeColorRadioButton(Color.RED,group,true) );
toolbar.add( makeColorRadioButton(Color.GREEN,group,false) );
toolbar.add( makeColorRadioButton(Color.BLUE,group,false) );
toolbar.addSeparator(new Dimension(20,20));
toolbar.add( makeClearButton() );
}
private JRadioButton makeColorRadioButton(final Color c, ButtonGroup grp, boolean selected) {
BufferedImage image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(1, 1, 24, 24, true);
g.dispose();
Icon unselectedIcon = new ImageIcon(image);
/* Create an ImageIcon for the selected state of the button. */
image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(3, 3, 24, 24, false);
g.dispose();
Icon selectedIcon = new ImageIcon(image);
/* Create and configure the button. */
JRadioButton button = new JRadioButton(unselectedIcon);
button.setSelectedIcon(selectedIcon);
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// The action for this button sets the current drawing color
// in the display to c.
display.setCurrentColor(c);
}
});
grp.add(button);
if (selected)
button.setSelected(true);
return button;
} // end makeColorRadioButton
private JButton makeClearButton() {
BufferedImage image = new BufferedImage(24,24,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.LIGHT_GRAY);
g2.fillRect(0,0,24,24);
g2.setStroke( new BasicStroke(3));
g2.setColor(Color.BLACK);
g2.drawLine(4,4,20,20);
g2.drawLine(4,20,20,4);
g2.dispose();
Icon clearIcon = new ImageIcon(image);
Action clearAction = new AbstractAction(null,clearIcon) {
public void actionPerformed(ActionEvent evt) {
display.clear();
}
};
clearAction.putValue(Action.SHORT_DESCRIPTION, "Clear the Display");
JButton button = new JButton(clearAction);
return button;
}
}
Output:
Related Posts :
- Back to Home »
- Simple Application Program »
- Paint with Color Brush Using ToolBar