Reputation: 535
I am trying to paint one Oval over another oval. Here i use a select statement to draw in the paint component method.
import java.awt.*;
public class test extends JPanel{
public static final int OVAL = 1;
public static final int SOLID = 2;
private int type = LINE;
public test(int type){
this.type = type;
}
public void piantComponent(Graphics g){
super.paintComponent(g);
switch(type)
{
case OVAL:
g.setColor(Color.YELLOW);
g.drawOval(0,0,150,150);
break;
case SOLID:
g.setColor(Color.BLUE);
g.fillOval(0,0,50,50);
break;
}
}
}
Now in my main method i want to display a solid blue oval (SOLID) inside of a yellow oval (OVAL).
import...;
public class Main{
public static void main (String [] args){
JFrame window = new JFrame();
window.add(new test(test.OVAL));
window.add(new test(test.SOLID));
window.setSize(300,300);
window.setVisible(true);
}
}
This does not do what I want it to do at all. This only displays a oval and not a oval and a solid. I think that i am overloading the window to only display the oval. I have tried to display with a layout manager(gridlayout) but that does not display the two paintings over each other it displays the two paintings next to each other.
How do i fix this without loosing the switch statement.
Upvotes: 2
Views: 3197
Reputation: 324128
window.add(new test(test.OVAL));
window.add(new test(test.SOLID));
Your problem has to do with ZOrder. To simplify, Swing paints components in the reverse order they are found on the panel. So in your case the SOLID gets painted BEFORE the OVAL. Since the OVAL is larger in size it covers the SOLID.
You can force Swing to paint the components in the more intuitive order by doing:
window.add(0, new test(test.OVAL));
window.add(0, new test(test.SOLID));
Now the last component added will also be painted last.
However a better design is to create separate Oval and Solid classes so you can specify size, location and color independently. Then you can add as many components to your panel as you want.
Upvotes: 1
Reputation: 11
import java.awt.*;
public class test extends JPanel {
public static final int OVAL = 1;
public static final int SOLID = 2;
**public static final int BOTH = 3;** //instance variable to represent the
//painting of both images
private static int type;
public test(int type){
this.type = type;
}
public void paintComponent(Graphics g){
switch(type)
{
case OVAL:
g.setColor(Color.BLACK);
g.drawOval(0,0,150,150);
break;
case SOLID:
g.setColor(Color.BLUE);
g.fillOval(0,0,50,50);
break;
case BOTH: //statements to draw both figures
g.setColor(Color.BLACK);
g.drawOval(0,0,150,150);
g.setColor(Color.BLUE);
g.fillOval(0,0,50,50);
break;
}
}//paint component
}
in the main method use
window.add( new test(test.BOTH);
Upvotes: 1
Reputation: 109813
Painting in Swing must be done for concrete JComponent
public class test{
(use proper naming convention and to change test
to Text
) doesn't contains any JComponents
use JPanel
or plain JComponent
painting in AWT, Swing by default never returns PreferredSize
, then container has zero Dimension
, required to override getPreferredSize
Upvotes: 1
Reputation: 478
Try something like this (Just using snippets):
private ArrayList<int> ovals = new ArrayList<int>();
public test(int type) {
ovals.add(type);
}
public void piantComponent(Graphics g)
{
super.paintComponent(g);
for(int oval : ovals)
{
switch(type)
{
case OVAL:
g.setColor(Color.YELLOW);
g.drawOval(0,0,150,150);
break;
case SOLID:
g.setColor(Color.BLUE);
g.fillOval(0,0,50,50);
break;
}
}
}
This will allow you to add as many ovals as you want (although with their static positioning it will just write over them n the canvas). You could try using a boolean array and iterating through that to see if you need to paint a specific type if you only want 1 of each.
Upvotes: 0
Reputation: 2540
Why don't you just paint one right after the other? Like that:
g.setColor(Color.YELLOW);
g.drawOval(0,0,150,150);
g.setColor(Color.BLUE);
g.fillOval(0,0,50,50);
Upvotes: 0