JAVA HOMEWORK Produce a GUI drawing application. It should should function as fo
ID: 3697217 • Letter: J
Question
JAVA HOMEWORK
Produce a GUI drawing application.
It should should function as follows:
The user can select from three different pen colors.
The user can also select an "eraser" pen.
Hint: Do you really need to "erase" a point? Or is there a shortcut you can use?
When the user clicks in the drawing space, the pen is activated. The user can then move the mouse to draw. The user clicks again to de-activate the pen.
Note: the user does not click-and-drag to draw. Instead, it's: a) click once to turn on, b) move to draw, c) click once to turn off.
Make sure you test that your program works when you change the color of the pen!
There is a clear button that clears the entire drawing space.
You need to keep track of both the location and color of a point which the user has drawn.To do this, use an ArrayList.
What kind of object should the ArrayList hold? Does Java provide a class that represents a Point with Color? If not, what can you do?
For full credit, design an object-oriented solution to keep track of an object with a location and a color.
For full credit, do not use double-buffering or the BasicStroke class. Instead, use the approach of keeping track of points instead. (We don't cover double-buffering or BasicStroke in our course, so if you don't
Include an additional drawing functionality. This must be different from simply choosing another pen color- that will not count for any extra credit. Be creative!!
Explanation / Answer
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Paint{ public static void main(String[] args){ gui g = new gui(); g.setVisible(true); } } public class gui extends JComponent implements ActionListener{ JButton red, green, blue, clear; Image image; Graphics2D draw; int x, y, prevX, prevY; gui(){ JFrame frame = new JFrame("Paint"); Container content = frame.getContentPane(); content.setLayout(new BorderLayout()); setDoubleBuffered(false); JPanel panel = new JPanel(); content.add(panel, BorderLayout.SOUTH); panel.setPreferredSize(new Dimension(32, 68)); panel.setMinimumSize(new Dimension(32, 68)); panel.setMaximumSize(new Dimension(32, 68)); red = new JButton("Red"); green = new JButton("Green"); blue = new JButton("Blue"); clear = new JButton("Clear"); red.setPreferredSize(new Dimension(50, 16)); green.setPreferredSize(new Dimension(50,16)); blue.setPreferredSize(new Dimension(50, 16)); panel.add(red); panel.add(green); panel.add(blue); panel.add(clear); red.addActionListener(this); green.addActionListener(this); blue.addActionListener(this); clear.addActionListener(this); frame.setSize(500, 500); addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ prevX = e.getX(); prevY = e.getY(); } }); addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ x = e.getX(); y = e.getY(); draw.drawLine(prevX, prevY, x, y); repaint(); prevX = x; prevY = y; } }); } public void paintComponent(Graphics g){ if(image==null){ image = createImage(getSize().width, getSize().height); draw = (Graphics2D)image.getGraphics(); draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); draw.setPaint(Color.white); draw.fillRect(0, 0, getSize().width, getSize().height); draw.setPaint(Color.black); repaint(); } g.drawImage(image, 0, 0, null); } public void actionPerformed(ActionEvent e) { if( e.getSource()==red){ draw.setPaint(Color.red); repaint(); } if( e.getSource()==green){ draw.setPaint(Color.green); repaint(); } if( e.getSource()==blue){ draw.setPaint(Color.blue); repaint(); } if( e.getSource()==clear){ draw.setPaint(Color.white); draw.fillRect(0, 0, getSize().width, getSize().height); draw.setPaint(Color.black); repaint(); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.