En af måderne man kan benytte sig af, er at tilføje en JPanel pr. firkant
lidt ligesom dette:
- import java.awt.Color;
-
- import javax.swing.JFrame;
-
- public class main extends JFrame
- {
- public static int frameX = 520;
- public static int frameY = 540;
-
- public static int circleX = 500;
- public static int circleY = 500;
- public static int circlePosX = 0;
- public static int circlePosY = 0;
-
-
-
- public static Panel panel= new Panel(0,0,circleX,circleY);
- public static main window;
-
- public main()
- {
- super("Circles 0.1");
- this.setVisible(true);
- this.setSize(frameX, frameY);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.add(panel);
- }
-
- public void removeRect(Panel rect){
- this.divideRect(rect);
- this.remove(rect);
- this.repaint();
- this.validate();
- }
-
- public void divideRect(Panel rect){
- Panel p1 = new Panel(rect.getCustomX(),rect.getCustomY(),rect.getCustomWidth()/2,rect.getCustomHeight()/2);
- Panel p2 = new Panel(rect.getCustomX()+rect.getCustomWidth()/2,rect.getCustomY(),rect.getCustomWidth()/2,rect.getCustomHeight()/2, Color.GREEN);
- Panel p3 = new Panel(rect.getCustomX(),rect.getCustomY()+rect.getCustomHeight()/2,rect.getCustomWidth()/2,rect.getCustomHeight()/2, Color.RED);
- Panel p4 = new Panel(rect.getCustomX()+rect.getCustomWidth()/2,rect.getCustomY()+rect.getCustomHeight()/2,rect.getCustomWidth()/2,rect.getCustomHeight()/2, Color.BLUE);
-
- this.add(p1);
- this.add(p2);
- this.add(p3);
- this.add(p4);
- }
-
- public static void main(String args[])
- {
- window = new main();
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
-
- import javax.swing.JPanel;
-
- @SuppressWarnings("serial")
- public class Panel extends JPanel
- {
- private int x,y,width,height;
- private Color col;
-
- public Panel(int x, int y,int width, int height){
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- col = Color.YELLOW;
-
- HandlerClass handler = new HandlerClass(this);
- this.addMouseListener(handler);
- this.addMouseMotionListener(handler);
- }
-
- public Panel(int x, int y,int width, int height, Color col){
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- this.col = col;
-
- HandlerClass handler = new HandlerClass(this);
- this.addMouseListener(handler);
- this.addMouseMotionListener(handler);
- }
-
- public int getCustomX(){
- return this.x;
- }
-
- public int getCustomY(){
- return this.y;
- }
-
- public int getCustomWidth(){
- return this.width;
- }
-
- public int getCustomHeight(){
- return this.height;
- }
-
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
-
- g.setColor(col);
- // g.fillRect(0, 0, main.circleX, main.circleY);
- g.fillRect(x, y, width,height);
-
- System.out.println("i");
- }
-
- private class HandlerClass implements MouseListener, MouseMotionListener
- {
-
- Panel rect;
-
- public HandlerClass(Panel rect){
- this.rect = rect;
- }
-
- public void mouseDragged(MouseEvent e)
- {
-
- }
-
- public void mouseMoved(MouseEvent e)
- {
-
- }
-
- public void mouseClicked(MouseEvent e)
- {
- main.window.removeRect(rect);
- }
-
- public void mouseEntered(MouseEvent e)
- {
-
- }
-
- public void mouseExited(MouseEvent e)
- {
-
- }
-
- public void mousePressed(MouseEvent e)
- {
-
- }
-
- public void mouseReleased(MouseEvent e)
- {
-
- }
-
- }
- }
En anden, og nok bedre metode, man kan benytte sig af, er at gøre brug af java2d, og have en liste af geometri modeller, som dit ene panel tegner, og så blot ændre på disse.
Så som dette:
- import java.awt.Color;
-
- import javax.swing.JFrame;
-
- public class main extends JFrame
- {
- public static int frameX = 520;
- public static int frameY = 540;
-
- public static int circleX = 500;
- public static int circleY = 500;
- public static int circlePosX = 0;
- public static int circlePosY = 0;
-
-
-
- public static Panel panel= new Panel(0,0,circleX,circleY);
- public static main window;
-
- public main()
- {
- super("Circles 0.1");
- this.setVisible(true);
- this.setSize(frameX, frameY);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.add(panel);
- }
-
-
-
- public static void main(String args[])
- {
- window = new main();
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.Rectangle2D;
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import javax.swing.JPanel;
-
- @SuppressWarnings("serial")
- public class Panel extends JPanel
- {
-
- private int x,y,width,height;
-
- private ArrayList<Rectangle2D> geometry = new ArrayList<Rectangle2D>();
- private HashMap<Rectangle2D, Color> colorMap = new HashMap<Rectangle2D, Color>();
-
- public Panel(int x, int y,int width, int height){
- HandlerClass handler = new HandlerClass(this);
- this.addMouseListener(handler);
- this.addMouseMotionListener(handler);
-
- Rectangle2D tempRect = new Rectangle(x, y, width, height);
- geometry.add(tempRect);
- colorMap.put(tempRect, Color.YELLOW);
- }
-
-
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
-
-
- for(Rectangle2D rect: geometry){
- g.setColor(colorMap.get(rect));
- g.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
- }
-
- }
-
- public void divideRect(Rectangle2D rect){
- Rectangle2D tempRect1 = new Rectangle((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth()/2, (int)rect.getHeight()/2);
- colorMap.put(tempRect1, Color.GREEN);
- Rectangle2D tempRect2 = new Rectangle((int)rect.getX()+(int)rect.getWidth()/2, (int)rect.getY(), (int)rect.getWidth()/2, (int)rect.getHeight()/2);
- colorMap.put(tempRect2, Color.RED);
- Rectangle2D tempRect3 = new Rectangle((int)rect.getX(), (int)rect.getY()+(int)rect.getHeight()/2, (int)rect.getWidth()/2, (int)rect.getHeight()/2);
- colorMap.put(tempRect3, Color.BLUE);
- Rectangle2D tempRect4 = new Rectangle((int)rect.getX()+(int)rect.getWidth()/2, (int)rect.getY()+(int)rect.getHeight()/2, (int)rect.getWidth()/2, (int)rect.getHeight()/2);
- colorMap.put(tempRect4, Color.YELLOW);
-
- geometry.remove(rect);
- geometry.add(tempRect1);
- geometry.add(tempRect2);
- geometry.add(tempRect3);
- geometry.add(tempRect4);
- }
-
- private class HandlerClass implements MouseListener, MouseMotionListener
- {
-
- Panel panel;
-
- public HandlerClass(Panel panel) {
- this.panel = panel;
- }
-
- public void mouseDragged(MouseEvent e)
- {
-
- }
-
- public void mouseMoved(MouseEvent e)
- {
-
- }
-
- public void mouseClicked(MouseEvent me)
- {
- int x = me.getX();
- int y = me.getY();
-
- Rectangle2D temp = new Rectangle(x, y, 5, 5);
-
- for(Rectangle2D rect : geometry){
- if(rect.intersects(temp)){
- panel.divideRect(rect);
- break;
- }
- }
-
- panel.repaint();
-
- }
-
- public void mouseEntered(MouseEvent e)
- {
-
- }
-
- public void mouseExited(MouseEvent e)
- {
-
- }
-
- public void mousePressed(MouseEvent e)
- {
-
- }
-
- public void mouseReleased(MouseEvent e)
- {
-
- }
-
- }
- }