Related
I am trying to start the program with jcheckbox hat selected and rectangle visible then the Rectangle disappears when the checkbox is unselected and repainted as checkbox is selected again. When I run the program and check the box another check box appears or left of the frame.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class Head extends JPanel {
JCheckBox hat;
public Head() {
hat = new JCheckBox("Hat");
hat.setSelected(true);
hat.addItemListener(new CheckSelection());
add(hat);
}
class CheckSelection implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}
public void paintComponent(Graphics g) {
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
if (hat.isSelected()) {
g.drawRect(100, 90, 120, 10);
}
}
public static void main(String[] args) {
Head head = new Head();
JFrame f = new JFrame();
f.add(head);
f.setSize(400, 400);
//f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You've broken the paint chain by not calling the paintComponent's super method
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
if (hat.isSelected()) {
g.drawRect(100, 90, 120, 10);
} else {
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
}
}
The Graphics context is a shared resource between components, one of the jobs of paintComponent is to prepare the Graphics for painting, typically by filling it with the background color of the component. So failing to call super.paintComponent means that what ever was previously painted to the Graphics context will still be there
See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing
I'm having trouble with my buttons. I know they're working because I've tested them out by exiting the problem through System.exit. Here is what my output looks like:
http://imgur.com/Ks7mIFa
When I click the close button, the handle on the switch should redraw to the other side and the close button should change to open. When I click the open button, it should do the opposite. However, the buttons aren't doing anything. What am I doing wrong?
public class ProgrammingAssignment2 {
public static void main(String[] args) {
boolean ison = false;
// Objects
Circuit circuitObject = new Circuit();
Controller controllerObject = new Controller();
Draw drawObject = new Draw();
AllListeners listenerObject = new AllListeners();
drawObject.window();
circuitObject.buttons(drawObject, ison);
controllerObject.openFile("Programming Assignment 2 Data.txt", drawObject);
}
}
Class circuit just creates the buttons
import javax.swing.JButton;
public class Circuit {
public void buttons(Draw drawObject, boolean ison) {
AllListeners listenerObject = new AllListeners();
if (ison == true) {
JButton openButton = new JButton("Close");
openButton.addActionListener(listenerObject);
openButton.setBounds(200, 100, 50, 20);
drawObject.add(openButton);
} else if (ison == false) {
JButton closeButton = new JButton("Open");
closeButton.addActionListener(listenerObject);
closeButton.setBounds(50, 100, 50, 20);
drawObject.add(closeButton);
}
}
}
Draw class does most of the work. It creates all the graphics and reads in a text file that has the titles of each object(like switch and lightbulb).
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import java.awt.Graphics;
public class Draw extends JFrame {
private String[] line = new String[5];
private int counter = 0;
private boolean ison;
public void window() {
setSize(500, 500);
setTitle("Programming Assignment 2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void readFile(String filename) {
counter = 1;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
for (int i = 0; i < 4; i++) {
line[i] = br.readLine();
}
} catch (FileNotFoundException e) {
String error = "File was not found";
System.out.println(error.toString());
System.out.println("or could not be opened.");
} catch (IOException e) {
System.out.println("Error reading from file.");
} finally {
try {
br.close();
} catch (IOException e) {
System.out.println("Error closing file.");
}
}
}
public void paint(Graphics g) {
Circuit circuitObject = new Circuit();
super.paint(g);
if (ison == true) {
turnon(g);
circuitObject.buttons(this, ison);
} else {
turnoff(g);
}
}
public void setisOn() {
ison = true;
}
public void setisOff() {
ison = false;
}
public void turnoff(Graphics g) {
// Title
g.drawString(line[0], 150, 40);
//Switch
g.drawString(line[2], 130, 190);
g.drawRect(100, 150, 100, 20);
g.drawOval(115, 155, 10, 10);
g.drawOval(175, 155, 10, 10);
g.drawArc(140, 140, 20, 20, 180, -180);
//off switch
g.drawLine(160, 150, 182, 133);
g.drawLine(157, 142, 173, 128);
g.drawLine(173, 128, 182, 133);
//Power Supply
g.drawString(line[1], 50, 420);
g.drawRect(50, 320, 50, 80);
g.drawLine(50, 320, 70, 290);
g.drawLine(100, 320, 120, 290);
g.drawLine(70, 290, 120, 290);
g.drawLine(120, 370, 120, 290);
g.drawLine(120, 370, 100, 400);
//plus
g.drawLine(94, 310, 100, 310);
g.drawLine(97, 307, 97, 313);
// minus
g.drawLine(100, 300, 107, 300);
// pliers
g.drawRect(70, 305, 5, 10);
g.drawRect(90, 288, 5, 10);
//lightbulb
g.drawString(line[3], 400, 250);
g.drawRect(400, 200, 20, 20);
g.drawOval(395, 170, 30, 30);
// pliers
g.drawRect(400, 220, 5, 10);
g.drawRect(415, 220, 5, 10);
// plus wire to switch
g.drawLine(72, 305, 120, 160);
//bulb to switch
g.drawLine(180, 160, 400, 230);
//bulb to minus
g.drawLine(90, 290, 420, 230);
}
public void turnon(Graphics g) {
// Title
g.drawString(line[0], 150, 40);
//Switch
g.drawString(line[2], 130, 190);
g.drawRect(100, 150, 100, 20);
g.drawOval(115, 155, 10, 10);
g.drawOval(175, 155, 10, 10);
g.drawArc(140, 140, 20, 20, 180, -180);
//on switch
g.drawLine(140, 150, 122, 133);
g.drawLine(143, 142, 129, 128);
g.drawLine(122, 133, 129, 128);
//Power Supply
g.drawString(line[1], 50, 420);
g.drawRect(50, 320, 50, 80);
g.drawLine(50, 320, 70, 290);
g.drawLine(100, 320, 120, 290);
g.drawLine(70, 290, 120, 290);
g.drawLine(120, 370, 120, 290);
g.drawLine(120, 370, 100, 400);
//plus
g.drawLine(94, 310, 100, 310);
g.drawLine(97, 307, 97, 313);
// minus
g.drawLine(100, 300, 107, 300);
// pliers
g.drawRect(70, 305, 5, 10);
g.drawRect(90, 288, 5, 10);
//lightbulb
g.drawString(line[3], 400, 250);
g.drawRect(400, 200, 20, 20);
g.drawOval(395, 170, 30, 30);
// pliers
g.drawRect(400, 220, 5, 10);
g.drawRect(415, 220, 5, 10);
// plus wire to switch
g.drawLine(72, 305, 120, 160);
//bulb to switch
g.drawLine(180, 160, 400, 230);
//bulb to minus
g.drawLine(90, 290, 420, 230);
}
}
Controller doesn't do much right.
public class Controller {
public void openFile(String filename, Draw drawObject) {
drawObject.readFile(filename);
}
}
And this is the actionlisterner class
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AllListeners implements ActionListener {
public void actionPerformed(ActionEvent e) {
Circuit circuitObject = new Circuit();
Draw drawObject = new Draw();
String buttonString = e.getActionCommand();
if (buttonString.equals("Close")) {
drawObject.setisOn();
drawObject.repaint();
} else if (buttonString.equals("Open")) {
drawObject.setisOff();
drawObject.repaint();
} else {
System.out.println("Unexpected error.");
}
}
}
You've got lots of major problems with this code including:
Drawing directly within a JFrame's paint method, something fraught with problems as you risk messing up the JFrame's own complicated painting.
Placing program logic within a painting method, a method you don't have full control over when or if it fires, and one that you shouldn't slow down
Placing component creation code within a painting method.
Trying to add multiple JButtons willy nilly rather than changing the state of an existing component.
Creating multiple Circuit objects.
Trying to use absolute positioning via setBounds(...) to place a component in a container that uses BorderLayout.
I suggest that you
Start over and scrap this code.
Draw only in a JPanel's paintComponent method, just as the tutorials will tell you to do.
Create your buttons once and only once and add them to your GUI.
Get all program logic out of the painting (here paintComponent) method, and all object state changing outside of that method as they are for painting and painting only.
Instead the logic should belong to the controller, and which should be notified of the button push.
So consider having the button push notify the control what was pushed,
the control changes the state of the program (changes variables)
and then it calls repaint so that the paintComponent method can use those variables to change its drawing.
Also, avoid using setBounds and null layouts if at all possible, and instead use layout managers/borders/nested JPanels to help you place your components.
I have a lot of QuadCurve2D methods in a drawing, but when I fill them they don't actually fill the whole image.
Code for fill and curves:
g2.setStroke(new BasicStroke(5));
QuadCurve2D earLeft1 = new QuadCurve2D.Double(145, 155, 137.5, 49, 150, 49);
g2.draw(earLeft1);
QuadCurve2D earLeft2 = new QuadCurve2D.Double(150, 49, 156.25, 49, 200, 100);
g2.draw(earLeft2);
QuadCurve2D betweenEars = new QuadCurve2D.Double(200, 100, 237.5, 88, 262.5, 87.5);
g2.draw(betweenEars);
QuadCurve2D earRight1 = new QuadCurve2D.Double(262.5, 87.5, 287.5, 25, 300, 25);
g2.draw(earRight1);
QuadCurve2D earRight2 = new QuadCurve2D.Double(300, 25, 312.5, 25, 337.5, 137.5);
g2.draw(earRight2);
CubicCurve2D headPhoneLeft = new CubicCurve2D.Double(145, 155, 75, 175, 100, 250, 150, 250);
g2.draw(headPhoneLeft);
CubicCurve2D headPhoneRight = new CubicCurve2D.Double(337.5, 137.5, 387.5, 137.5, 393.75, 188, 362.5, 225);
g2.draw(headPhoneRight);
QuadCurve2D headbandTop1 = new QuadCurve2D.Double(109, 177, 150, 75, 225, 50);
g2.draw(headbandTop1);
QuadCurve2D headbandTop2 = new QuadCurve2D.Double(225, 50, 300, 50, 372, 150);
g2.draw(headbandTop2);
QuadCurve2D headbandBottom1 = new QuadCurve2D.Double(135, 155, 150, 112.5, 212.5, 78);
g2.draw(headbandBottom1);
QuadCurve2D headbandBottom2 = new QuadCurve2D.Double(212.5, 78, 306.25, 78, 351, 137.5);
g2.draw(headbandBottom2);
QuadCurve2D faceBottomLeft = new QuadCurve2D.Double(150, 250, 162.5, 275, 200, 300);
g2.draw(faceBottomLeft);
QuadCurve2D faceBottomRight = new QuadCurve2D.Double(362.5, 225, 363.5, 237.5, 350, 262.5);
g2.draw(faceBottomRight);
CubicCurve2D leftArm = new CubicCurve2D.Double(200, 300, 87.5, 300, 87.5, 375, 188.5, 362.5);
g2.draw(leftArm);
CubicCurve2D rightArm = new CubicCurve2D.Double(350, 262.5, 425, 237.5, 450, 300, 375, 325);
g2.draw(rightArm);
QuadCurve2D leftLegOuter = new QuadCurve2D.Double(188.5, 362.5, 154, 425, 200, 512.5);
g2.draw(leftLegOuter);
QuadCurve2D rightLegOuter = new QuadCurve2D.Double(375, 325, 388.5, 356.25, 387.5, 412.5);
g2.draw(rightLegOuter);
QuadCurve2D leftFootTop = new QuadCurve2D.Double(200, 512.5, 125, 500, 130, 562.5);
g2.draw(leftFootTop);
QuadCurve2D leftFootBottom = new QuadCurve2D.Double(130, 562.5, 175, 575, 262.5, 562.5);
g2.draw(leftFootBottom);
QuadCurve2D leftLegInner = new QuadCurve2D.Double(262.5, 562.5, 237.5, 400, 268.75, 363);
g2.draw(leftLegInner);
QuadCurve2D rightLegInner = new QuadCurve2D.Double(268.75, 363, 318.75, 362.5, 337.5, 475);
g2.draw(rightLegInner);
QuadCurve2D rightFootBottom = new QuadCurve2D.Double(337.5, 475, 400, 480, 455, 470);
g2.draw(rightFootBottom);
QuadCurve2D rightFootTop = new QuadCurve2D.Double(455, 470, 450, 400, 387.5, 412.5);
g2.draw(rightFootTop);
CubicCurve2D tailInner = new CubicCurve2D.Double(268.75, 363, 287.5, 450,125, 387.5, 62.5, 400);
g2.draw(tailInner);
//QuadCurve2D tailInner1 = new QuadCurve2D.Double(268.75, 363, 275, 387.5, 200, 400);
//g2.draw(tailInner1);
//QuadCurve2D tailInner2 = new QuadCurve2D.Double(200, 400, 125, 387.5, 62.5, 400);
//g2.draw(tailInner2);
QuadCurve2D tailOuter1 = new QuadCurve2D.Double(62.5, 400, 25, 425, 200, 437.5);
g2.draw(tailOuter1);
QuadCurve2D tailOuter2 = new QuadCurve2D.Double(200, 437.5, 287.5, 425, 300, 375);
g2.draw(tailOuter2);
int[] x = {175, 200, 225, 225, 287, 300, 309, 337, 325, 309, 302, 292, 240, 227, 226, 215};
int[] y = {225, 210, 237, 200, 187, 212, 187, 187, 262, 262, 230, 268, 275, 250, 277, 281};
GeneralPath mouthAndTeeth = new GeneralPath();
mouthAndTeeth.moveTo(x[0], y[0]);
for (int index = 1; index < x.length; index++) {
mouthAndTeeth.lineTo(x[index], y[index]);
};
mouthAndTeeth.closePath();
g2.draw(mouthAndTeeth);
/*
g2.fill(earLeft1);
g2.fill(earLeft2);
g2.fill(earRight1);
g2.fill(earRight2);
g2.fill(betweenEars);
g2.fill(headbandBottom1);
g2.fill(headbandBottom2);
g2.fill(headbandTop1);
g2.fill(headbandTop2);
g2.fill(headPhoneLeft);
g2.fill(headPhoneRight);
g2.fill(faceBottomLeft);
g2.fill(faceBottomRight);
g2.fill(leftArm);
g2.fill(rightArm);
g2.fill(leftFootBottom);
g2.fill(leftFootTop);
g2.fill(rightFootBottom);
g2.fill(rightFootTop);
g2.fill(leftLegInner);
g2.fill(leftLegOuter);
g2.fill(rightLegInner);
g2.fill(rightLegOuter);
//g2.fill(tailInner1);
//g2.fill(tailInner2);
g2.fill(tailOuter1);
g2.fill(tailOuter2);
*/
This has been my only solution but it's not doing it. Suggestions?
"A" solution might be to paint the "shape" to a BufferedImage and simply scale the image, but this will scale the stroke/line size as well.
A better solution might be to encapsulate the drawing into a Shape and use Shape#createTransformedShape instead.
public class Cat extends Path2D.Double {
public Cat() {
//Left Ear
moveTo(145, 155);
curveTo(145, 155, 137.5, 49, 150, 49);
moveTo(150, 49);
curveTo(150, 49, 156.25, 49, 200, 100);
// Between Ears
moveTo(200, 100);
curveTo(200, 100, 237.5, 88, 262.5, 87.5);
// Right ear
moveTo(262.5, 87.5);
curveTo(262.5, 87.5, 287.5, 25, 300, 25);
moveTo(300, 25);
curveTo(300, 25, 312.5, 25, 337.5, 137.5);
// Head phone left
moveTo(300, 25);
append(new CubicCurve2D.Double(145, 155, 75, 175, 100, 250, 150, 250), false);
moveTo(337.5, 137.5);
append(new CubicCurve2D.Double(337.5, 137.5, 387.5, 137.5, 393.75, 188, 362.5, 225), false);
moveTo(109, 177);
curveTo(109, 177, 150, 75, 225, 50);
moveTo(225, 50);
curveTo(225, 50, 300, 50, 372, 150);
moveTo(135, 155);
curveTo(135, 155, 150, 112.5, 212.5, 78);
moveTo(212.5, 78);
curveTo(212.5, 78, 306.25, 78, 351, 137.5);
moveTo(150, 250);
curveTo(150, 250, 162.5, 275, 200, 300);
moveTo(362.5, 225);
curveTo(362.5, 225, 363.5, 237.5, 350, 262.5);
moveTo(200, 300);
append(new CubicCurve2D.Double(200, 300, 87.5, 300, 87.5, 375, 188.5, 362.5), false);
moveTo(350, 262.5);
append(new CubicCurve2D.Double(350, 262.5, 425, 237.5, 450, 300, 375, 325), false);
moveTo(188.5, 362.5);
curveTo(188.5, 362.5, 154, 425, 200, 512.5);
moveTo(375, 325);
curveTo(375, 325, 388.5, 356.25, 387.5, 412.5);
moveTo(200, 512.5);
curveTo(200, 512.5, 125, 500, 130, 562.5);
moveTo(130, 562.5);
curveTo(130, 562.5, 175, 575, 262.5, 562.5);
moveTo(262.5, 562.5);
curveTo(262.5, 562.5, 237.5, 400, 268.75, 363);
moveTo(268.75, 363);
curveTo(268.75, 363, 318.75, 362.5, 337.5, 475);
moveTo(337.5, 475);
curveTo(337.5, 475, 400, 480, 455, 470);
moveTo(455, 470);
curveTo(455, 470, 450, 400, 387.5, 412.5);
moveTo(268.75, 363);
append(new CubicCurve2D.Double(268.75, 363, 287.5, 450, 125, 387.5, 62.5, 400), false);
moveTo(62.5, 400);
curveTo(62.5, 400, 25, 425, 200, 437.5);
moveTo(200, 437.5);
curveTo(200, 437.5, 287.5, 425, 300, 375);
int[] x = {175, 200, 225, 225, 287, 300, 309, 337, 325, 309, 302, 292, 240, 227, 226, 215};
int[] y = {225, 210, 237, 200, 187, 212, 187, 187, 262, 262, 230, 268, 275, 250, 277, 281};
GeneralPath mouthAndTeeth = new GeneralPath();
mouthAndTeeth.moveTo(x[0], y[0]);
for (int index = 1; index < x.length; index++) {
mouthAndTeeth.lineTo(x[index], y[index]);
}
mouthAndTeeth.closePath();
append(mouthAndTeeth, false);
}
}
And then scale the shape based on the available space of the container...
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Cat cat;
public TestPane() {
cat = new Cat();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(5));
int catWidth = cat.getBounds().x + cat.getBounds().width;
int catHeight = cat.getBounds().y + cat.getBounds().height;
int width = getWidth() - 1;
int height = getHeight() - 1;
double scaleWidth = width / (double)catWidth;
double scaleHeight = height / (double)catHeight;
double scale = Math.min(scaleHeight, scaleWidth);
AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
Shape shape = cat.createTransformedShape(at);
g2d.draw(shape);
g2d.dispose();
}
}
}
Which you use will come down to needs, for example, using a Shape like this, won't allow you to color individual sections differently
I am wondering why the method setBackground() is not actually making the background black. I have feeling this has something to do with the class implementing JApplet as opposed to Applet but I can not figure out the specifics. It's really bugging me. Any help is appreciated!
import javax.swing.*;
import java.awt.*;
public class Rocket extends JApplet
{
private final int APPLET_WIDTH = 200;
private final int APPLET_HEIGHT = 200;
private int[] xRocket = {100, 120, 120, 130, 130, 70, 70, 80, 80};
private int[] yRocket = {15, 40, 115, 125, 150, 150, 125, 115, 40};
private int[] xWindow = {95, 105, 110, 90};
private int[] yWindow = {45, 45, 70, 70};
private int[] xFlame = {70, 70, 75, 80, 90, 100, 110, 115, 120, 130, 130};
private int[] yFlame = {155, 170, 165, 190, 170, 175, 160, 185, 160, 175, 155};
public void init ()
{
setBackground (Color.black);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
public void paint (Graphics page)
{
page.setColor (Color.cyan);
page.fillPolygon (xRocket, yRocket, xRocket.length);
page.setColor (Color.gray);
page.fillPolygon (xWindow, yWindow, xWindow.length);
page.setColor (Color.red);
page.drawPolyline (xFlame, yFlame, xFlame.length);
}
}
Set the color on the ContentPane rather than on the parent applet component
getContentPane().setBackground(Color.BLACK);
Aside:
For custom painting in Swing override paintComponent rather than paint. JApplet is not a sub-class of JComponent so a new component based on this is needed to do this. Make sure to invoke super.paintComponent(g).
I don't know why but nothing is appearing?
I suppose to have a applet of a house.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.*;
public class color extends JApplet
{
public void init()
{
addMouseListener(new MyMouseListener());
getContentPane().setBackground(Color.white);
}
public class MyMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
boolean closeDoors = true;
if(x>330 && x<280 && y>20 && y<20)
{
closeDoors = false;
repaint();
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
}
public void paint ( Graphics g, boolean closeDoors)
{
super.paint (g);
do
{
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);
}
while (closeDoors = true);
if (closeDoors = false);
{
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);
}
}
}
You are probably looping inside the paint method. It seems like an infinite loop there.
do
{
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);
} while (closeDoors = true);
I would replace this with:
if (closeDoors = true)
{
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);
}
I'll try to help get you on the right track :-)
You may already know this, but if your not using an IDE, I recommend using appletviewer to develop your applets instead of with a browser. Just food for thought :-)
First of all, Toader Mihai Claudiu's suggestion is correct. Change
do
{
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);
}
while (closeDoors = true);
if (closeDoors = false);
{
into
if (closeDoors)
{
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
{
Otherwise, you're going to be painting as long as closeDoors is true. You just need to paint once. Java will ask you to paint again when it has to (for instance, when you call repaint()).
Also, set closeDoors as a member variable. In other words, have:
public class color extends JApplet
{
public boolean closeDoors = false;
And when you switch the value of closeDoors in the click listener, you can simplify it as:
int x = e.getX();
int y = e.getY();
if(x>330 && x<280 && y>20 && y<20)
{
closeDoors = !closeDoors;
repaint();
}
That will, when you click in your specific area, invert the value of closeDoors. In other words, if closeDoors is true, it will be set to false, and vice versa.
Note, your code if(x>330 && x<280 && y>20 && y<20) probably won't work at all, since y cannot be greater than 20 and less than 20 at the same time, ever. I'll let you play with that to figure out what works :-).
Hope this helps.
Just a minor detail, but you should probably call your class Color instead of color to follow Java's standard naming convention, or call it something else if you don't want to clash with java.awt.Color.
Generally, in Swing you should never override the paint() method but instead paintComponent. (I'm not sure about JApplet, though - I would paint instead on a JPanel inside the applet, not the applet itself.)
And no endless loop in your paint-method - it should return quickly, not work eternally, as the Toader already said.
But this is not your problem, seemingly, as you wrote in a comment:
I get Applet not initializing
Add such (important!) information to the question (it has an edit link for a reason, you know).
Your browser should have a Java console somewhere, use it, and look whether there is some error message. Copy this (including the stacktrace, if any) to your question. This could enable us helping you.
(If you are using OpenJDK with the icedTea-Plugin on Linux, look at ~/.icedteaplugin/java.stderr and ~/.icedteaplugin/java.stdout instead, they didn't yet implement the Java console.)
One more problem. You are overloading not actually overriding paint (or paintComponent). Add #Override and the compiler will tell you of your mistake:
#Override public void paint(Graphics g, boolean closeDoors) { // wont compile
Seems like you need a course in debugging. At the very least put some System.err.printlns in and check the Java Console.