A color and draw error

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4808 Views - Last Post: 02 March 2012 - 12:35 PM Rate Topic: -----

#1 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

A color and draw error

Posted 29 February 2012 - 08:08 AM

I have been trying to see how this code works but it keeps giving me errors on the rectangle's color and size and it will not draw it. I have tried to change some variables and played around with the color. Any advice will be appreciated.

import javax.swing.JFrame;
public class Building
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Building");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new BuildingPanel());
frame.pack();
frame.setVisible(true);
}
}




//Building Panel
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class BuildingPanel extends JPanel
{
private Rectangle rec1, rec2, rec3, rec4;
//number of windows
private final int NUM_BOXES = 10;
//set up boundaries of windows, so the windows would not go out of walls
private final int boundary = 3;
private final int X = 40-boundary*2, Y = 190-boundary*2;
private final int X2 = 50-boundary*2, Y2 = 170-boundary*2;
private final int X3 = 70-boundary*2, Y3 = 215-boundary*2;
private final int X4 = 90-boundary*2, Y4 = 283-boundary*2;
//Define Randomness
private Random generator;

public BuildingPanel()
{
generator = new Random();
//width,height,x,y
rec1 = new Rectangle (40,190,Color.black,20,210);
rec2 = new Rectangle (50,170,Color.black,90,230);
rec3 = new Rectangle (70,215,Color.black,170,185);
rec4 = new Rectangle (90,283,Color.black,310,117);

setPreferredSize (new Dimension (400,400));
setBackground (Color.blue);
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);

int x,y,width,height;
//set up window size
int windows = 3;

rec1.draw(page);
rec2.draw(page);
rec3.draw(page);
rec4.draw(page);
//set up windows boundaries 
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X)+ 20 + boundary;
y = generator.nextInt(Y)+ 210 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X2)+ 90 + boundary;
y = generator.nextInt(Y2)+ 230 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X3)+ 170 + boundary;
y = generator.nextInt(Y3)+ 185 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES+10; count++)
{
x = generator.nextInt(X4)+ 310 + boundary;
y = generator.nextInt(Y4)+ 117 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}

}
}




Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: A color and draw error

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: A color and draw error

Posted 29 February 2012 - 08:24 AM

Post your errors. Have you consulted the API to see if you're using the tools correctly?
Was This Post Helpful? 0
  • +
  • -

#3 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: A color and draw error

Posted 29 February 2012 - 08:35 AM

These are my errors from the API:

at BuildingPanel.<init>(BuildingPanel.java:24)
at Building.main(Building.java:9)

I use eclipse. In your opinion, is there a better API.

I am guessing init means initiation of line 24. I do not understand that. If I take the rectangle dimensions out of one line , the error drops to the next line
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: A color and draw error

Posted 29 February 2012 - 08:40 AM

I suspect you didn't include the entire error listing. Copy and paste it into your next post. They appear best if they are also put into code tags so that the minimal formatting is preserved.

Eclipse is an editor, a fancy one, so fancy that it can be called an IDE for Integrated Development Environment. In my opinion, Eclipse is superb, but you will find many others with contrary opinions.

What I meant by API is the Java programmer's reference. Here's the home page for Java 7 API, but you can usually drill down right to the page you want by Googling something like "java color class" and following one of the top 2 or 3 results.
Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: A color and draw error

Posted 29 February 2012 - 08:47 AM

Here's what I get when I try to run/compile your code, also from Eclipse:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The method draw(Graphics) is undefined for the type Rectangle
	The method draw(Graphics) is undefined for the type Rectangle
	The method draw(Graphics) is undefined for the type Rectangle
	The method draw(Graphics) is undefined for the type Rectangle

	at BuildingPanel.<init>(BuildingPanel.java:23)
	at Building.main(Building.java:9)

Further, Eclipse's syntax checker clearly shows that there are problems with lines 23 - 26 of your BuildingPanel class. There are error indicators and red underlines, and if you hover your mouse over the red underlines, Eclipse will report the reason for the error indications. Similarly for lines 39 - 42.

If you're going to use a powerful tool like Eclipse, you should learn how to use it so that you can identify and correct these syntax errors yourself.
Was This Post Helpful? 1
  • +
  • -

#6 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: A color and draw error

Posted 29 February 2012 - 08:51 AM

Sorry I misunderstood you about the API. Here is the error it is showing me.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The constructor Rectangle(int, int, Color, int, int) is undefined
	The method draw(Graphics) is undefined for the type Rectangle
	The method draw(Graphics) is undefined for the type Rectangle
	The method draw(Graphics) is undefined for the type Rectangle
	The method draw(Graphics) is undefined for the type Rectangle

	at BuildingPanel.<init>(BuildingPanel.java:24)
	at Building.main(Building.java:9)



I looked in my main and I see the BuildingPanel Class there.
Was This Post Helpful? 0
  • +
  • -

#7 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: A color and draw error

Posted 29 February 2012 - 09:04 AM

Do i need to make a rectangle class?

I tried to do the suggestions that eclipse gave but it did not work.
Was This Post Helpful? 0
  • +
  • -

#8 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: A color and draw error

Posted 29 February 2012 - 09:07 AM

What was the suggestion? What did you try?
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: A color and draw error

Posted 29 February 2012 - 09:11 AM

To Mylo's point, always post your updated code.

Of course you don't create your own Rectangle class, but this is what you should do:

You have two errors repeated multiple times:

You asked for a Rectangle constructor that doesn't exist and you then tried to use a method that doesn't exist. This is how you fix it:

Go to the API: try Googling "java rectangle" and pick the top result that points to an API page (you'll figure out which ones those are with a little practice), view the available constructors and methods on that page, and fix your code so that you're using constructors and methods that exist.
Was This Post Helpful? 1
  • +
  • -

#10 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: A color and draw error

Posted 29 February 2012 - 09:26 AM

Attaching javadoc to eclipse is an invaluable asset too.

Find any class name in your code (that isn't yours), then ctrl click it, you should get an option to attach javadoc. Click the button to open a file dialog, go to your program files -> java -> jdk and attach the src.zip file.

Now you can ctrl click anything and instantly open the code for the class.
Was This Post Helpful? 0
  • +
  • -

#11 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: A color and draw error

Posted 29 February 2012 - 09:31 AM

I searched and found the draw rectangle constructor and added it in. I still was not able to get the draw method to work.

//Building Panel
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class BuildingPanel extends JPanel
{
public Rectangle rec1, rec2, rec3, rec4;

//number of windows
private final int NUM_BOXES = 10;
//set up boundaries of windows, so the windows would not go out of walls
private final int boundary = 3;
private final int X = 40-boundary*2, Y = 190-boundary*2;
private final int X2 = 50-boundary*2, Y2 = 170-boundary*2;
private final int X3 = 70-boundary*2, Y3 = 215-boundary*2;
private final int X4 = 90-boundary*2, Y4 = 283-boundary*2;
//Define Randomness
private Random generator;

public BuildingPanel() {
}
void Rectangle(int x, int y, int width, int height) {
}
{
generator = new Random();
//width,height,x,y
rec1 = new Rectangle (40,190,20,210);
rec2 = new Rectangle (50,170,90,230);
rec3 = new Rectangle (70,215,170,185);
rec4 = new Rectangle (90,283,310,117);
setRectangle (Color.black);
setPreferredSize (new Dimension (400,400));
setBackground (Color.blue);
}

	

public void paintComponent (Graphics page)
{
super.paintComponent(page);

int x,y,width,height;
//set up window size
int windows = 3;
rec1.draw(page);
rec2.draw(page);
rec3.draw(page);
rec4.draw(page);

//set up windows boundaries 
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X)+ 20 + boundary;
y = generator.nextInt(Y)+ 210 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X2)+ 90 + boundary;
y = generator.nextInt(Y2)+ 230 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X3)+ 170 + boundary;
y = generator.nextInt(Y3)+ 185 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES+10; count++)
{
x = generator.nextInt(X4)+ 310 + boundary;
y = generator.nextInt(Y4)+ 117 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}

}
private void setRectangle(Color black) {
	// TODO Auto-generated method stub
	
}
}


Was This Post Helpful? 0
  • +
  • -

#12 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: A color and draw error

Posted 29 February 2012 - 09:39 AM

Mylo,
How do I attach the zip to eclipse. I found it in the jdk file.
Was This Post Helpful? 0
  • +
  • -

#13 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: A color and draw error

Posted 29 February 2012 - 09:51 AM

I don't know the actual way though preferences. But going by your current code... ctrl + click on JPanel near extends Jpanel, this should open another tab with a button to attach it.

As for your problem, you have messed up your parenthesis under you draw rectangle method. I'll leave it as an exercise for you to use the javadoc to identify the incorrect methods though, as GregBrannon is trying to do.
Was This Post Helpful? 0
  • +
  • -

#14 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: A color and draw error

Posted 29 February 2012 - 10:06 AM

I add the zip but ctrl+click does not work.
Was This Post Helpful? 0
  • +
  • -

#15 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: A color and draw error

Posted 29 February 2012 - 10:14 AM

Try click on JPanel, then press F3
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2