LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java - Questions (https://www.linuxquestions.org/questions/programming-9/java-questions-428716/)

AquamaN 03-26-2006 02:15 PM

Java - Questions
 
Ok, I'm strayed from Obj-C, C/C++ and am trying out Java now. I have run into some trouble with my KeyListener as my simple program is not changing my modes (from polygon to circle).


Draw.java
Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
   
   
public class Draw extends JApplet implements KeyListener

        Display canvas;
       
        public void init()
        {
                canvas = new Display();
                setContentPane(canvas);
        }
         
        public char mode = 'c';
         
        public void keyTyped(KeyEvent evt)
        {
                char ch = evt.getKeyChar();
               
                if (ch == 'p' || ch == 'P')
                        mode = 'p';
                else if (ch == 'c' || ch == 'C')
                        mode = 'c';
                else if (ch == 'b' || ch == 'B')
                        mode = 'b';
        }
         
        public void keyPressed(KeyEvent evt) {}
        public void keyReleased(KeyEvent evt) {}
       
    class Display extends JPanel implements MouseListener
        {
           
          /* Variables for implementing polygon input. */

                private int[] xCoord, yCoord;  // Arrays containing the points of
                                        //  the polygon.  Up to 500 points
                                        //  are allowed.

                private int pointCt;  // The number of points that have been input.

                private Color polygonColor = Color.red;
                              // Color that is used to draw the polygons.
                 
                private Color theColor = Color.red;
               

                public Display()
                {
                        setBackground(Color.white);
                        addMouseListener(this);
                        xCoord = new int[500];
                        yCoord = new int[500];
                        pointCt = 0;
                }


                public void paintComponent(Graphics g)
                {

                        super.paintComponent(g); // Fills with background color, white.
                        g.setColor(Color.black);
                        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                        g.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
                        g.drawLine(35, 0, 35, 250);
                       
                        g.setColor(Color.black);
                        g.fillRect(0, 0, 35, 50);
                       
                        g.setColor(Color.blue);
                        g.fillRect(0, 50, 35, 100);
                       
                        g.setColor(Color.red);
                        g.fillRect(0, 100, 35, 150);
                       
                        g.setColor(Color.yellow);
                        g.fillRect(0, 150, 35, 200);
                       
                        g.setColor(Color.green);
                        g.fillRect(0, 200, 35, 250);
                }


                private void putLine(int x1, int y1, int x2, int y2)
                {
                        Graphics g = getGraphics();
                        g.drawLine(x1,y1,x2,y2);
                        g.dispose();
                }
                 
                private void drawACircle (int x1, int y1, int x2, int y2)
                {
                        Graphics g = getGraphics();
                        int dx, dy;
                       
                        dx = Math.abs(x1 - x2);
                        dy = Math.abs(y1 - y2);
                        double radius = Math.sqrt((dx * dx) + (dy * dy));
               
                        g.setColor(theColor);
                        g.fillOval(x1, y1, (int)radius, (int)radius);
                        g.dispose();
                }


                private void putPolygon()
                {
                        if (pointCt < 2)
                                return;
                        Graphics g = getGraphics();
                        if (pointCt == 2)
                        {
                                g.drawLine(xCoord[0], yCoord[0], xCoord[1], yCoord[1]);
                        }
                        else
                        {
                                g.setColor(theColor);
                                g.fillPolygon(xCoord, yCoord, pointCt);
                                g.setColor(Color.black);
                                g.drawPolygon(xCoord, yCoord, pointCt);
                        }
                                g.dispose();
                }


                public void mousePressed(MouseEvent evt)
                {
                        if (evt.isShiftDown())
                        {
                                pointCt = 0;
                                repaint();
                        }
                        else if ( pointCt > 0 && (Math.abs(xCoord[0] - evt.getX())<=2) && (Math.abs(yCoord[0] - evt.getY())<=2) && mode == 'p' )
                        {
                                putPolygon();
                                pointCt = 0;
                        }
                        else if (evt.isMetaDown() || pointCt == 500)
                        {
                                putPolygon();
                                pointCt = 0;
                        }
                        else
                        {
                                if (mode == 'p')
                                {
                               
                                        xCoord[pointCt] = evt.getX();
                                        yCoord[pointCt] = evt.getY();
                                                //Get color to be used
                                        if (evt.getX() <= 25)
                                        {
                                                if (evt.getY() <= 50)
                                                        theColor = Color.black;
                                                else if (evt.getY() <= 100)
                                                        theColor = Color.blue;
                                                else if (evt.getY() <= 150)
                                                        theColor = Color.red;
                                                else if (evt.getY() <= 200)
                                                        theColor = Color.yellow;
                                                else if (evt.getY() <= 250)
                                                        theColor = Color.green;
                               
                                                        pointCt--;
                                        }
                               
                                        pointCt++;
                               
                                        if (pointCt >= 2 && evt.getX() > 25)
                                        {
                                                putLine(xCoord[pointCt-2], yCoord[pointCt-2], xCoord[pointCt-1], yCoord[pointCt-1]);
                                        }
                                }// end if 'p'
               
                                if (mode == 'c') //Circle Mode
                                {
                                        xCoord[pointCt] = evt.getX();
                                        yCoord[pointCt] = evt.getY();
                               
                                        if (evt.getX() <= 25)
                                        {
                                                        if (evt.getY() <= 50)
                                                                theColor = Color.black;
                                                        else if (evt.getY() <= 100)
                                                                theColor = Color.blue;
                                                        else if (evt.getY() <= 150)
                                                                theColor = Color.red;
                                                        else if (evt.getY() <= 200)
                                                                theColor = Color.yellow;
                                                        else if (evt.getY() <= 250)
                                                                theColor = Color.green;
                                        }
                                       
                                        pointCt++;
                               
                                        if (pointCt >= 2 && evt.getX() > 25)
                                        {
                                                drawACircle(xCoord[pointCt-1], yCoord[pointCt-1], xCoord[pointCt], yCoord[pointCt]);
                                                pointCt = 0;
                                        }
                                }//end mode 'c'
                        } //end else
                        canvas.requestFocus();
                } // end mousePressed()

                public void mouseReleased(MouseEvent evt) { }
                public void mouseClicked(MouseEvent evt) { }
                public void mouseEntered(MouseEvent evt) { }
                public void mouseExited(MouseEvent evt) { }
         
        } // end nested class Display
}  // end class Draw

Draw.html
Code:

<html>
<head>
<TITLE>Simple Paint Program</TITLE>
</head>
<body>

<p align=center>
<applet code="Draw.class" height=250 width=300>
</applet>
</p>

</body>
</html>

Any help as to why it's not working properly would be great. Thanks guys.

AquamaN 03-26-2006 02:32 PM

Ok, figured it out. Needed "canvas.addKeyListener(this);" in my init().


All times are GMT -5. The time now is 05:55 AM.