前提・実現したいこと
三角形をカーソルを使ってかけるようにしたいのですが、やり方が分かりません・・・
この場合、新しくzといった変数を作るのが方法かなと考えましたが・・・
発生している問題・エラーメッセージ
三角形の作り方が分かりません・・・
該当のソースコード
javafx
1 2こちらが、メインのクラスのコードです 3package kadai; 4 5import java.io.File; 6import java.io.FileInputStream; 7import java.io.FileOutputStream; 8import java.io.ObjectInputStream; 9import java.io.ObjectOutputStream; 10import java.util.ArrayList; 11 12import javafx.application.Application; 13import javafx.event.ActionEvent; 14import javafx.event.EventHandler; 15import javafx.scene.Scene; 16import javafx.scene.canvas.Canvas; 17import javafx.scene.canvas.GraphicsContext; 18import javafx.scene.control.ColorPicker; 19import javafx.scene.control.Menu; 20import javafx.scene.control.MenuBar; 21import javafx.scene.control.MenuItem; 22import javafx.scene.control.RadioMenuItem; 23import javafx.scene.control.ToggleGroup; 24import javafx.scene.control.ToolBar; 25import javafx.scene.input.MouseEvent; 26import javafx.scene.layout.BorderPane; 27import javafx.scene.paint.Color; 28import javafx.stage.FileChooser; 29import javafx.stage.Stage; 30 31public class twelve extends Application{ 32 private Canvas cv; 33 private ToolBar tb; 34 private ColorPicker cp; 35 private ToggleGroup tg; 36 private RadioMenuItem[] rmi = new RadioMenuItem[4]; 37 private MenuItem[] mi = new MenuItem[2]; 38 private Menu [] mn = new Menu[3]; 39 private MenuBar mb; 40 private ArrayList<Shape> shapeList; 41 private int currentShape; 42 private Color currentColor; 43 private double x1,x2,y1,y2; 44 public static void main(String[]args) { 45 launch(args); 46 } 47 public void start(Stage stage)throws Exception{ 48 cv = new Canvas(600,340); 49 mb = new MenuBar(); 50 cp = new ColorPicker(); 51 tb = new ToolBar(); 52 53 mn[0] = new Menu("ファイル"); 54 mn[1] = new Menu("設定"); 55 mn[2] = new Menu("図形"); 56 57 mi[0] = new MenuItem("開く"); 58 mi[1] = new MenuItem("保存"); 59 60 rmi[0] = new RadioMenuItem("四角形"); 61 rmi[1] = new RadioMenuItem("楕円"); 62 rmi[2] = new RadioMenuItem("直線"); 63 rmi[3] = new RadioMenuItem("三角形"); 64 65 mn[0].getItems().add(mi[0]); 66 mn[0].getItems().add(mi[1]); 67 68 mn[1].getItems().add(mn[2]); 69 70 mn[2].getItems().add(rmi[0]); 71 mn[2].getItems().add(rmi[1]); 72 mn[2].getItems().add(rmi[2]); 73 mn[2].getItems().add(rmi[3]); 74 75 mb.getMenus().add(mn[0]); 76 mb.getMenus().add(mn[1]); 77 78 tb.getItems().add(cp); 79 80 tg = new ToggleGroup(); 81 rmi[0].setToggleGroup(tg); 82 rmi[1].setToggleGroup(tg); 83 rmi[2].setToggleGroup(tg); 84 rmi[3].setToggleGroup(tg); 85 86 BorderPane bp = new BorderPane(); 87 88 bp.setTop(mb); 89 bp.setCenter(cv); 90 bp.setBottom(tb); 91 92 for(int i = 0;i<mi.length;i++) { 93 mi[i].setOnAction(new SampleEventHandler()); 94 } 95 for(int i = 0;i<rmi.length;i++) { 96 rmi[i].setOnAction(new SampleEventHandler()); 97 } 98 cp.setOnAction(new SampleEventHandler()); 99 100 cv.addEventHandler(MouseEvent.MOUSE_PRESSED,(new SampleEventHandler())); 101 cv.addEventHandler(MouseEvent.MOUSE_RELEASED,(new SampleEventHandler())); 102 103 shapeList =new ArrayList<Shape>(); 104 currentShape = Shape.TRI; 105 currentColor = Color.RED; 106 cp.setValue(currentColor); 107 rmi[0].setSelected(true); 108 x1 = -1;y1 = -1;x2 = 1;y2 = -1; 109 110 Scene sc = new Scene(bp,600,400); 111 112 stage.setScene(sc); 113 stage.setTitle("サンプル"); 114 stage.show(); 115 } 116 class SampleEventHandler implements EventHandler<ActionEvent>{ 117 public void handle(ActionEvent e) { 118if(e.getSource() == mi[0]) { 119 try { 120 FileChooser fc = new FileChooser(); 121 fc.getExtensionFilters().add( 122 new FileChooser.ExtensionFilter("グラフィックファイル", "*,g")); 123 File flo = fc.showOpenDialog(new Stage()); 124 if(flo != null) { 125 ObjectInputStream oi = new ObjectInputStream(new FileInputStream(flo)); 126 Shape tmp = null; 127 shapeList.clear(); 128 while((tmp = (Shape)oi.readObject())!= null){ 129shapeList.add(tmp); 130 } 131 oi.close(); 132 } 133 } 134 catch(Exception ex) { 135 ex.printStackTrace(); 136 } 137 GraphicsContext gc = cv.getGraphicsContext2D(); 138 gc.clearRect(0, 0, 600, 340); 139 for(int i = 0;i<shapeList.size();i++) { 140 Shape sh = (Shape)shapeList.get(i); 141 sh.draw(gc); 142 } 143 }else if(e.getSource() == mi[1]) { 144 try { 145 FileChooser fc = new FileChooser(); 146 fc.getExtensionFilters().add( 147 new FileChooser.ExtensionFilter("グラフィックファイル", "*,g")); 148 File fls = fc.showOpenDialog(new Stage()); 149 if(fls != null) { 150 ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(fls)); 151 for(int i = 0;i<shapeList.size();i++) { 152 oo.writeObject(shapeList.get(i)); 153 } 154 oo.writeObject(null); 155 oo.close(); 156 } 157 } 158 catch(Exception ex) { 159 ex.printStackTrace(); 160 } 161} 162 else if(e.getSource() == rmi[0]) { 163 currentShape = Shape.RECT; 164 } 165 else if(e.getSource() == rmi[1]) { 166 currentShape = Shape.OVAL; 167 } 168 else if(e.getSource() == rmi[2]) { 169 currentShape = Shape.LINE; 170 } 171 else if(e.getSource() == rmi[3]) { 172 currentShape = Shape.TRI; 173 } 174 else if(e.getSource() == cp) { 175 currentColor = cp.getValue(); 176 } 177 } 178 } 179 class SampleMouseEventHandler implements EventHandler<MouseEvent>{ 180 public void handle(MouseEvent e) { 181if(e.getEventType() == MouseEvent.MOUSE_PRESSED) { 182 x2 = e.getX(); 183 y2 = e.getY(); 184 if(x1 < 0 || y1 < 0 ||(x1 == x2 && y1 == y2)) 185 return; 186 Shape sh = null; 187 if(currentShape == Shape.RECT){ 188 sh = new Rect(); 189 } 190 else if(currentShape == Shape.OVAL) { 191 sh = new Rect(); 192 } 193else if(currentShape == Shape.LINE) { 194 sh = new Trian(); 195 } 196else if(currentShape == Shape.TRI) { 197 sh = new Trian(); 198 } 199 double r = currentColor.getRed(); 200 double g= currentColor.getGreen(); 201 double b = currentColor.getBlue(); 202 sh.setColor(r,g,b); 203 204 if(currentShape != Shape.LINE){ 205 if(x1 > x2) { 206 x2 = x1; 207 x1 = e.getX(); 208 } 209 if(y1 > y2) { 210 y2 = y1; 211 y1 = e.getX(); 212 } 213 } 214 sh.setStartPoint(x1,y1); 215 sh.setEndPoint(x2,y2); 216 217 shapeList.add(sh); 218 219 GraphicsContext gc = cv.getGraphicsContext2D(); 220 sh.draw(gc); 221 } 222 } 223 } 224 225} 226こちらが、メインのクラスで必要となるShapeクラスのコードです 227package kadai; 228 229import java.io.Serializable; 230 231import javafx.scene.canvas.GraphicsContext; 232import javafx.scene.paint.Color; 233 234abstract class Shape implements Serializable{ 235 static final int RECT = 0; 236 static final int OVAL = 1; 237 static final int LINE = 2; 238 static final int TRI = 3; 239 240 double x1,y1,x2,y2; 241 double r,g,b; 242 243 abstract public void draw(GraphicsContext gc); 244 245 public void setColor(double r,double g,double b) { 246 this.r = r; 247 this.g = g; 248 this.b = b; 249 } 250 public void setStartPoint(double x,double y) { 251 x1 = x; 252 y1 = y; 253 } 254 public void setEndPoint(double x,double y) { 255 x2 = x; 256 y2 = y; 257 } 258} 259class Rect extends Shape implements Serializable{ 260 public void draw(GraphicsContext gc) { 261 gc.setFill(Color.color(r,g,b)); 262 gc.fillRect(x1, y1, x2-x1, y2-y1); 263 } 264} 265class Oval extends Shape implements Serializable{ 266 public void draw(GraphicsContext gc) { 267 gc.setFill(Color.color(r,g,b)); 268 gc.fillOval(x1, y1, x2-x1, y2-y1); 269 } 270} 271class Line extends Shape implements Serializable{ 272 public void draw(GraphicsContext gc) { 273 gc.setStroke(Color.color(r,g,b)); 274 gc.strokeLine(x1, y1, x2, y2); 275 } 276} 277class Trian extends Shape implements Serializable{ 278 public void draw(GraphicsContext gc) { 279 gc.setFill(Color.color(r,g,b)); 280 gc.fillPolygon(); 281 } 282} 283
試したこと
多角形の作り方を調べました。
補足情報(FW/ツールのバージョンなど)
jdk14
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/06 17:17