请参见下一次迭代:Java中的裸骨画家应用程序。
我有一个小小的绘图程序:
package net.coderodde.javapaint;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
/**
* This class implements a GUI component for drawing.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Jun 8, 2016)
*/
public class PaintCanvas extends Canvas {
/**
* The actual image being drawn to.
*/
private final BufferedImage image;
/**
* The graphics context of the above.
*/
private final Graphics2D imageGraphics;
public PaintCanvas(final int width, final int height) {
super();
super.setBounds(0, 0, width, height);
this.image = new BufferedImage(width,
height,
BufferedImage.TYPE_INT_RGB);
this.imageGraphics = this.image.createGraphics();
this.imageGraphics.setColor(Color.WHITE);
this.imageGraphics.fillRect(0, 0, width, height);
this.imageGraphics.setColor(Color.BLACK);
final PaintCanvasMouseListener listener =
new PaintCanvasMouseListener();
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
}
@Override
public void paint(final Graphics g) {
update(g);
}
@Override
public void update(final Graphics g) {
g.drawImage(this.image, 0, 0, null);
}
private class PaintCanvasMouseListener
extends MouseAdapter implements MouseMotionListener {
@Override
public void mouseClicked(final MouseEvent event) {
processEvent(event);
}
@Override
public void mouseDragged(final MouseEvent event) {
processEvent(event);
}
private void processEvent(final MouseEvent event) {
PaintCanvas.this.imageGraphics.fillOval(event.getX(), event.getY(), 5, 5);
PaintCanvas.this.repaint();
}
}
}package net.coderodde.javapaint;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
/**
* This class implements an application for simple drawing.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Jun 8, 2016)
*/
public class App {
private final JFrame frame = new JFrame("JavaPaint");
public App() {
frame.getContentPane().add(new PaintCanvas(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
setFrameLocationToCenter();
frame.setVisible(true);
}
private void setFrameLocationToCenter() {
final Dimension screenDimension = Toolkit.getDefaultToolkit()
.getScreenSize();
final int screenWidth = screenDimension.width;
final int screenHeight = screenDimension.height;
frame.setLocation((screenWidth - frame.getWidth()) / 2,
(screenHeight - frame.getHeight()) / 2);
}
public static void main(final String... args) {
final App app = new App();
}
}它使我得以画出以下杰作:

我最感兴趣的是关于内部的事情,但是你可以告诉我任何我想到的事情。
发布于 2016-06-08 13:15:57
几个注意事项:
package javafx_drawoncanvas;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavaFX_DrawOnCanvas extends Application {
@Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas(400, 400);
final GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
initDraw(graphicsContext);
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
graphicsContext.beginPath();
graphicsContext.moveTo(event.getX(), event.getY());
graphicsContext.stroke();
}
});
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED,
new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
graphicsContext.lineTo(event.getX(), event.getY());
graphicsContext.stroke();
}
});
canvas.addEventHandler(MouseEvent.MOUSE_RELEASED,
new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
}
});
StackPane root = new StackPane();
root.getChildren().add(canvas);
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Paint");
primaryStage.setScene(scene);
primaryStage.show();
}
private void initDraw(GraphicsContext gc){
double canvasWidth = gc.getCanvas().getWidth();
double canvasHeight = gc.getCanvas().getHeight();
gc.setFill(Color.LIGHTGRAY);
gc.setStroke(Color.BLACK);
gc.setLineWidth(5);
gc.fill();
gc.strokeRect(
0, //x of the upper left corner
0, //y of the upper left corner
canvasWidth, //width of the rectangle
canvasHeight); //height of the rectangle
gc.setFill(Color.RED);
gc.setStroke(Color.BLUE);
gc.setLineWidth(1);
}
public static void main(String... args) {
launch(args);
}
}发布于 2016-06-08 12:41:56
this.imageGraphics.setColor(Color.WHITE);
this.imageGraphics.fillRect(0, 0, width, height);
this.imageGraphics.setColor(Color.BLACK);可以将构造函数的这一部分移动到私有函数clearScreen中。
剩下的代码看起来很好。你可以对各种事情发表评论,比如
frame.setLocation((screenWidth - frame.getWidth()) / 2,
(screenHeight - frame.getHeight()) / 2);(a - b) / 2放在一个单独的函数中. frame.getContentPane().add(new PaintCanvas(640, 480));和
PaintCanvas.this.imageGraphics.fillOval(event.getX(), event.getY(), 5, 5);和
private final JFrame frame = new JFrame("JavaPaint");但是你做的是一个快速的应用程序。在我看来,你花在编码质量上的时间是正确的。..。我不知道你在这上面花了多长时间,但基本上,这已经足够干净了。添加更多的特性,然后在重复的代码弹出时进行必要的清理。
https://codereview.stackexchange.com/questions/131422
复制相似问题