前提・実現したいこと
Java Appletを使って色々な色や太さの線を描く
###質問内容
色の指定に関してはなんの問題もないのですが、線の太さに問題があり、例えばthin(細い)を選択して線を書いた後にthick(太い)を選択して線を描こうとするとthinで書いた線までthickと同じ太さになってしまいます。選択した別々の太さで線を描きたいのですがどうしたらよろしいでしょう?
このサイトでの質問が初めてな上、プログラミング初心者なので、質問内容に至らぬ点が多くあるかも知れませんがそこも含めてご教示願えると幸いです。
該当のソースコード
Java
1import java.awt.BasicStroke; 2import java.awt.BorderLayout; 3import java.awt.Button; 4import java.awt.Canvas; 5import java.awt.Color; 6import java.awt.FlowLayout; 7import java.awt.Frame; 8import java.awt.Graphics; 9import java.awt.Graphics2D; 10import java.awt.Menu; 11import java.awt.MenuBar; 12import java.awt.MenuItem; 13import java.awt.Panel; 14import java.awt.event.ActionEvent; 15import java.awt.event.ActionListener; 16import java.awt.event.MouseEvent; 17import java.awt.event.MouseListener; 18import java.awt.event.MouseMotionListener; 19import java.awt.event.WindowEvent; 20import java.awt.event.WindowListener; 21import java.util.LinkedList; 22 23@SuppressWarnings("serial") 24public class FrameApp4 extends Frame implements WindowListener, ActionListener, MouseListener, MouseMotionListener{ 25 java.util.List<ColorPoint> events = new LinkedList<ColorPoint>(); 26 Canvas canvas; 27 Color color = Color.black; 28 int Fontweight = 0; 29 30 public static void main(String[] args) { 31 FrameApp4 frame = new FrameApp4(); 32 frame.init(); 33 frame.setSize(200, 240); 34 frame.addWindowListener(frame); 35 frame.setVisible(true); 36 } 37 38 public void init() { 39 setLayout(new BorderLayout()); 40 41 Panel panel = new Panel(); 42 { 43 panel.setLayout(new FlowLayout()); 44 Button black = new Button("black"); 45 black.addActionListener(this); 46 panel.add(black); 47 Button red = new Button("red"); 48 red.addActionListener(this); 49 panel.add(red); 50 Button green = new Button("green"); 51 green.addActionListener(this); 52 panel.add(green); 53 Button blue = new Button("blue"); 54 blue.addActionListener(this); 55 panel.add(blue); 56 Button yellow = new Button("yellow"); 57 yellow.addActionListener(this); 58 panel.add(yellow); 59 } 60 add(panel, BorderLayout.NORTH); 61 62 Panel panel2 = new Panel(); 63 { 64 panel2.setLayout(new FlowLayout()); 65 Button thin = new Button("thin"); 66 thin.addActionListener(this); 67 panel2.add(thin); 68 Button normal = new Button("normal"); 69 normal.addActionListener(this); 70 panel2.add(normal); 71 Button thick = new Button("thick"); 72 thick.addActionListener(this); 73 panel2.add(thick); 74 } 75 add(panel2, BorderLayout.SOUTH); 76 77 Panel panel3 = new Panel(); { 78 Button clear = new Button("clear"); 79 clear.addActionListener(this); 80 panel3.add(clear); 81 } 82 add(panel3, BorderLayout.WEST); 83 84 canvas = new Canvas() { 85 public void paint(Graphics g) { 86 ColorPoint p = null; 87 for (ColorPoint e : events) { 88 if (p != null && e != null) { 89 g.setColor(p.getColor()); 90 Graphics2D g2 = (Graphics2D)g; 91 BasicStroke BStroke = new BasicStroke(Fontweight); 92 g2.setStroke(BStroke); 93 g2.drawLine(p.getX(), p.getY(), e.getX(), e.getY()); 94 } 95 p = e; 96 } 97 } 98 }; 99 canvas.addMouseListener(this); 100 canvas.addMouseMotionListener(this); 101 add(canvas, BorderLayout.CENTER); 102 103 MenuBar mb = new MenuBar(); 104 { 105 Menu file = new Menu("File"); 106 { 107 MenuItem exit = new MenuItem("Exit"); 108 exit.addActionListener(this); 109 file.add(exit); 110 } 111 mb.add(file); 112 113 Menu color = new Menu("Color");{ 114 MenuItem black = new MenuItem("black"); 115 black.addActionListener(this); 116 color.add(black); 117 MenuItem blue = new MenuItem("blue"); 118 blue.addActionListener(this); 119 color.add(blue); 120 MenuItem red = new MenuItem("red"); 121 red.addActionListener(this); 122 color.add(red); 123 MenuItem green = new MenuItem("green"); 124 green.addActionListener(this); 125 color.add(green); 126 } 127 mb.add(color); 128 129 Menu fontweight = new Menu("Fontweight");{ 130 MenuItem thin = new MenuItem("thin"); 131 thin.addActionListener(this); 132 fontweight.add(thin); 133 MenuItem normal = new MenuItem("normal"); 134 normal.addActionListener(this); 135 fontweight.add(normal); 136 MenuItem thick = new MenuItem("thick"); 137 thick.addActionListener(this); 138 fontweight.add(thick); 139 } 140 mb.add(fontweight); 141 } 142 setMenuBar(mb); 143 } 144 145 public void actionPerformed(ActionEvent e) { 146 if (e.getActionCommand().equals("Exit")) { 147 System.exit(0); 148 } 149 if (e.getActionCommand().equals("clear")) { 150 events.clear(); 151 canvas.repaint(); 152 } 153 if (e.getActionCommand().equals("black")) { 154 color = Color.black; 155 } 156 if (e.getActionCommand().equals("red")) { 157 color = Color.red; 158 } 159 if (e.getActionCommand().equals("green")) { 160 color = Color.green; 161 } 162 if (e.getActionCommand().equals("blue")) { 163 color = Color.blue; 164 } 165 if (e.getActionCommand().equals("yellow")) { 166 color = Color.yellow; 167 } 168 if (e.getActionCommand().equals("thin")) { 169 Fontweight = 1; 170 } 171 if (e.getActionCommand().equals("normal")) { 172 Fontweight = 3; 173 } 174 if (e.getActionCommand().equals("thick")) { 175 Fontweight = 6; 176 } 177 178 } 179 180 public void mousePressed(MouseEvent e) { 181 events.add(new ColorPoint(e.getX(), e.getY(), color, Fontweight)); 182 canvas.repaint(); 183 } 184 185 public void mouseReleased(MouseEvent e) { 186 events.add(null); 187 } 188 189 public void mouseDragged(MouseEvent e) { 190 events.add(new ColorPoint(e.getX(), e.getY(), color, Fontweight)); 191 canvas.repaint(); 192 } 193 194 @Override 195 public void windowClosing(WindowEvent e) { 196 System.exit(0); 197 } 198 199 public void mouseMoved(MouseEvent e) {} 200 public void mouseClicked(MouseEvent e) {} 201 public void mouseEntered(MouseEvent e) {} 202 public void mouseExited(MouseEvent e) {} 203 public void windowOpened(WindowEvent e) {} 204 public void windowClosed(WindowEvent e) {} 205 public void windowIconified(WindowEvent e) {} 206 public void windowDeiconified(WindowEvent e) {} 207 public void windowActivated(WindowEvent e) {} 208 public void windowDeactivated(WindowEvent e) {} 209} 210 211class ColorPoint { 212 int x, y; 213 int Fontweight; 214 Color color; 215 216 public ColorPoint(int x, int y, Color color, int Fontweight) { 217 this.x = x; 218 this.y = y; 219 this.color = color; 220 this.Fontweight = Fontweight; 221 } 222 223 public int getX() { 224 return x; 225 } 226 227 public void setX(int x) { 228 this.x = x; 229 } 230 231 public int getY() { 232 return y; 233 } 234 235 public void setY(int y) { 236 this.y = y; 237 } 238 239 public Color getColor() { 240 return color; 241 } 242 243 public int getFontweight() { 244 return Fontweight; 245 } 246 247 public void setColor(Color color) { 248 this.color = color; 249 } 250 251 public void setFontweight(int Fontweight) { 252 this.Fontweight = Fontweight; 253 } 254}
補足情報(FW/ツールのバージョンなど)
eclipse Oxygen.1a Release (4.7.1a)
回答1件
あなたの回答
tips
プレビュー