4 Replies - 463 Views - Last Post: 01 February 2013 - 07:50 PM Rate Topic: -----

#1 rashtiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 12

simple graphic programming

Posted 31 January 2013 - 06:26 PM

The issue is that when i compile(which it does without errors) and run the program the java pops out a window that shows just white. Could someone see what i am missing in my code? i have really tried to debug it, but without
success. cheers
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class MyPanel extends JPanel
{
	
	int numCols;
	int numRows;
	
	public static void main(String[] args)
	{
		W1Graphics w = new W1Graphics();
		w.setVisible(true);
	}

	public MyPanel()
	{
		numCols = 6;
		numRows = 8;
		
	}


	public void paint(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		// above: this "upgrades" the Graphics class to a Graphics2D class:
		// this has some extra methods that we'll use later on

		int w = getWidth();
		int h = getHeight();

	
		
		Rectangle r1 = new Rectangle(0,0,w,h);
		Rectangle r2 = getRect(2,8);
		Rectangle r3 = getRect(1,3);
		Rectangle r4 = getRect(3,1);
		Rectangle r5 = getRect(8,2);
		
		g2.setColor(Color.red);

		g2.fill(r1);
		
		g2.setColor(Color.yellow);
		
		g2.fillOval(r2.x, r2.y, r2.width, r2.height);
		g2.fillOval(r3.x, r3.y, r3.width, r3.height);
		
		g2.setColor(Color.green);
		g2.fillOval(r4.x, r4.y, r4.width, r4.height);
		g2.fillOval(r5.x, r5.y, r5.width, r5.height);
		

		System.out.println(w + h);

	}
	
	Rectangle getRect(int thisCol, int thisRow)
	{
		if (thisCol < numCols && thisRow < numRows)
		{
			
			int r_w = getWidth()/numCols;
			int r_h = getHeight()/numRows;
			int r_x = thisCol * r_w;
			int r_y = thisRow * r_h;
			Rectangle r = new Rectangle(r_x, r_y, r_w, r_h);
            
			return r;


		}
		
		else
		{
			return null;
		}
	}

} 


// main program

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;


public class W1Graphics extends JFrame
{
	MyPanel myVeryOwnPanel;

	public static void main(String[] args)
	{
		W1Graphics w = new W1Graphics();
		w.setVisible(true);
	}

	public W1Graphics()
	{
		setTitle("Kaveh Bakhshalizadeh Rashti");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,220);
		setLocation(300,300);
		myVeryOwnPanel = new MyPanel();
		add(myVeryOwnPanel);
 	}

 }


This post has been edited by macosxnerd101: 31 January 2013 - 07:01 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: simple graphic programming

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: simple graphic programming

Posted 31 January 2013 - 08:12 PM

		Rectangle r2 = getRect(2,8);
		Rectangle r3 = getRect(1,3);
		Rectangle r4 = getRect(3,1);
		Rectangle r5 = getRect(8,2);



There is a problem with your getRect(). Comment thse out and it works.
Was This Post Helpful? 0
  • +
  • -

#3 rashtiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 12

Re: simple graphic programming

Posted 01 February 2013 - 03:12 AM

i did, but now i get tons of errors saying cannot find symbol.
I still think it's something easy to miss, but can's seem to find out what...
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: simple graphic programming

Posted 01 February 2013 - 07:22 AM

Look at your getRect code. The problem is there. Why don't you just create Rectangle objects the normal way like you did here? Not that what you did was bad, but there is comething wrong with your calculations, so getRect() seems to be returning retarded results. :)

I would rather just do:

Rectangle r1 = new Rectangle(0,0,(w/nuCols),(h/numRows));



Or you could just go over the math in getRect() and fix whatever is happening. I won't look at it, becuse I hate other people's math, regardless of how simple it may be.

If you remove every r2. through r5. call you will see that the app works.

This post has been edited by farrell2k: 01 February 2013 - 07:24 AM

Was This Post Helpful? 1
  • +
  • -

#5 lbfb  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 23-February 12

Re: simple graphic programming

Posted 01 February 2013 - 07:50 PM

I am not a Java programmer, but looking at the code for your getRect method, I can see two possible problems:

1. dividing the width of the window by the number of columns you have set will not always give an integer (I don't know if Java automatically rounds this figure or gives an error)

2. the calculation you use to set the x and y co-ordinates for the top left corner of the rectangle can give results that are outside the actual window.

Also, in your getRect method, if the values you are passing in are not less than the numRow/numCol values, then you will return null. If you then try to access the x and y values for that non-existent rectangle (i.e. r2 and r5), you will probably run into problems.

Hope this helps a bit at least.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1