ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.