Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

276
LINES

< > BotCompany Repo | #1034113 // Simple Drawing Program by Luis Juarez

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (5282L/30K).

1  
//from https://gist.github.com/IAmLuisJ/5985afed66e5b62601a6a73eb8287876
2  
3  
/**
4  
 *  * Course CIS 117
5  
 * Author: Luis Juarez
6  
 * Title: Drawing Program
7  
 * Date: 4/10/2016
8  
 * Purpose: GUI drawing program
9  
 */
10  
11  
p {
12  
    JFrame frame = new DrawingFrame();
13  
    frame.setTitle("Drawing Program");
14  
    frame.setSize(700, 600);
15  
    frame.setLocationRelativeTo(null);
16  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17  
    frame.setVisible(true);
18  
}
19  
20  
sclass Circle
21  
{
22  
    private int size;
23  
    private Point center;
24  
    private Color color;
25  
26  
    Circle(int iSize, Point location, Color C)
27  
    {
28  
        setSize(iSize);
29  
        setLocation(location);
30  
        setColor(C);
31  
    }
32  
33  
    void setSize(int iSize) {
34  
        if (iSize > 1) {
35  
            size = iSize;
36  
        } else {
37  
            size = 1;
38  
        }
39  
    }
40  
41  
    void setLocation(Point Pcenter) {
42  
        center = Pcenter;
43  
    }
44  
45  
    void setColor(Color Ccolor) {
46  
        color = Ccolor;
47  
    }
48  
49  
    int getSize()
50  
    {
51  
        return size;
52  
    }
53  
54  
    Point getCenter()
55  
    {
56  
        return center;
57  
    }
58  
59  
    Color getColor()
60  
    {
61  
        return color;
62  
    }
63  
64  
65  
    public void draw(Graphics g)
66  
    {
67  
        g.setColor(getColor());
68  
        g.fillOval(getCenter().x,getCenter().y,getSize(),getSize());
69  
    }
70  
}
71  
72  
sclass DrawingPanel extends JPanel implements MouseMotionListener
73  
{
74  
    private int circleDiameter;
75  
    private Color circleColor;
76  
    private Circle drawingCircle;
77  
    private ArrayList <Circle> circleArrayList = new ArrayList<Circle>();
78  
79  
80  
    DrawingPanel(Color co, int si)
81  
    {
82  
        setCircleColor(co);
83  
       setCircleDiameter(si);
84  
85  
        addMouseMotionListener(this);
86  
    }
87  
88  
    public void paintComponent(Graphics g)
89  
    {
90  
        super.paintComponent(g);
91  
        Iterator<Circle> circleIterator = circleArrayList.iterator();
92  
93  
        Circle drawCircle;
94  
95  
        while( circleIterator.hasNext() )
96  
        {
97  
            drawCircle = (Circle) circleIterator.next();
98  
            drawCircle.draw(g);
99  
        }
100  
    }
101  
102  
    void setCircleDiameter(int tempSize)
103  
    {
104  
        circleDiameter = tempSize;
105  
    }
106  
107  
    void setCircleColor(Color tempColor)
108  
    {
109  
        circleColor = tempColor;
110  
    }
111  
112  
    int getCircleSize()
113  
    {
114  
        return circleDiameter;
115  
    }
116  
117  
    Color getCircleColor()
118  
    {
119  
        return circleColor;
120  
    }
121  
122  
void clear() {
123  
    //clear panel
124  
    JOptionPane.showMessageDialog(null, "Please restart the program for a new panel");
125  
126  
}
127  
128  
    @Override
129  
    public void mouseDragged(MouseEvent e) {
130  
        if ( e.isMetaDown() )
131  
        {
132  
            Circle newCircle = new Circle( getCircleSize(), e.getPoint(),
133  
                    this.getBackground() );
134  
            circleArrayList.add( newCircle );
135  
        }
136  
        else {
137  
            Circle newCircle = new Circle( getCircleSize(), e.getPoint(),
138  
                    this.getCircleColor() );
139  
            circleArrayList.add( newCircle );
140  
        }
141  
142  
        repaint();
143  
    }
144  
145  
    @Override
146  
    public void mouseMoved(MouseEvent e) {
147  
148  
    }
149  
}
150  
151  
sclass DrawingFrame extends JFrame implements ActionListener
152  
{
153  
    final int SMALL = 4;
154  
    final int MEDIUM = 8;
155  
    final int LARGE = 10;
156  
157  
    private DrawingPanel drawPanel = new DrawingPanel(Color.black, SMALL);
158  
    Container con = getContentPane();
159  
    BorderLayout layout = new BorderLayout();
160  
    JMenuBar mainMenuBar = new JMenuBar();
161  
    JMenu menu1 = new JMenu("File");
162  
    JMenu menu2 = new JMenu("Size");
163  
    JMenu menu3 = new JMenu("Color");
164  
    JMenu menu4 = new JMenu("Help");
165  
    JMenuItem clear = new JMenuItem("Clear");
166  
    JMenuItem exit = new JMenuItem("Exit");
167  
    JMenuItem small = new JMenuItem("Small");
168  
    JMenuItem medium = new JMenuItem("Medium");
169  
    JMenuItem large = new JMenuItem("Large");
170  
    JMenuItem blackMenu = new JMenuItem("Black");
171  
    JMenuItem greenMenu = new JMenuItem("Green");
172  
    JMenuItem yellowMenu = new JMenuItem("Yellow");
173  
    JMenuItem redMenu = new JMenuItem("Red");
174  
    JMenuItem blueMenu = new JMenuItem("Blue");
175  
    JMenuItem about = new JMenuItem("About");
176  
177  
178  
179  
180  
181  
    DrawingFrame()
182  
    {
183  
        con.setLayout(layout);
184  
        drawPanel = new DrawingPanel( Color.BLACK, SMALL );
185  
        drawPanel.setBackground( Color.WHITE );
186  
        drawPanel.setVisible(true);
187  
        setJMenuBar(mainMenuBar);
188  
        mainMenuBar.add(menu1);
189  
        mainMenuBar.add(menu2);
190  
        mainMenuBar.add(menu3);
191  
        mainMenuBar.add(menu4);
192  
        menu1.add(clear);
193  
        menu1.add(exit);
194  
        menu2.add(small);
195  
        menu2.add(medium);
196  
        menu2.add(large);
197  
        menu3.add(blackMenu);
198  
        menu3.add(greenMenu);
199  
        menu3.add(yellowMenu);
200  
        menu3.add(redMenu);
201  
        menu3.add(blueMenu);
202  
        menu4.add(about);
203  
        menu1.setMnemonic('F');
204  
        menu2.setMnemonic('z');
205  
        menu3.setMnemonic('o');
206  
        menu4.setMnemonic('H');
207  
        clear.setMnemonic('C');
208  
        exit.setMnemonic('x');
209  
210  
        exit.addActionListener(this);
211  
        clear.addActionListener(this);
212  
        small.addActionListener(this);
213  
        medium.addActionListener(this);
214  
        large.addActionListener(this);
215  
        blackMenu.addActionListener(this);
216  
        greenMenu.addActionListener(this);
217  
        yellowMenu.addActionListener(this);
218  
        redMenu.addActionListener(this);
219  
        blueMenu.addActionListener(this);
220  
        about.addActionListener(this);
221  
        redMenu.addActionListener(this);
222  
        this.add(drawPanel);
223  
        drawPanel.setPreferredSize(new Dimension(600,600));
224  
225  
    }
226  
227  
228  
    @Override
229  
    public void actionPerformed(ActionEvent e) {
230  
       String arg = e.getActionCommand();
231  
        if(arg.equals("Exit") )
232  
        {
233  
            System.exit(0);
234  
        }
235  
        if(arg.equals("About") )
236  
        {
237  
            JOptionPane.showMessageDialog(null, "Drawing Program by Luis Juarez");
238  
        }
239  
        if(arg.equals("Clear"))
240  
        {
241  
           drawPanel.clear();
242  
        }
243  
        if(arg.equals("Red"))
244  
        {
245  
            drawPanel.setCircleColor(Color.red);
246  
        }
247  
        if(arg.equals("Black"))
248  
        {
249  
            drawPanel.setCircleColor(Color.black);
250  
        }
251  
        if(arg.equals("Yellow"))
252  
        {
253  
            drawPanel.setCircleColor(Color.yellow);
254  
        }
255  
        if(arg.equals("Green"))
256  
        {
257  
            drawPanel.setCircleColor(Color.green);
258  
        }
259  
        if(arg.equals("Blue"))
260  
        {
261  
            drawPanel.setCircleColor(Color.blue);
262  
        }
263  
        if(arg.equals("Small"))
264  
        {
265  
            drawPanel.setCircleDiameter(SMALL);
266  
        }
267  
        if(arg.equals("Medium"))
268  
        {
269  
            drawPanel.setCircleDiameter(MEDIUM);
270  
        }
271  
        if(arg.equals("Large"))
272  
        {
273  
            drawPanel.setCircleDiameter(LARGE);
274  
        }
275  
    }
276  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034113
Snippet name: Simple Drawing Program by Luis Juarez
Eternal ID of this version: #1034113/2
Text MD5: 85bb64359ced5d8d0b904da7a9bea2ff
Transpilation MD5: 52cf71cb708d83da722523c8a35ce58c
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-22 17:55:58
Source code size: 6937 bytes / 276 lines
Pitched / IR pitched: No / No
Views / Downloads: 68 / 397
Version history: 1 change(s)
Referenced in: [show references]