Question

The assignment is to take the current program that draws a line and to add 4...

The assignment is to take the current program that draws a line and to add 4 buttons at the bottom of the frame that will change the line drawn to a different color (ie. No button pressed draws a black line, Button labeled Red changes any new lines drawn to red and retains any previously colored lines, etc for all the buttons). This is the code I have so far but I'm having issues with the buttons. This is also done entirely with AWT and no Swing. We can't use Swing with this program.

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MyNewDrawFrame extends Frame{
        
        public MyNewDrawFrame(){
                        setSize(500,500);
                        setTitle("Drawing is fun");
                        addWindowListener(new WindowAdapter(){
                                public void windowClosing(WindowEvent we){
                                        System.exit(0);
                                }
                        });
                        MyNewDrawPanel mdp = new MyNewDrawPanel();
                        add(mdp);
                        setVisible(true);
        }
        public static void main(String[] args){
                MyNewDrawFrame mdf = new MyNewDrawFrame();
        }
}
class MyNewDrawPanel extends DoubleBuffer implements MouseListener, MouseMotionListener{

        int lastX=0, lastY=0;
        ArrayList<Line>lines;
        Color drawColor;

        public MyNewDrawPanel() {
                lines = new ArrayList<Line>();
                setBackground(Color.white);
                drawColor = Color.black;
                setForeground(drawColor);
                addMouseListener(this);
                addMouseMotionListener(this);
        }
        public void mouseExited(MouseEvent me){}
        public void mouseClicked(MouseEvent me){}
        public void mouseReleased(MouseEvent me){}
        public void mouseEntered(MouseEvent me){
                record(me.getX(), me.getY());
        }
        public void mousePressed(MouseEvent me){
                record(me.getX(), me.getY());
        }
        public void mouseMoved(MouseEvent me){}
    public void mouseDragged(MouseEvent me){
                int x = me.getX();
                int y = me.getY();
                lines.add(new Line(lastX, lastY, x, y));
                record(x, y);
                repaint();
    }
        public void paint(Graphics g){
                for(Line line: lines){
                        g.drawLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
                }
        }
        protected void record(int x, int y) {
                lastX = x;
                lastY = y;
        }
}
class Line{

        int startx, starty, endx, endy;
        
        public Line(){}
        public Line(int startx, int starty, int endx, int endy){
                setStartX(startx);
                setStartY(starty);
                setEndX(endx);
                setEndY(endy);
        }
        public void setStartX(int startx){
                this.startx = startx;
        }
        public void setStartY(int starty){
                this.starty = starty;
        }
        public void setEndX(int endx){
                this.endx = endx;
        }
        public void setEndY(int endy){
                this.endy = endy;
        }
        public int getStartX(){
                return startx;
        }
        public int getStartY(){
                return starty;
        }
        public int getEndX(){
                return endx;
        }
        public int getEndY(){
                return endy;
        }
}

this is the supplemental program for MyNewDrawFrame called DoubleBuffer

import java.awt.*;

public class DoubleBuffer extends Panel{

        private int bufferWidth;
        private int bufferHeight;
        private Image bufferImage;
        private Graphics bufferGraphics;


    public DoubleBuffer(){
        super();
    }

        public void update(Graphics g){
                paintBuffer(g);
        }

        public void paintBuffer(Graphics g){
        //    checks the buffersize with the current panelsize
        //    or initialises the image with the first paint
        if(bufferWidth!=getSize().width || bufferHeight!=getSize().height ||bufferImage==null || bufferGraphics==null)
                        resetBuffer();
                if(bufferGraphics!=null){
                //this clears the offscreen image, not the onscreen one
                bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);

                    //calls the paintbuffer method with 
                //the offscreen graphics as a param
                paint(bufferGraphics);

                //we finaly paint the offscreen image onto the onscreen image
                g.drawImage(bufferImage,0,0,this);
        }
                        
        }

        public void paint(Graphics g){
                //in classes extended from this one, add something to paint here!
                //always remember, g is the offscreen graphics
        }
        private void resetBuffer(){
        // always keep track of the image size
        bufferWidth=getSize().width;
        bufferHeight=getSize().height;

                //    clean up the previous image
        if(bufferGraphics!=null){
                bufferGraphics.dispose();
                bufferGraphics=null;
        }
        if(bufferImage!=null){
                bufferImage.flush();
                bufferImage=null;
        }
        System.gc();

                //    create the new image with the size of the panel
        bufferImage=createImage(bufferWidth,bufferHeight);
        bufferGraphics=bufferImage.getGraphics();
        }
}

The code for the buttons should be implemented in the first program, MyNewDrawFrame.

Thanks in advance for any help.

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
The assignment is to take the current program that draws a line and to add 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • "Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import...

    "Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.applet.*; public class PolygonDrawer extends Applet implements MouseListener{ //Declares variables    private int[] X, Y;    private int ptCT;    private final static Color polygonColor = Color.BLUE; //Color stuff    public void init() {        setBackground(Color.BLACK);        addMouseListener(this);        X = new int [450];        Y =...

  • There 2 parts to complete: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Q4 extends JFrame...

    There 2 parts to complete: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Q4 extends JFrame { private int xPos, yPos; public Q4() { JPanel drawPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); // paint parent's background //complete this: } }; drawPanel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { //complete this :- set the xPos, yPos }    }); setContentPane(drawPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Mouse-Click Demo"); setSize(400, 250); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • Stick to the template which is provided on below and just change the //TODO part, please....

    Stick to the template which is provided on below and just change the //TODO part, please. Template Lab13.java package lab13; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; public class Lab13 {    public static void main (String[] args) {        JFrame frame = new SmileyFrame();        frame.add(new SmileyPanel());        frame.setVisible(true);    } } class SmileyFrame extends JFrame {    private static final long serialVersionUID = 7613378514394599117L;    private static final int WIDTH = 400;...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • Ex: In relation to image painting in frame, the following code is the use of getImage()...

    Ex: In relation to image painting in frame, the following code is the use of getImage() in toolkit. Modify the image() through ImageIcon and imageIO.read() of javax into a converted form using imageIO.read(). import java.awt.*; import javax.swing.*; public class FrameImage extends JPanel{ public void paint(Graphics g) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage("image/grapes.gif"); g.drawImage(image, 60, 40, this); } public static void main(String[] args) { JFrame frame = new JFrame(); FrameImage pannell = new FrameImage(); frame.add(pannell); frame.setTitle("image paint"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • 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 problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

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