我的代碼將根據用戶單擊的位置繪制圓圈,然后繪制連接這些圓圈的線。我希望將圓圈和線條的顏色默認設定為黑色,但用戶可以鍵入 r,b,g,l (red, blue,green,black) 來設定 NEXT 圓圈和線條的顏色被繪制。我必須在 KeyTyped 方法中放入什么?每當用戶輸入 r、b、g 或 l 時,我都會設定 if/else 陳述句。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Circle extends JPanel implements MouseListener, KeyListener, MouseMotionListener {
private List<Point> points;
public static void main(String[] args) {
EventQueue.invokeLater(() -> new Circle().buildAndDisplayGui());
}
public Circle() {
points = new ArrayList<>();
setPreferredSize(new Dimension(500, 500));
setBackground(Color.white);
addMouseListener(this);
}
private void buildAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.addKeyListener(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
int count = points.size();
for (int i = 0; i < count; i ) {
Point pt0 = points.get(i);
g.fillOval(pt0.x - 10, pt0.y - 11, 20, 20);
if (i > 0) {
Point pt1 = points.get(i - 1);
g.drawLine(pt1.x, pt1.y, pt0.x, pt0.y);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
//Prints the coordinates of the clicks
System.out.println("X: " e.getX() " Y: " e.getY());
Point pt = new Point(e.getX(), e.getY());
points.add(pt);
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// Do nothing.
}
@Override
public void mouseExited(MouseEvent e) {
// Do nothing.
}
@Override
public void mousePressed(MouseEvent e) {
// Do nothing.
}
@Override
public void mouseReleased(MouseEvent e) {
// Do nothing.
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'r') {
//Sets the color of the next circle and lines to red
} else {
if (e.getKeyChar() == 'b') {
//Sets the color of the next circle and lines to blue
} else {
if (e.getKeyChar() == 'g') {
//Sets the color of the next circle and lines to green
} else {
if (e.getKeyChar() == 'l') {
//Sets the color of the next circle and lines to black
}
}
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
電流輸出:

期望的輸出(如果用戶在兩次滑鼠點擊后按下“r”并且再點擊兩次滑鼠):

uj5u.com熱心網友回復:
將
Color實體變數添加到class Circle. 在示例中,我將其稱為“currentColor”。我Color.black在建構式中將其設定為。將每個專案保存
Color在points. 一種方法是擁有一個并行陣列。我不喜歡緊密耦合的陣列,所以我在示例中創建了一個輔助類。
class PointAndColor {
Point thePoint;
Color theColor;
PointAndColor () {
theColor = Color.black;
thePoint = new Point (0,0);
}
PointAndColor (Point point, Color color) {
theColor = color;
thePoint = point;
}
Color getColor () { return theColor; }
Point getPoint () { return thePoint; }
}
我更改了點串列的通用型別:
private List<PointAndColor> points;
private Color currentColor;
用點保存顏色的示例:
@Override
public void mouseClicked(MouseEvent e) {
//Prints the coordinates of the clicks
System.out.println("X: " e.getX() " Y: " e.getY());
Point pt = new Point(e.getX(), e.getY());
points.add (new PointAndColor (pt, currentColor));
repaint();
}
keyTyped在方法中設定當前顏色@Override public void keyTyped(KeyEvent e) { switch (e.getKeyChar()) { case 'r': currentColor = Color.red; break; case 'g': currentColor = Color.green; break; case 'b': currentColor = Color.blue; break; case 'l': currentColor = Color.black; break; default: break; } System.out.println ("Color set to " currentColor);}
Color使用檢索Point:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int count = points.size();
for (int i = 0; i < count; i ) {
Point pt0 = points.get(i).getPoint();
g.setColor (points.get(i).getColor());
g.fillOval(pt0.x - 10, pt0.y - 11, 20, 20);
if (i > 0) {
Point pt1 = points.get(i - 1).getPoint();
g.drawLine(pt1.x, pt1.y, pt0.x, pt0.y);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/531998.html
下一篇:避免使用for回圈低于索引-1
