0% found this document useful (0 votes)
23 views

Java Code Drawing Circles

This document defines a Rect class that represents a rectangle with properties like position, dimensions, and color. It contains methods to draw, check if a point is contained, and move the rectangle. It also defines a ColorPanel class that extends JPanel and contains a Rect object. It overrides the paintComponent method to draw the rectangle and adds a mouse listener to allow selecting and moving the rectangle by clicking and dragging. Finally, it defines a GUI class with a main method that creates a JFrame, adds a ColorPanel to it, and makes the frame visible, providing a simple GUI for interacting with the rectangle.

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java Code Drawing Circles

This document defines a Rect class that represents a rectangle with properties like position, dimensions, and color. It contains methods to draw, check if a point is contained, and move the rectangle. It also defines a ColorPanel class that extends JPanel and contains a Rect object. It overrides the paintComponent method to draw the rectangle and adds a mouse listener to allow selecting and moving the rectangle by clicking and dragging. Finally, it defines a GUI class with a main method that creates a JFrame, adds a ColorPanel to it, and makes the frame visible, providing a simple GUI for interacting with the rectangle.

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.awt.

*;
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);
}
}

You might also like