/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dragb;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DragB extends JApplet
{
int mouseX, mouseY;
MyPane myPane;
public void init()
{
myPane = new MyPane();
setContentPane(myPane);
myPane.setBackground(Color.white);
myPane.addMouseMotionListener(new CircleDragger());
}
// Make your drawing widget by making a subcless of JPanel
// and override one method, paintComponent().
public class MyPane extends JPanel
{
// This inner class defines a drawing space
// and does the drawing on the space.
public void paintComponent(Graphics g)
{
// Draw the panel and mouse position
super.paintComponent(g); // Fills the panel space with background color.
g.setColor(Color.blue);
int margin = 1;
g.drawRect(margin,margin, getSize().width-2*margin,getSize().height-2*margin);
Graphics2D g2d = (Graphics2D) g;
int r =30;
GradientPaint gradient = new GradientPaint(mouseX-r/2,mouseY-r/2,Color.red,mouseX,mouseY,Color.orange);
g2d.setPaint(gradient);
g.fillOval(mouseX-r/2,mouseY-r/2,r,r);
g.setColor(Color.black);
g.drawString("(" + mouseX + "," + mouseY + ")",
mouseX, mouseY-r/2);
}
}
private class CircleDragger extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
myPane.repaint();
}
}
}
Tidak ada komentar:
Posting Komentar