fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5. import java.util.ArrayList;
  6.  
  7. public class PaintLine extends JFrame {
  8.  
  9. private int lineLimit;
  10. private ArrayList<StraightLine> straightLines;
  11. private boolean isNewClick;
  12. private StraightLine tmpStraightLine;
  13. private boolean canDraw;
  14.  
  15. private DrawingPanel drawingPanel;
  16.  
  17. public static void main(String[] args) {
  18. SwingUtilities.invokeLater(() -> {
  19. PaintLine paintLine = new PaintLine("直線の描画");
  20. paintLine.setVisible(true);
  21. });
  22. }
  23.  
  24. PaintLine(String title) {
  25. lineLimit = 10;
  26. straightLines = new ArrayList<>();
  27. isNewClick = true;
  28. canDraw = true;
  29.  
  30. setTitle(title);
  31. setSize(300, 200);
  32. setLocationRelativeTo(null);
  33. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.  
  35. drawingPanel = new DrawingPanel();
  36.  
  37. getContentPane().add(drawingPanel);
  38. }
  39.  
  40. public MouseAdapter mouseAdapter = new MouseAdapter() {
  41. @Override
  42. public void mousePressed(MouseEvent e) {
  43. if (canDraw) {
  44. if (isNewClick) {
  45. tmpStraightLine = new StraightLine();
  46. tmpStraightLine.setStartPoint(e.getPoint());
  47. isNewClick = false;
  48. } else {
  49. tmpStraightLine.setEndPoint(e.getPoint());
  50. straightLines.add(tmpStraightLine);
  51. tmpStraightLine = null;
  52. isNewClick = true;
  53. drawingPanel.repaint();
  54. }
  55. updateCanDrawState();
  56. }
  57. }
  58. };
  59.  
  60. public void updateCanDrawState() {
  61. if ((straightLines != null && straightLines.isEmpty()) || straightLines.size() < lineLimit) {
  62. canDraw = true;
  63. } else {
  64. canDraw = false;
  65. }
  66. }
  67.  
  68. private class DrawingPanel extends JPanel {
  69.  
  70. public DrawingPanel() {
  71. setBackground(Color.YELLOW);
  72. addMouseListener(mouseAdapter);
  73. }
  74.  
  75. protected void paintComponent(Graphics g) {
  76. super.paintComponent(g);
  77. Graphics2D g2d = (Graphics2D) g.create();
  78. g2d.setColor(Color.BLUE);
  79. if (!canDraw) {
  80. // straightLines.remove(straightLines.size() - 1);
  81. g2d.drawString("もう書けません!", 50, 50);
  82. }
  83. straightLines.stream().forEach(straightLine -> g2d.drawLine(
  84. straightLine.startPoint.x,
  85. straightLine.startPoint.y,
  86. straightLine.endPoint.x,
  87. straightLine.endPoint.y
  88. ));
  89. g2d.dispose();
  90. }
  91. }
  92.  
  93. private class StraightLine {
  94.  
  95. private Point startPoint;
  96. private Point endPoint;
  97.  
  98. public void setStartPoint(Point startPoint) {
  99. this.startPoint = startPoint;
  100. }
  101.  
  102. public void setEndPoint(Point endPoint) {
  103. this.endPoint = endPoint;
  104. }
  105. }
  106.  
  107. }
  108.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class PaintLine is public, should be declared in a file named PaintLine.java
public class PaintLine extends JFrame {
       ^
1 error
stdout
Standard output is empty