然而,这不是我的主要问题。我想实现一个键处理程序,这样,当按下1-9键时,每个键代表该行的不同颜色变化。我已经尝试过实现一个密钥处理程序,但显然出现了问题,因为我无法完成它。任何帮助,将不胜感激。我的代码如下所示。
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);
}
}
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);
}
}
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());
}
}
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());
}
}
答案 0 :(得分:3)
在主要结束时:
sc.setFocusable(true);
sc.requestFocusInWindow();
KeyListener
答案 1 :(得分:2)
嗯,你只需要定义一个颜色变量。此变量将在您的处理键方法中设置,并在您的paint方法中使用。此外,您需要在按下一个键后刷新屏幕。
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);
}
}
}
}
...