I have the following code:
public class OpaqueExample {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
OpaqueFrame frame = new OpaqueFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class OpaqueFrame extends JFrame {
private static final long serialVersionUID = 5486007826709615846L;
public OpaqueFrame() {
super("Opacity Demo");
this.setSize(200, 200);
JComponent boxPanel = new BoxComponent(50, 50);
this.add(boxPanel);
}
}
class BoxComponent extends JComponent {
private static final long serialVersionUID = -1935449999922455838L;
public BoxComponent(int x, int y) {
super();
this.setSize(x, y);
this.setLocation(40, 40);
}
public void paintComponent(Graphics g) {
g.setColor(Color.red);
}
}
In simplicity:
a. Created a frame of size 200, 200
b. Created a Box component of size 50,50
c. Set the location of the box component 40, 40 from the top left corner of frame. Box componet is red color
When i run it, i expect to see a smaller red box in the frame container. Did i get this right or i just don't understand the basic of swing component (seems to be the case).
Please help. Thank.
There isn't enough context to provide a complete answer.
This is simply another possible solution to your problem....
If your intention is to try and place a component at a specific position, then why not just set its background color instead of trying to fill it using custom painting?
If you intention is to paint lots of little squares, then you don't need a separate component for each...
Swing is designed to utilise the layout manager API, it's at the core of how the framework works.
While I'll be the first to admit there are times a null
layout is useful, I'd try a lot of things first (personally).
The following example uses a single component, but allows you to draw multiple boxes onto at varying positions and sizes...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class GraphicsExample {
public static void main(String[] args) {
new GraphicsExample();
}
public GraphicsExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TestPane tp = new TestPane();
tp.add(50, 50, 40, 40);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Rectangle> boxes;
public TestPane() {
boxes = new ArrayList<Rectangle>(25);
}
public void add(int x, int y, int width, int height) {
boxes.add(new Rectangle(x, y, width, height));
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (Rectangle box : boxes) {
g2d.fill(box);
}
g2d.dispose();
}
}
}
One of the other things you are doing wrong is not honouring the paint chain. When you override one of the paint
methods, you must call super.paintXxx
to ensure that the paint chain isn't broken. This methods do a lot of important work and are very unforgiving if you forget to include them ;)
Check out Performing Custom Painting for more details
frame.setLayout(null);
or
setLayout(null);
in the frame's constructor
This allows you to define the position of components directly
public void paintComponent(Graphics g) {
g.setColor(Color.red);
}
This should be changed to
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
}
Info on the graphics class: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.