Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- public class ChoiseColor extends Component {
- protected ActionListener actionListener;
- protected Color color = Color.white;
- protected boolean brush = false;
- protected ArrayList<Info> xy = new ArrayList<>();
- public ChoiseColor() {
- enableEvents(AWTEvent.MOUSE_EVENT_MASK);
- enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
- }
- public void paint(Graphics g) {
- int width = getSize().width - 1;
- int height = getSize().height - 1;
- g.setColor(Color.black);
- g.fillRect(0,0,width-1,height-1);
- for(Info info : xy){
- g.setColor(info.getColor());
- if(!brush) {
- g.fillOval(info.getX() - 5, info.getY() - 5, 10, 10);
- }else{
- g.fillRect(info.getX() - 5, info.getY() - 5, 10, 10);
- }
- }
- }
- public void clear(){
- xy.clear();
- repaint();
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(300 , 300);
- }
- public Dimension getMinimumSize() {
- return getPreferredSize();
- }
- public void setColor(Color color) {
- this.color = color;
- }
- public void setBrush(){
- brush = !brush;
- }
- @Override
- protected void processMouseMotionEvent(MouseEvent e) {
- switch (e.getID()) {
- case MouseEvent.MOUSE_DRAGGED:
- xy.add(new Info(e.getX() , e.getY() , color));
- repaint();
- break;
- }
- super.processMouseMotionEvent(e);
- }
- public void addActionListener(ActionListener listener) {
- actionListener = AWTEventMulticaster.add(actionListener, listener);
- enableEvents(AWTEvent.MOUSE_EVENT_MASK);
- enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
- }
- public void removeActionListener(ActionListener listener) {
- actionListener = AWTEventMulticaster.remove(actionListener, listener);
- }
- class Info{
- private Color color;
- private int x;
- private int y;
- Info(int x , int y , Color color){
- this.x = x;
- this.y = y;
- this.color = color;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public Color getColor() {
- return color;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement