drawing problem with layered panes

C

ClubSoda

Here's my problem:

I added a layered pane to my main frame.

In this layered pane I added 2 panels. (panel1 & panel2)

To panel1 I addded an objec derived from a class with a paint(Graphics
g) method.
Upon adding/compiling/running the things to paint in this paint class
were painted and visible.

Afterwards I added another object to panel1 also derived from a
(different) class with a paint(Graphics g) method.

Now I wanted both panels to be visible in the same range (since panel2
only had a few lines to draw and panel1 was more of a guide to draw)
so I set panel1 to background and made panel2 opaque(false)

Problem now is that the things that were supposed to be drawn by
panel2 are no longer visible...

If anybody can help me with this. (code: see below)


Code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;

public class GUI extends JFrame
{
private JLayeredPane gamePane = new JLayeredPane();
private JPanel gamePanel = new JPanel();
private JPanel mazePanel = new JPanel();

public GUI(String name, int difficulty)
{
super(name);
Container c = this.getContentPane();

gamePane.setBounds(0,0,500,500);

gamePanel.setLayout(null);
gamePanel.setBackground(new Color(0,204,0));
gamePanel.setBounds(0,0,500,500);

mazePanel.setBounds(0,0,500,500);
//mazePanel.setLayout(null);
//mazePanel.setOpaque(false);
//mazePanel.setBackground(Color.BLACK);

Raster grid = new Raster(difficulty, 500, 500);
grid.setBounds(0,0,500,500);
gamePanel.add(grid);

Maze newMaze = new Maze();
newMaze.setBounds(0,0,500,500);
mazePanel.add(newMaze);

gamePane.add(gamePanel);
gamePane.add(mazePanel);
gamePane.moveToBack(gamePanel);

c.add(gamePane);

this.setLocation(200,100);
this.setSize(710, 660);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}


// new class (this one draws)
import java.awt.*;
import javax.swing.*;
import java.util.Vector;

public class Raster extends JComponent
{
private int x1, y1, x2, y2;
private int size;
private int maxX, maxY;
private Vector points = new Vector(0);

public Raster(int size, int maxX, int maxY)
{
this.size = size;
this.maxX = maxX;
this.maxY = maxY;
createPoints();
}

public void createPoints()
{
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
{
int xCoordinate = i * size;
int yCoordinate = j * size;
Point newPoint = new Point(xCoordinate, yCoordinate);
points.addElement(newPoint);
}
}
}

public void paint(Graphics g)
{
for (int i = 0; i < 25; i++)
{
g.setColor(new Color(204,0,0));
x1 = size * i;
x2 = size * i;
y1 = 0;
y2 = maxY;
g.drawLine(x1, y1, x2, y2);
x1 = 0;
x2 = maxX;
y1 = size * i;
y2 = size * i;
g.drawLine(x1, y1, x2, y2);
}
for (int i = 0; i < points.size(); i++)
{
g.setColor(new Color(0,255,0));
Point thisPoint = (Point)points.get(i);
g.fillOval(thisPoint.getX() - 2, thisPoint.getY() - 2,5,5);
}
}

public Vector getPoints() { return points; }
}


// new class (this one doesn't draw)
import java.awt.*;
import javax.swing.*;
import java.util.Vector;

public class Maze extends JComponent
{
private Vector maze = new Vector(0,1);

public Maze()
{
Line line1 = new Line(0, 0, 500, 0);
// Line is another class, works like a charm... (line = 4 points,
x1, x2,y1,y2...)
maze.addElement(line1);
Line line2 = new Line(0, 0, 0, 500);
maze.addElement(line2);
Line line3 = new Line(0, 500, 500, 500);
maze.addElement(line3);
Line line4 = new Line(500, 0, 500, 500);
maze.addElement(line4);
}

public void paint(Graphics g)
{
for (int i = 0; i < maze.size(); i++)
{
g.setColor(new Color(0,255,0));
Line thisLine = (Line)maze.get(i);
g.drawLine(thisLine.getX1(), thisLine.getY1(),
thisLine.getX2(), thisLine.getY2());
}
g.drawRect(20,20,50,50);
}

public void setMaze(Vector maze)
{
this.maze = maze;
}

public Vector getMaze() { return maze; }
}
 
A

Andrew Thompson

I added a layered pane to my main frame.

A better group is..
<http://www.physci.org/codes/javafaq.jsp#cljg>
...but please do not simply post this question
again there. There are better ways to do it.
....
To panel1 I addded an objec derived from a class with a paint(Graphics
g) method.

Whoa up now, I suspect that may
be the root of the problem..
<http://www.physci.org/guifaq.jsp#2.4>

[ Have a good look through that GUI FAQ
while you are there, plenty of good tips. ]

...and can you come up with a better
name for your GUI than, well ..GUI?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top