In: Computer Science
using Java Your mission in this exercise is to implement a very simple Java painting application.
Rapid Prototyping The JFrame is an example that can support the following functions:
1. Draw curves, specified by a mouse drag.
2. Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated).
3. Shape selection (line, rectangle or oval) selected by a combo box OR menu.
4. Color selection using radio buttons OR menu.
5. Line thickness using a combo box OR menu.
6. A CLEAR button.
some tips are:-
>> Put import java.awt.*; and import java.awt.event.*; at the top of your java source.
>> To find the mouse coordinates of a mouse event (e.g., a click), use the int getX() and int getY() methods on the MouseEvent object.
>> Remember that getGraphics() returns a Graphics object that represents one set of drawing parameter settings for the JFrame (there is no global Graphics object!).
>> Heres a code snippet to draw a blue dot at X=10, Y=100 on the JFrame:
Graphics G=getGraphics();
G.setColor(Color.BLUE);
G.drawRect(10,100,1,1);
>> Here's the mouse dragged handler we wrote in class for a (lame) painting function on the JFrame:
private void myMouseDragged(java.awt.event.MouseEvent evt) {
int x=evt.getX();
int y=evt.getY();
java.awt.Graphics G=getGraphics();
G.drawRect(x, y, 1, 1); }
>> And another snippet to find out which item was selected from a combo box:
public void actionPerformed(ActionEvent e) {
JComboBox cb=(JComboBox)e.getSource();
String itemName=(String)cb.getSelectedItem(); }
import java.awt.*; import java.awt.event.*; import javax.swing.*; class paintGUI extends JComponent { // Image in which we're going to draw private Image image; // Graphics2D object ==> used to draw on private Graphics2D g2; // Mouse coordinates private int currentX, currentY, oldX, oldY; public paintGUI() { setDoubleBuffered(false); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // save coord x,y when mouse is pressed oldX = e.getX(); oldY = e.getY(); System.out.println("Mouse pressed"); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { System.out.println("Mouse dragged"); // coord x,y when drag mouse currentX = e.getX(); currentY = e.getY(); } }); } protected void paintComponent(Graphics g) { if (image == null) { // image to draw null ==> we create image = createImage(getSize().width, getSize().height); g2 = (Graphics2D) image.getGraphics(); // clear draw area clear(); } g.drawImage(image, 0, 0, null); repaint(); } // now we create exposed methods public void clear() { g2.clearRect(0, 0, getSize().width, getSize().height); repaint(); } public void thin() { g2.setStroke(new BasicStroke(3)); } public void thick() { g2.setStroke(new BasicStroke(10)); } public void red() { // apply red color on g2 context g2.setPaint(Color.red); } public void black() { g2.setPaint(Color.black); } public void magenta() { g2.setPaint(Color.magenta); } public void drawLine() { System.out.println("Drawing line"); g2.drawLine(oldX, oldY, currentX, currentY); repaint(); } public void drawRectangle() { System.out.println("Drawing rectangle"); g2.drawRect(oldX, oldY, Math.abs(currentX - oldX), Math.abs(currentY - oldY)); g2.fillRect(oldX, oldY, Math.abs(currentX - oldX), Math.abs(currentY - oldY)); repaint(); } } public class GUIPaint { JButton clearBtn, blackBtn, redBtn, magentaBtn, filledRectangleBtn, lineBtn, thinBtn, thickBtn; paintGUI paintGUI; ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == clearBtn) { paintGUI.clear(); } else if (e.getSource() == thinBtn) { paintGUI.thin(); } else if (e.getSource() == thickBtn) { paintGUI.thick(); } else if (e.getSource() == blackBtn) { paintGUI.black(); } else if (e.getSource() == redBtn) { paintGUI.red(); } else if (e.getSource() == magentaBtn) { paintGUI.magenta(); } else if (e.getSource() == filledRectangleBtn) { paintGUI.drawRectangle(); } else if (e.getSource() == lineBtn) { paintGUI.drawLine(); } } }; public static void main(String[] args) { new GUIPaint().show(); } public void show() { // create main frame JFrame frame = new JFrame("Swing Paint"); Container content = frame.getContentPane(); // set layout on content pane content.setLayout(new BorderLayout()); // create draw area paintGUI = new paintGUI(); // add to content pane content.add(paintGUI, BorderLayout.CENTER); // create controls to apply colors and call clear feature JPanel controls = new JPanel(); clearBtn = new JButton("Clear"); clearBtn.addActionListener(actionListener); blackBtn = new JButton("Black"); blackBtn.addActionListener(actionListener); redBtn = new JButton("Red"); redBtn.addActionListener(actionListener); magentaBtn = new JButton("Magenta"); magentaBtn.addActionListener(actionListener); lineBtn = new JButton("Line"); lineBtn.addActionListener(actionListener); filledRectangleBtn = new JButton("Filled Rectangle"); filledRectangleBtn.addActionListener(actionListener); thickBtn = new JButton("Thick Line"); thickBtn.addActionListener(actionListener); thinBtn = new JButton("Thin Line"); thinBtn.addActionListener(actionListener); controls.add(lineBtn); controls.add(filledRectangleBtn); controls.add(thinBtn); controls.add(thickBtn); controls.add(blackBtn); controls.add(redBtn); controls.add(magentaBtn); controls.add(clearBtn); // add to content pane content.add(controls, BorderLayout.NORTH); frame.setSize(800, 800); // can close frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // show the swing paint result frame.setVisible(true); } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.