PDA

View Full Version : Java Help



NKN
12-20-2012, 01:49 AM
So.. I wrote this for the lulzy.

It deletes the circle behind it when going down and to the right, but then messes up. Run it and watch what happens. I'm like, smashing my head on my desk, cause idk what to do.


package tictactoe;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;


public class TicTacToeBase {

public static void main(String[] args){
System.out.print("Lol");
ShowGUI();
}
public static void ShowGUI(){
JFrame frame = new JFrame("Coloring Canvas Thingyz");
frame.add(new Panel());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
Graphics g = frame.getGraphics();
Graphics2D g2d = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double();
double x,y = 0.0;
x = 50.0;
y = 40.0;
System.out.print(frame.getWidth());
System.out.print(frame.getHeight());
Boolean xadd = true,yadd = true;
PointerInfo a = MouseInfo.getPointerInfo();
while(true) {
a = MouseInfo.getPointerInfo();
Point pnt = a.getLocation();
if((int)circle.getMaxX() <= 25)
xadd = true;
if((int)circle.getMinY() <= 10)
yadd = true;
if((int)circle.getMinX() >= frame.getWidth() - 25)
xadd = false;
if((int)circle.getMaxY() >= frame.getHeight() - 10)
yadd = false;
if(circle.contains(pnt)){
yadd = getNextBool();
xadd = getNextBool();
}
System.out.print("Circle Min X: "+circle.getMinX() + "\n");
System.out.print("Circle Max Y: "+circle.getMaxY() + "\n");
if(xadd == true && yadd == true){
x = x + 1;
y = y + 1;
}
if(xadd == false && yadd == true){
x = x - 1;
y = y + 1;
}
if(xadd == false && yadd == false){
x = x - 1;
y = y - 1;
}
if(xadd == true && yadd == false){
x = x + 1;
y = y - 1;
}
circle.setFrameFromCenter(x, y, x+10,y+10);
if(xadd == true && yadd == true){
g2d.clearRect((int)circle.getMinX()-10, (int)circle.getMinY()-10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}
if(xadd == false && yadd == true){
g2d.clearRect((int)circle.getMinX()+10, (int)circle.getMinY()-10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}
if(xadd == false && yadd == false){
g2d.clearRect((int)circle.getMinX()+10, (int)circle.getMinY()+10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}
if(xadd == true && yadd == false){
g2d.clearRect((int)circle.getMinX()-10, (int)circle.getMinY()+10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}

g2d.draw(circle);
wait(7);



}



}
public static void wait(int sec) {
try {
Thread.sleep(sec);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean getNextBool(){
Random rnd = new Random();
return rnd.nextBoolean();
}
@SuppressWarnings("serial")
static class Panel extends JPanel{
public Panel(){
setBorder(BorderFactory.createEtchedBorder(EtchedB order.LOWERED));

}
public Dimension getPreferredSize() {
return new Dimension(500,500);
}
public void paintComponent(Graphics g){
super.paintComponent(g);





}


}

}




The clearing part is from here:

circle.setFrameFromCenter(x, y, x+10,y+10);
if(xadd == true && yadd == true){
g2d.clearRect((int)circle.getMinX()-10, (int)circle.getMinY()-10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}
if(xadd == false && yadd == true){
g2d.clearRect((int)circle.getMinX()+10, (int)circle.getMinY()-10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}
if(xadd == false && yadd == false){
g2d.clearRect((int)circle.getMinX()+10, (int)circle.getMinY()+10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}
if(xadd == true && yadd == false){
g2d.clearRect((int)circle.getMinX()-10, (int)circle.getMinY()+10,
((int)circle.getMaxX() - (int)circle.getMinX()+10),((int)circle.getMaxY() - (int)circle.getMinY()+10));
}


This isn't a homework assignment or anything, so dw, I'm not cheating.