I’m having two weird errors
New error is when i tell java to draw a string that displays the coordinateness of x and y, It doesn’t.
public void paint (Graphics g)
{
super.paint (g);
//System.out.println ("Boolean: " + this.closeDoors);
g.drawString("("+x+","+y+")",x,y);
}
Link to my program if you to compile it.
http://hotfile.com/dl/107032853/c81d927/Pigment.java.html
This is my complete program
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;
/**
*
* @author George Beazer
*/
public class Pigment extends JApplet
{
boolean closeDoors;
private int x = 0;
private int y = 0;
public static void main(String [] args)
{
Pigment stuff = new Pigment();
}
public Pigment()
{
setBackground (Color.blue);
}
@Override
public void init()
{
setLayout(new FlowLayout());
addMouseListener(new MyMouseListener());
}
@Override
public void paint (Graphics g)
{
super.paint (g);
//System.out.println ("Boolean: " + this.closeDoors);
g.drawString("("+x+","+y+")",x,y);
if (x > 35)
{
g.drawLine (35, 50, 570, 50);
g.drawLine (35, 50, 250, 0);
g.drawLine (250, 0, 570, 50);
g.drawRect (50, 50, 500, 350);
g.fillRect (100, 75, 80, 80);
g.fillRect (400, 75, 80, 80);
g.fillRect (240, 200, 125, 200);
}
else
{
g.drawLine (35, 50, 570, 50);
g.drawLine (35, 50, 250, 0);
g.drawLine (250, 0, 570, 50);
g.drawLine (180, 120, 100, 120);
g.drawLine (400, 120, 480, 120);
g.drawLine (140, 75, 140, 160);
g.drawLine (450, 75, 450, 160);
g.drawRect (50, 50, 500, 350);
g.drawRect (100, 75, 80, 80);
g.drawRect (400, 75, 80, 80);
g.drawRect (240, 200, 125, 200);
g.drawOval (330,280, 20, 20);
}
}
private class MyMouseListener implements MouseListener
{
public void mouseClicked (MouseEvent e)
{
x = e.getX();
y = e.getY();
}
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited(MouseEvent e){}
public void mousePressed (MouseEvent e){
}
public void mouseReleased (MouseEvent e){}
}
}
With regards to your first question, the reason that you’re getting a compiler error here:
Is that this
breakstatement is not actually in a loop. In Java, you can break out ofwhile,for,do...while, andswitchstatements, but notifstatements. There’s not a particularly good reason for this – it’s mainly historical – but the compiler does indeed enforce it.Of the three aforementioned control structures,
for,while, anddo...whileare referred to as loops because they execute code potentially many times. Thebreakstatement in this case is a way of saying “please abort execution of the current loop; I don’t want to run it any more.” Its opposite iscontinue, which means “please go to the next iteration of this loop.”The reason you can break out of a
switchis because Java’sswitchstatement is based on the C programming language’s version ofswitchin which labels are “fall-through.” In this context,breakmeans “I have finished executing all of the code I want to execute in this particular label; please get me out of this statement.” Interestingly, you canbreakout of aswitch, but you can’tcontinue.However, you cannot
breakout of anifstatement. There is no high-level reason for this, and in fact the language designers could just as easily have allowed this behavior to mean “stop executing this part of theifstatement.” I think the reason they opted not to do this is thatifstatements have a sort of “implicitbreak” at the end of each handler. For example, if you writeThen after executing
A, the control flow will immediately jump you toC, rather than falling through to theelsehandler.If you want to simulate a
breakout of of the middle of anifstatement, you could do something like this:The idea here is that you run the
ifstatement for some time, then decide using a secondifstatement whether or not to run the rest of the code in the loop.Hope this helps!