Reputation: 772
I want to paint Graphics on JPanel. Right now, what I am doing is, I use the paintComponent method to define the drawing:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(10,10,100,100);
}
Then I call repaint() wherever I want to put the graphics.
But I wonder if there is a way to add Graphics to JPanel just like adding components without using this paintComponent method: panel.add(myComponent). I saw that Graphics type cannot be initiated, but maybe there might be another type to let me do that.
I'm pretty sure lots of GUIs such as FANG Engine have this option, but all the examples I saw with Swing was using this method. Any suggestions to do without this? Because it is messing up with the overall design of my program sometimes.
Thanks in advance.
Upvotes: 1
Views: 7842
Reputation: 231
An idea would be to put every custom painting inside an individual JPanel, then add this.
public class MyFrame extends JFrame {
//initialize frame...
//put your components
//now put your jpanel
MyPanel custom = new MyPanel();
custom.setBounds(10, 10, 100, 100);
this.add(custom);
}
public class MyPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//draw your stuff...
}
}
Of course, this would make it a lot less dynamic...
EDIT:
Talking about separation, if you want to separate your screen in e.g. two parts: One for the custom painting and one for the components, for example for a game, then this solution should fit your needs...
Upvotes: 1
Reputation: 109815
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
JPanel viewportPanelTop = new JPanel();
JPanel viewportPanelBottom = new JPanel();
JPanel viewportPanelCenter = new JPanel();
viewportPanelCenter.setBackground(Color.black);
viewportPanelCenter.setPreferredSize(new Dimension(600, 400));
JPanel viewportPanelWest = new JPanel();
JPanel viewportPanelEast = new JPanel();
JFrame frame = new JFrame();
frame.setTitle("JFrame");
frame.setLocation(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(viewportPanelTop, BorderLayout.NORTH);
frame.add(viewportPanelWest, BorderLayout.WEST);
frame.add(viewportPanelCenter, BorderLayout.CENTER);
frame.add(viewportPanelEast, BorderLayout.EAST);
frame.add(viewportPanelBottom, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
private Main() {
}
}
Upvotes: 0
Reputation: 686
You can take a look at the image and buffered image apis. You can generate and handle buffered images in a lot of ways other than the paintcomponent and graphics g. I guess if you generate buffered images and then load them on your JPanel you will be able to get a similar effect. You can use the setRGB method to paint what you want. It's not as versatile as the paint method but it is an alternative.
Upvotes: 3
Reputation: 40811
The easiest way would be to create a custom Icon object, which is in charge of your painting. You can then add your icon to your panel via a JLabel (or put it on a button or any other component that takes icons), while abstracting out the painting work.
Upvotes: 4