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
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);
}
}