Java Code Drawing Circles
Java Code Drawing Circles
*;
public class Rect {
private int X,Y,a,b;
private Color color;
public Rect(int x, int y, int w, int z, Color c{
X=x;
Y=y;
a=w;
b=z;
color = c;
}
public void draw (Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
g.drawRect(X, Y, a, b);
g.setColor(oldColor);
}
public boolean containsPoint(int x, int y){
return (x>X && x<X+a && y>Y && y<Y+b);
}
public void move(int xAmount , int yAmount){
X+= xAmount;
Y+= yAmount;
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ColorPanel extends JPanel{
private Rect selected;
private Rect s1;
private int x,y;
public ColorPanel(Color backColor){
setBackground (backColor);
s1 = new Rect(50,50,50,50, Color.red);
selected = null;
addMouseListener(new PanelListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
s1.draw(g);
}
private class PanelListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
if(selected == null){
if (s1.containsPoint(e.getX(), e.getY())){
selected = s1;}
x=e.getX();
y=e.getY();}
else{
int newX= e.getX();
int newY = e.getY();
int dx = newX-x;
int dy = newY-y;
selected.move(dx, dy);
repaint();
selected = null;
x=newX;
y=newY;
}
}
}
}
import java.awt.*;
import javax.swing.*;
public class GUI {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(500,500);
jf.setTitle("Hi");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorPanel panel = new ColorPanel(Color.white);
Container pane = jf.getContentPane();
pane.add(panel);
jf.setVisible(true);
}
}