I have a problem with full-screen : i create a frame and put it in a full-screen Window but i see nothing but the color of the frame's background.
here is the code i used:
PB frame = new PB();
win = new Window(frame);
gs.setFullScreenWindow(win);
frame.setVisible(true);
frame.repaint();
win.repaint();
and the PB class, my frame:
public class PB extends JFrame
{
PB()
{
super();
this.setBackground(Color.BLUE);
this.getContentPane().add(new JButton("button"));
JPanel jp = new JPanel();
jp.setBackground(Color.red);
jp.setSize(360, 200);
this.getContentPane().add(jp);
this.setVisible(true);
repaint();
pack();
}
#Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new Color(0,0,0));
g.fillRect(0,0,200,200);
}
}
So all i can see, is a big screen with the background color (here blue);
Thanks for all help
I bet you didn't try your frame separately, did you?
This part of frame code:
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new Color(0,0,0));
g.fillRect(0,0,200,200);
}
will never let it paint its own content, just the colored rect.
So my recomendations are:
Do not override JFrame's paint method - it will cause a lot of problems
Just set main container panel background to the desired color
Try the frame without full-screen window first to see if it displays what you need
I think you're mis-understading the purpose of the "owner" for the Window.
JFrame extends from Window.
So, instead of saying
win = new Window(frame);
gs.setFullScreenWindow(win);
You only need to use
gs.setFullScreenWindow(frame);
Oh, and what Mikle said as well.
Related
I'm working on a project and I just started on the GUI. Since this isn't my most favorite topic, I stumbled real quick on something not working quite right. Everything (PacmanGrid,PacmanScore) is shown correctly but the borders I wrote for the PacmanScore Panel! Anyway here is the code, hope someone can help.
public class PacmanFrame extends JFrame{
public PacmanFrame() {
this.setLayout(new BorderLayout());
this.setTitle("Pacman");
PacmanGrid p1=new PacmanGrid();
PacmanScore p2 = new PacmanScore();
this.add(p1,BorderLayout.CENTER);
this.add(p2,BorderLayout.EAST);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
super.repaint();
pack();
super.setVisible(true);
}
public static void main(String[] args) {
PacmanFrame p1 = new PacmanFrame();
}
}
PacmanScore
public class PacmanScore extends JPanel{
private TitledBorder t3 = BorderFactory.createTitledBorder("Menu");
private Border etched = BorderFactory.createEtchedBorder(Color.WHITE, Color.white);
public PacmanScore() {
setLayout(new FlowLayout());
setPreferredSize(new Dimension(100,800));
setBackground(Color.DARK_GRAY);
t3.setBorder(etched);
setBorder(t3);
setVisible(true);
setOpaque(true);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
g2.setColor(Color.white);
g2.drawString("Score: ", 20, 400);
}
}
PacmanGrid is also extended by a Panel and draws the classical PacmanGrid using predefined patterns. But I don't think it is relevant since the problem is clearly on the PacmanScore Panel. I will post the code if anyone needs tho.
Thanks in Advance!
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
g2.setColor(Color.white);
g2.drawString("Score: ", 20, 400);
}
You didn't override paint() properly because you didn't invoke super.paint() and therefore the border is not painted.
Don't override paint(). Custom painting is done by overriding paintComponent().
Read the section from the Swing tutorial on A Closer Look at the Paint Mechanism for more information.
Why are you even doing custom painting? Just add a JLabel to the panel.
Also, Swing components (except for top level windows) are visible by default so there is no need to make the panel visible.
I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen.
Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
1) you have three general issues
never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)
your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater() meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
hey i had he same problem and the screen turns black every time i run the program.
in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.
my goal is to draw some bufferedimage onto another. then all this stuff draw onto some other bufferedimage and so on. And finally draw this on top of a panel.
For now i'm trying to draw bufferedimage onto panel and nothing works. My bufferedimage looks completely white:
public class Main2 {
public static void main(String[] args) {
JFrame frame = new JFrame("asdf");
final JPanel panel = (JPanel) frame.getContentPane();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
somepaint(panel);
}
});
}
private static void somepaint(JPanel panel) {
BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
image.getGraphics().setColor(Color.red);
image.getGraphics().fillRect(0, 0, 200, 200);
Graphics2D graphics = (Graphics2D) panel.getGraphics();
graphics.setColor(Color.magenta);
graphics.fillRect(0, 0, 500, 500);
graphics.drawImage(image, null, 0, 0); // draws white square instead of red one
}
}
thanks
Re:
private static void somepaint(JPanel panel) {
BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
image.getGraphics().setColor(Color.red);
image.getGraphics().fillRect(0, 0, 200, 200);
Graphics2D graphics = (Graphics2D) panel.getGraphics();
This is not how you draw inside of a JPanel or JComponent.
Don't call getGraphics() on a component as the Graphics object returned will be short-lived, and anything drawn with it will not persist. Instead do your JPanel's drawing inside of its paintComponent(Graphics G) method override. You will need to create a class that extends JPanel in order to override paintComponent(...).
Most importantly, to see how to do Swing graphics correctly, don't guess. You'll want to read the Swing Graphics Tutorials first as it will require you to toss out some incorrect assumptions (I know that this is what I had to do to get it right).
You need to rectify your parameters in the drawImage() call. Change this:
graphics.drawImage(image, null, 0, 0);
to
graphics.drawImage(image, 0, 0,null);
Check the Java docs for more details.
I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen.
Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
1) you have three general issues
never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)
your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater() meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
hey i had he same problem and the screen turns black every time i run the program.
in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.
How can I tell the paint method to draw background on JPanel only and not on the entire JFrame.
My JFrame size is bigger than the JPanel. When I try to paint a grid background for the JPanel, the grid seems to be painted all over the JFrame instead of just the JPanel.
Here parts of the code:
public class Drawing extends JFrame {
JPanel drawingPanel;
...........
public Drawing (){
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(600,600));
}
public void paint(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2); //call a METHOD to paint the for JPANEL
}
private void paintBackground(Graphics2D g2)
{
g2.setPaint(Color.GRAY);
for (int i = 0; i < drawingPanel.getSize().width; i += 300)
{
Shape line = new Line2D.Float(i, 0, i, drawingPanel.getSize().height);
g2.draw(line);
}
for (int i = 0; i < drawingPanel.getSize().height; i += 300)
{
Shape line = new Line2D.Float(0, i, drawingPanel.getSize().width, i);
g2.draw(line);
}
} //END private void paintBackground(Graphics2D g2)
}
If you want to do painting on the JPanel then override the JPanel, not the JFrame.
You should be overriding the paintComponent() method of JPanel. Read the section from the Swing tutorial on Custom Painting for a working example.
camickr is correct. So:
public class Drawing extends JFrame {
JPanel drawingPanel;
...........
public Drawing (){
drawingPanel = new MyPanel();
drawingPanel.setPreferredSize(new Dimension(600,600));
add(drawingPanel);
}
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
myBackgroundRoutine(g2);
}
}
You need to strictly separate your drawing from different components. Swing is already
managing subcomponents, so there is absolutely no need to implement drawings in your
Panel in the Frame (calling paintComponents() is a severe error).
And you should never override paint(), because only paintComponent()
is used in Swing. Don't mix both until you absolutely know what you are doing.
super.paintComponents(g);
I would suggest as your first point of investigation.
The code you posted is not complete, it's missing how the panel is added to the JFrame and which LayoutManager is being used.
The code seams to be correct. Are you sure the JPanel is not occupying the whole JFrame? Add a System.out.println(drawingPanel.getSize()) to check this.
If you are using the BorderLayout, the default for JFrame, and has just added the panel without any constraint, the panel will use the whole area. The PreferredSize is ignored.
Try this, just for testing:
public Drawing (){
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(600,600)); // ignored
drawingPanel.setBounds(0, 0, 600, 600); // location and size
setLayout(null);
add(drawingPanel);
}
but IMO this is not the best or correct way to do it. I would prefer to override the paintComponent() method from the JPanel, as suggested by Thorsten and camickr.
But it will still use the whole area of the JFrame until other Component is added to the JFrame or the LayoutManager changed.
You should override the JPanel, not the JFrame to do painting. You can override the paintComponent() method of the JPanel