Question

I have to create a Java paint/drawing application. The JFrame app must support the following functions:...

I have to create a Java paint/drawing application.

The JFrame app must support the following functions:
 Draw curves, specified by a mouse drag.
 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).
 Shape selection (line, rectangle or oval) selected by a combo box OR menu.
 Color selection using radio buttons OR menu.
 Line thickness using a combo box OR menu.
 A CLEAR button.

I have most of it done, but can't seem to get the combo boxes for the shape and line thickness selections to work. Help!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Paint Application:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintApp extends JApplet {

public static void main(String[] args) {
JFrame window = new JFrame("Paint Application");
SetPaintPanel content = new SetPaintPanel();
window.setContentPanel(content);
window.setSize(600,480);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}

public void init() {
setContentPanel( new SetPaintPanel() );
}

public static class SetPaintPanel extends JPanel
implements MouseListener, MouseMotionListener {

private final static int BLACK = 0,
RED = 1,
GREEN = 2,
BLUE = 3,
CYAN = 4,
MAGENTA = 5,
YELLOW = 6;

private int currentColor = BLACK;

private int prevX, prevY;

private boolean dragging;

private Graphics graphicsForDrawing;

SetPaintPanel() {
setBackground(Color.WHITE);
addMouseListener(this);
addMouseMotionListener(this);
}

public void paintComponent(Graphics g) {

super.paintComponent(g);

int width = getWidth();
int height = getHeight();

int colorSpacing = (height - 56) / 7;

g.setColor(Color.GRAY);
g.drawRect(0, 0, width-1, height-1);
g.drawRect(1, 1, width-3, height-3);
g.drawRect(2, 2, width-5, height-5);

g.fillRect(width - 56, 0, 56, height);


g.setColor(Color.WHITE);
g.fillRect(width-53, height-53, 50, 50);
g.setColor(Color.BLACK);
g.drawRect(width-53, height-53, 49, 49);
g.drawString("CLEAR", width-48, height-23);


g.setColor(Color.BLACK);
g.fillRect(width-53, 3 + 0*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.RED);
g.fillRect(width-53, 3 + 1*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.GREEN);
g.fillRect(width-53, 3 + 2*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.BLUE);
g.fillRect(width-53, 3 + 3*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.CYAN);
g.fillRect(width-53, 3 + 4*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.MAGENTA);
g.fillRect(width-53, 3 + 5*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.YELLOW);
g.fillRect(width-53, 3 + 6*colorSpacing, 50, colorSpacing-3);


g.setColor(Color.WHITE);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
}


private void onColorChange(int y) {

int width = getWidth();
int height = getHeight();
int colorSpacing = (height - 56) / 7;
int newColor = y / colorSpacing;

if (newColor < 0 || newColor > 6)
return;

Graphics g = getGraphics();
g.setColor(Color.GRAY);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
currentColor = newColor;
g.setColor(Color.WHITE);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
g.dispose();
}

private void setUpDrawingGraphics() {
graphicsForDrawing = getGraphics();
switch (currentColor) {
case BLACK:
graphicsForDrawing.setColor(Color.BLACK);
break;
case RED:
graphicsForDrawing.setColor(Color.RED);
break;
case GREEN:
graphicsForDrawing.setColor(Color.GREEN);
break;
case BLUE:
graphicsForDrawing.setColor(Color.BLUE);
break;
case CYAN:
graphicsForDrawing.setColor(Color.CYAN);
break;
case MAGENTA:
graphicsForDrawing.setColor(Color.MAGENTA);
break;
case YELLOW:
graphicsForDrawing.setColor(Color.YELLOW);
break;
}
}


public void mousePressed(MouseEvent evt) {
int x = evt.getX();
int y = evt.getY();

int width = getWidth();
int height = getHeight();

if (dragging == true)
return;

if (x > width - 53) {
if (y > height - 53)
repaint();
else
onColorChange(y);
}
else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) {
prevX = x;
prevY = y;
dragging = true;
setUpDrawingGraphics();
}
}

public void mouseReleased(MouseEvent evt) {
if (dragging == false)
return;
dragging = false;
graphicsForDrawing.dispose();
graphicsForDrawing = null;
}

public void mouseDragged(MouseEvent evt) {
if (dragging == false)
return;

int x = evt.getX();
int y = evt.getY();

if (x < 3)
x = 3;
if (x > getWidth() - 57)
x = getWidth() - 57;

if (y < 3)
y = 3;
if (y > getHeight() - 4)
y = getHeight() - 4;
graphicsForDrawing.drawLine(prevX, prevY, x, y);
prevX = x;
prevY = y;
}

public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseMoved(MouseEvent evt) { }
}
}

Add a comment
Know the answer?
Add Answer to:
I have to create a Java paint/drawing application. The JFrame app must support the following functions:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • One example of computer-aided design (CAD) is building geometric structures inter- actively. In Chapter 4, we...

    One example of computer-aided design (CAD) is building geometric structures inter- actively. In Chapter 4, we will look at ways in which we can model geometric objects comprised of polygons. Here, we want to examine the interactive part. Let’s start by writing an application that will let the user specify a series of axis- aligned rectangles interactively. Each rectangle can be defined by two mouse positions at diagonally opposite corners. Consider the event listener canvas.addEventListener("mousedown", function() { gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer); if...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT