グラフィックスの基礎
public void paint (Graphics g){ Color c1=new Color(100, 200, 100); g.setColor(c1); //矩形 g.drawRect(5,5,getWidth()-10,getHeight()-10); //色変更 Color c2=new Color(100, 100, 200); g.setColor(c2); //楕円 g.setColor(c2); g.fillOval(20,40,70,30); g.drawOval(120,40,70,30); //格子の表示 int i; g.setColor(new Color(100,100,250)); for( i=0;i<=5;i++){ g.drawLine(20,100+10*i,220,100+10*i); } for( i=0;i<=10;i++){ g.drawLine(20+20*i,100,20+20*i,150); } }
private Button button1 = new Button(); private void jbInit() throws Exception { button1.setLabel("hello"); button1.setBounds(new Rectangle(126, 256, 75, 27)); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); }}); this.setLayout(null); this.add(button1, null); }
try { jbInit(); } catch(Exception e) { e.printStackTrace(); }
void button1_actionPerformed(ActionEvent e) { int xp,yp; Graphics g=getGraphics(); xp=(int)(Math.random()*200); yp=(int)(Math.random()*100)+150; g.drawString("hello",xp,yp); }