Skip to main content

Problem with drawing circles...

1 reply [Last post]
Anonymous

Hi, i am actually creating a finte state simulator..and i need to draw circles on my canvas. The method of drawing circles i when the user clicks on the canvas, x and y coordinates are used to draw circles... but when the user clicks another point, the previous circle is erased and a new circle appears at the new point.. i need all circles to stay on the screen... i tried to implement an arraylist to hold an array of integers which hold my circle coordinates, then when paint is called, everything will be re-drawn by a for loop.. below is my code... please advise...

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;public class MyCanvas extends JPanel implements MouseListener { private int stateFlag =0; private int stateLocationX; private int stateLocationY; ArrayList stateList = new ArrayList(); Integer[] stateXY = new Integer[2];; int stateCount = 0; public MyCanvas() { super(); this.setBackground(Color.WHITE); this.setBorder(BorderFactory.createLineBorder(Color.BLACK)); this.setMinimumSize(new Dimension(400,300)); this.setPreferredSize(new Dimension(640,480)); addMouseListener(this); } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (stateFlag == 1) { for(int i = 0; i < stateCount; i++){ Integer[] coords = (Integer[])stateList.get(i); int x12 = coords[0].intValue(); int y12 = coords[1].intValue(); g2.setColor(Color.RED); g2.fillOval((x12-25),(y12-25),50,50); g2.setColor(Color.BLUE); g2.drawOval((x12-25),(y12-25),50,50); g2.drawString("q1", stateLocationX-5, stateLocationY-30); } g2.setColor(Color.RED); g2.fillOval((stateLocationX-25),(stateLocationY-25),50,50); g2.setColor(Color.BLUE); g2.drawOval((stateLocationX-25),(stateLocationY-25),50,50); g2.drawString("q1", stateLocationX-5, stateLocationY-30); } } public void setStateFlag(int myFlag) { stateFlag = myFlag; } public void clearCanvas() { stateFlag = 0; repaint(); } public void mousePressed (MouseEvent me) { stateLocationX = me.getX(); stateLocationY = me.getY(); stateXY[0] = new Integer(stateLocationX); stateXY[1] = new Integer(stateLocationY); stateList.add(stateCount++, stateXY); } public void mouseReleased (MouseEvent me) { repaint(); } public void mouseExited (MouseEvent me) {} public void mouseClicked (MouseEvent me) {} public void mouseEntered (MouseEvent me) {

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class MyCanvas extends JPanel implements MouseListener {

private int stateFlag =0;
private int stateLocationX;
private int stateLocationY;
ArrayList stateList = new ArrayList();
Integer[] stateXY = new Integer[2];;
int stateCount;

public MyCanvas() {
super();
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.setMinimumSize(new Dimension(400,300));
this.setPreferredSize(new Dimension(640,480));
addMouseListener(this);
}

public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (stateFlag == 1) {
for(int i = 0; i < stateCount; i++){
Integer[] coords = (Integer[])stateList.get(i);
int x12 = coords[0].intValue();
int y12 = coords[1].intValue();
g2.setColor(Color.RED);
g2.fillOval((x12-25),(y12-25),50,50);
g2.setColor(Color.BLUE);
g2.drawOval((x12-25),(y12-25),50,50);
g2.drawString("q1", stateLocationX-5, stateLocationY-30);

}
g2.setColor(Color.RED);
g2.fillOval((stateLocationX-25),(stateLocationY-25),50,50);
g2.setColor(Color.BLUE);
g2.drawOval((stateLocationX-25),(stateLocationY-25),50,50);
g2.drawString("q1", stateLocationX-5, stateLocationY-30);
}
}

public void setStateFlag(int myFlag) {
stateFlag = myFlag;
}

public void clearCanvas() {
stateFlag = 0;
repaint();
}

public void mousePressed (MouseEvent me) {
stateLocationX = me.getX();
stateLocationY = me.getY();
stateXY[0] = new Integer(stateLocationX);
stateXY[1] = new Integer(stateLocationY);
stateList.add(stateCount++, stateXY);
}

public void mouseReleased (MouseEvent me) {
repaint();

}
public void mouseExited (MouseEvent me) {}
public void mouseClicked (MouseEvent me) {}
public void mouseEntered (MouseEvent me) {}

}