Changing the line color in a paint program using events
I am trying to create a program that draws a line using points. I did it a bit. Then the line should be redrawn when the program is minimized and zoomed in again. This is where my first problem comes in. Let's say I draw 2 lines, the last point of the first line and the first point of my second line are merged together when it is maximized.
This is not my main problem. I want to implement a key handler so that if keys 1-9 are pressed, each key represents a different color change for the string. I tried to implement the key handler, but it is obvious that something is wrong because I cannot get it to work. Any help would be greatly appreciated. My code is shown below.
package part2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.awt.Point;
import java.awt.Color;
public class ScribbleComponentRepaint extends JComponent implements Scribbler {
int w = 640;
int h = 360;
Point c;
ArrayList<Point> line;
ArrayList<ArrayList<Point>> lines;
public static void main(String[] args) {
ScribbleComponentRepaint sc = new ScribbleComponentRepaint();
new JEasyFrame(sc, "Scribble");
sc.addMouseListener(new Clicker(sc));
sc.addMouseMotionListener(new Mover(sc));
sc.addKeyListener(new Keys(sc));
}
public ScribbleComponentRepaint() {
line = new ArrayList<Point>();
lines = new ArrayList<ArrayList<Point>>();
}
public void handleKey(int e) {
switch (e) {
case KeyEvent.VK_1:
break;
default: {
System.out.println("Default");
}
}
}
public void paintComponent(Graphics g) {
for (ArrayList<Point> line : lines) {
int n = 0;
int limit = 1;
if (line.size() > 0) {
for (Point p : line) {
Point c = line.get(Math.max(n - limit, 0));
n++;
g.setColor(Color.RED);
g.drawLine(c.x, c.y, p.x, p.y);
}
}
}
}
public void penDown(Point p) {
c = p;
}
public void penUp(Point p) {
drawTo(p);
c = null;
}
public void drawTo(Point p) {
if (c != null) {
Graphics g = getGraphics();
g.setColor(Color.BLUE);
g.drawLine(c.x, c.y, p.x, p.y);
c = p;
}
line.add(c);
lines.add(line);
}
public void drag(Point p) {
//should not be null
drawTo(p);
}
public Dimension getPreferredSize() {
return new Dimension(w, h);
}
}
code>
package part2;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Keys extends KeyAdapter {
Scribbler sc;
public Keys(Scribbler sc){
this.sc = sc;
}
public void keyPressed(KeyEvent e) {
//System.out.println(k);
sc.handleKey(e.getKeyCode());
}
}
code>
package part2;
import java.awt.*;
public interface Scribbler {
public void paintComponent(Graphics g);
public void penDown(Point p);
public void drag(Point p);
public void penUp(Point p);
public void handleKey(int e);
}
code>
+3
source to share
2 answers
At the end of main:
sc.setFocusable(true);
sc.requestFocusInWindow();
BTW
- The GUI should be built on EDT.
- Consider using key bindings instead
KeyListener
- Well-designed code (as with the interface definition and coding), but for better help, most likely post SSCCE . I had to make some code changes before I could test my theory that the key discovery problem was "focus".
+3
source to share
Well, you just need to define a color variable. This flavor will be specified in key handling methods and used in your paint method. Also, you will need to refresh the screen after pressing a key.
public class ScribbleComponentRepaint extends JComponent implements Scribbler {
Color color = Color.BLUE; // var definition
...
public void handleKey(int e) {
switch (e) {
case KeyEvent.VK_1:
color = Color.RED; // setting the new color
break;
default: {
System.out.println("Default");
}
}
repaint(); // updates the screen
}
...
public void paintComponent(Graphics g) {
for (ArrayList<Point> line : lines) {
int n = 0;
int limit = 1;
if (line.size() > 0) {
for (Point p : line) {
Point c = line.get(Math.max(n - limit, 0));
n++;
g.setColor(color); // uses the color var
g.drawLine(c.x, c.y, p.x, p.y);
}
}
}
}
...
+2
source to share