I have written an applet in Java that allows the user to set background color, and "pen" color then click and draw in the window frame to drawn a line. I set this line by filling ovals at each x and y point of the dragging mouse. I also allows user to use the - and + keys to change the size of the line by increasing or decreasing the radius of the ovals. My issue is that there is some cause of lag in the drawing of the line. I believe it is in the mouseDrag method and the speed at which that method is executed limits how many x and y points can be attained to paint ovals on. Is there any way to alleviate this lag to allow for better lines?
import java.applet.Applet;
import java.awt.Event;
import java.awt.Color;
import java.awt.Graphics;
public class Screen extends Applet
{
int x = -20, y = -20;
int height = 20, width = 20;
boolean black, blue, cyan, green, magenta, orange, pink, red, white, yellow;
public void init()
{
setSize(400,400);
setBackground(Color.white);
}
public void paint(Graphics g)
{
if (black == true)
g.setColor(Color.BLACK);
else if (blue == true)
g.setColor(Color.BLUE);
else if (cyan == true)
g.setColor(Color.CYAN);
else if (green == true)
g.setColor(Color.GREEN);
else if (magenta == true)
g.setColor(Color.MAGENTA);
else if (orange == true)
g.setColor(Color.ORANGE);
else if (pink == true)
g.setColor(Color.PINK);
else if (red == true)
g.setColor(Color.RED);
else if (white == true)
g.setColor(Color.WHITE);
else if (yellow == true)
g.setColor(Color.YELLOW);
g.fillOval(x, y, width, height);
}
public boolean mouseDrag(Event e, int xPos, int yPos)
{
x = xPos;
y = yPos;
paint(getGraphics());
return true;
}
public boolean keyDown(Event e, int key)
{
black = false;
blue = false;
cyan = false;
green = false;
magenta = false;
orange = false;
pink = false;
red = false;
white = false;
yellow = false;
if (key == 'n')
{
x =-20;
y =-20;
update(getGraphics());
}
else if (key == 'x')
black = true;
else if (key == 'b')
blue = true;
else if (key == 'c')
cyan = true;
else if (key == 'g')
green = true;
else if (key == 'm')
magenta = true;
else if (key == 'o')
orange = true;
else if (key == 'p')
pink = true;
else if (key == 'r')
red = true;
else if (key == 'w')
white = true;
else if (key == 'y')
yellow = true;
else if (key == '+')
{
height += 5;
width += 5;
}
else if (key == '-')
{
height += -5;
width += -5;
}
else if (key == 'X')
setBackground(Color.BLACK);
else if (key == 'B' )
setBackground(Color.BLUE);
else if (key == 'C')
setBackground(Color.CYAN);
else if (key == 'G')
setBackground(Color.GREEN);
else if (key == 'M')
setBackground(Color.MAGENTA);
else if (key == 'O')
setBackground(Color.ORANGE);
else if (key == 'P')
setBackground(Color.PINK);
else if (key == 'R')
setBackground(Color.RED);
else if (key == 'W')
setBackground(Color.WHITE);
else if (key == 'Y')
setBackground(Color.YELLOW);
return true;
}
}
Your painting looks questionable:
paint(...)
directly, something that should never be done.super.paint(...)
method inside of your paint override.Instead you should:
repaint()
when you want to suggest that the GUI be paintedpaintComponent(...)
method.Edit:
For a complete example, please see my answer here: Changing JPanel Graphics g color drawing line