public class TerrisView extends JFrame {
public Canvas canvas;
public TerrisView(String title) {
super(title);
canvas = new Canvas();
canvas.setSize(300, 400);
canvas.setBackground(Color.WHITE);
// setSize(300, 400);
this.add(canvas, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
paint();
}
public void paint() {
// this.createBufferStrategy(2);
// Graphics gp=getBufferStrategy().getDrawGraphics();
// gp.setColor(Color.RED);
// gp.fillRect(100, 100, 50, 50);
// getBufferStrategy().show();
Graphics gp = canvas.getGraphics();
gp.setColor(Color.BLACK);
gp.fillRect(0, 0, 10, 10);
}
}
Why does it fail to draw the Rect on the canvas? What is wrong with the code?
Don't mix Swing with AWT components without good reason.
In this case:
Extend JComponent instead of Canvas
Override paintComponent(Graphics) instead of paint(Graphics)
Before I formatted that code, I failed to notice that paint() was not an override, and this classic..
Graphics gp = canvas.getGraphics();
Don't do that with components. Use the Graphics object that is passed to the methods mentioned in point 2, and paint when told to do so.
This answer is already accepted, but I could not resist reviewing and cleaning up the source to make an SSCCE, as well as add a few more tweaks. See comments in code for further tips.
import java.awt.*;
import javax.swing.*;
public class TerrisView {
public JComponent canvas;
public TerrisView(String title) {
// Don't extend frame, just use one
JFrame f = new JFrame(title);
canvas = new JComponent() {
#Override // check this is a real method
public void paintComponent(Graphics g) {
super.paintComponent(g);
// paint the BG - automatic for a JPanel
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLACK);
// make it dynamic, changing with the size
int pad = 10;
g.fillRect(pad, pad, getWidth()-(2*pad), getHeight()-(2*pad));
}
};
// layout managers are more likely to respect the preferred size
canvas.setPreferredSize(new Dimension(300, 400));
canvas.setBackground(Color.ORANGE);
f.add(canvas, BorderLayout.CENTER);
f.pack();
// nice tweak
f.setMinimumSize(f.getSize());
// see http://stackoverflow.com/a/7143398/418556
f.setLocationByPlatform(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
// start/alter Swing GUIs on the EDT
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TerrisView("Terris View");
}
});
}
}
Related
So I am new in java graphics and I am creating a program that will show a rectangle. But when I run my program it only show like a small box and not the rectangle. I don't really know why it is happening.
Here is my code:
import javax.swing.*;
public class GraphicsEditor{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Rectangle rectangle = new Rectangle();
frame.setSize(1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
panel.add(rectangle);
}
}
This is my rectangle class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Rectangle extends JPanel implements Shape {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.fillRect(0, 0, 200, 130);
}
}
This is my shape interface:
import java.awt.*;
public interface Shape {
void paintComponent(Graphics g);
}
Here, try this
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class GraphicsEditor {
public static void main(String[] args) {
JFrame frame = new JFrame();
Rectangle rectangle = new Rectangle();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(rectangle);
frame.pack();
// center frame on screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class Rectangle extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.fillRect(0, 0, 200, 130);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}
A couple of things.
you don't need the interface.
unlike components, just painting a picture doesn't affect the layout manager, so the panel will be reduced to it's default size with out regard to any painting.
so you need to override getPreferredSize() in your JPanel.
As the comments said, you should set the preferred size of both your panel and rectangle to your desired size, and then pack the frame, like:
panel.setPreferredSize(new Dimension(500,500));
rectangle.setPreferredSize(new Dimension(500,500));
frame.pack();
Otherwise your LayoutManager (when not specified it defaults to FlowLayout) will handle your rectangle the way it wants. So another way would be learning about Layout Managers, and using your desired one.
As a side note, I would like to make some suggestions to your code. Remember, Swing is not thread safe, so place your code inside an invokeLater() call, such as:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Rectangle rectangle = new Rectangle();
frame.setSize(1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(500,500));
rectangle.setPreferredSize(new Dimension(500,500));
frame.add(panel);
panel.add(rectangle);
frame.pack();
frame.setVisible(true);
}
});
Also, calling frame.setVisible(true) should be called after adding your components.
I am creating a Video Player using VLCJ.
The player uses a Canvas as it's video surface.
videoPlayer.newVideoSurface(canvas);
So I am bounded to use Canvas.
I want to show message over the Canvas like, "Player Started". I tried using a JLayeredPane and a JLabel to accomplish this.
A short-demo code:
package canvasexample;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class CanvasExample extends JFrame
{
public JLabel label = new JLabel("Message");
public Canvas canvas = new MyCanvas();
public CanvasExample()
{
setSize(500, 500);
setLocationRelativeTo(null);
createWindow();
setVisible(true);
}
public void createWindow()
{
JLayeredPane pane = new JLayeredPane();
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
panel1.setSize(500, 500);
panel2.setSize(500, 500);
label.setBorder(new LineBorder(Color.BLUE));
panel1.add(canvas);
panel2.add(label, BorderLayout.NORTH);
pane.add(panel1, 1, 0);
pane.add(panel2, 2, 0);
add(pane);
}
private class MyCanvas extends Canvas
{
public MyCanvas() {
setBackground (Color.GRAY);
setSize(500, 500);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(75, 75, 150, 75);
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new CanvasExample();
}
});
}
}
But soon I found out that you can't mix heavyweight AWT component and lightweight Swing components.
So, can anyone tell me how can I display a message over a Canvas?
If you are trying to overlay static text on an vlcj embedded canvas you need to use Marquess
Or use the marque options directly in the mediaplayer here
For overlays besides text you can have a look AbstractJWindowOverlayComponent
or I haven't tried this approach u can create an JLayeredPane where you have your canvas at layer 1 and can have your JComponent which will draw your overlays at layer 0 with transparent background you can override it's paintComponent() method and draw whatever you need
I'm trying to draw a basic object to a JPanel
although it doesn't seem to be working.
I'm certain I am doing something wrong with where the paint method
is being called
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class testGui {
static colors gc_colors;
static gui gc_gui;
public static void main(String[] args) {
gc_colors = new colors();
gc_gui = new gui();
gc_gui.cv_frame.setVisible(true);
}
public static class colors {
Color cv_ltGrey;
Color cv_mdGrey;
Color cv_dkGrey;
public colors() {
cv_ltGrey = Color.decode("#DDDDDD");
cv_mdGrey = Color.decode("#CCCCCC");
cv_dkGrey = Color.decode("#111111");
}
}
public static class gui {
JFrame cv_frame;
JPanel cv_panel;
JPanel cv_content;
public gui() {
cv_frame = new JFrame();
cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cv_frame.setTitle("Test GUI");
cv_frame.setSize(600, 400);
cv_frame.setLayout(new FlowLayout());
cv_panel = new JPanel();
cv_panel.setBackground(gc_colors.cv_ltGrey);
cv_panel.setPreferredSize(new Dimension(500, 300));
cv_frame.add(cv_panel);
cv_content = new content();
cv_panel.add(cv_content);
}
}
public static class content extends JPanel {
public void paint(Graphics graphic) {
super.paint(graphic);
draw(graphic);
}
public void update() {
repaint();
}
public void draw(Graphics graphic) {
Graphics2D graphic2D = (Graphics2D) graphic;
graphic2D.setPaint(gc_colors.cv_ltGrey);
graphic2D.fillRect(10, 10, 100, 100);
}
}
}
I have a class for my gui which I am adding a JPanel to (a light grey one).
Which I am then trying to add my drawing to using a JPanel extended class
called content.
When I run it though it seems to create the grey JPanel which I want but
the drawing is just a tiny white square and I'm not sure why.
So, you content panel has a default preferred size of 0x0, FlowLayout honours the preferredSize of its components (with a little margin), hence the reason why you have a nice little small white rectangle.
What you need to do is override the getPreferredSize method of the content panel and return a suitable size, for example
public static class content extends JPanel {
#Override
public Dimension getPreferredSize() {
return new Dimension(120, 120);
}
public void paint(Graphics graphic) {
super.paint(graphic);
draw(graphic);
}
public void update() {
repaint();
}
public void draw(Graphics graphic) {
Graphics2D graphic2D = (Graphics2D) graphic;
graphic2D.setPaint(gc_colors.cv_ltGrey);
graphic2D.fillRect(10, 10, 100, 100);
}
}
I've decided to just leave out the second JPanel altogether.
It was too much of a hassle to put the JPanel inside of another JPanel
so instead I am only going to use a single JPanel
public static class gui {
JFrame cv_frame;
JPanel cv_panel;
JPanel cv_content;
public gui() {
cv_frame = new JFrame();
cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cv_frame.setTitle("Test GUI");
cv_frame.setSize(600, 400);
cv_frame.setLayout(new FlowLayout());
cv_content = new content();
cv_content.setBackground(gc_colors.cv_ltGrey);
cv_content.setPreferredSize(new Dimension(500, 300));
cv_frame.add(cv_content);
}
}
This what I am doing but is nothing is getting displayed on Jframe window. I have not extended class JFrame to do my, is it necessary to do so for displaying objects on window.
public class testGraphics {
static JFrame workingFrame = null;
public static void main(String args[])
{
JFrame workingManager = new JFrame("Hello");
workingManager.setSize(500, 500);
workingManager.setVisible(true);
Graphics g = workingManager.getGraphics();
JPanel jp = (JPanel) workingManager.getContentPane();
workingManager.paintComponents(g);
g.fillOval(0, 0, 30, 30);
g.drawOval(0, 50, 30, 30);
g.setColor(Color.CYAN);
}
}
Do not ever call getGraphics() or explicitly call paintXxx() to do custom painting. The correct way to do custom painting is to override the paintComponent method of the panel to paint on. The overriden method will be implicitly called for you. Then add that panel to the frame. Also you should override the getPreferredSize() of the panel, so it has a preferred size, so you can just pack the frame
class PaintPanel extends JPanel {
#Override
protected paintComponent(Grapchics g) {
super.paintComponent(g);
g.drawString(....);
}
#Override
public Dimension getPreferredSize() {'
return new Dimension(300, 300);
}
}
Then add it to the frame (or if you want to set it as the content pane of the frame, do that instead)
PaintPanel panel = new PaintPaint();
frame.add(panel);
...
frame.pack();
See more at Performing Custom Painting
I made several changes to your code to get it to work properly.
I changed your main method to call the SwingUtilities invokeLater method to make sure that the Swing components were defined and used on the Event Dispatch thread.
I created a drawing JPanel. I set the color first, then drew the ovals.
I added a JFrame default close operation. You must specify a default close operation, or else your Java application will continue running after you close the JFrame.
I moved the size to the drawing panel. The frame size will be calculated when you call the JFrame pack method.
And here's the modified code:
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestGraphics implements Runnable{
private JFrame workingManager;
private JPanel drawingPanel;
#Override
public void run() {
workingManager = new JFrame("Hello");
workingManager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawingPanel = new DrawingPanel();
workingManager.add(drawingPanel, BorderLayout.CENTER);
workingManager.pack();
workingManager.setLocationByPlatform(true);
workingManager.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new TestGraphics());
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID =
-3701718376300985046L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(500, 500));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval(0, 0, 30, 30);
g.drawOval(0, 50, 30, 30);
}
}
}
The setSize() and setVisible() must be at the bottom of the method:
JFrame workingManager = new JFrame("Hello");
Graphics g = workingManager.getGraphics();
JPanel jp = (JPanel) workingManager.getContentPane();
workingManager.paintComponents(g);
g.fillOval(0, 0, 30, 30);
g.drawOval(0, 50, 30, 30);
g.setColor(Color.CYAN);
workingManager.setSize(500, 500);
workingManager.setVisible(true);
This question already has answers here:
Java JFrame background color not working
(4 answers)
Closed 8 years ago.
I can't change the background color of a JFrame, or of a JPanel inside the JFrame, this is my code:
public class MyFrame extends JFrame {
public MyFrame(){
setSize(600,600);
setResizable(false);
panel = new MyPanel();
panel.setBackground(Color.BLACK);
//getContentPane().setBackground(Color.BLACK); doesn't work
getContentPane().add(panel);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(-1);
}
public void windowClosed(WindowEvent ev){
System.exit(-1);
}
});
}
public void paint(Graphics g){
super.paint(g);
g.clearRect(0,0,600,600);
synchronized (this){
if (positions!=null){
for (int i=0; i<positions.length; i++){
P2d p = positions[i];
//int x0 = (int)(180+p.x*180);
//int y0 = (int)(180-p.y*180);
g.drawOval((int)p.x,(int)p.y,5,5);
}
}
}
}
}
i have tried in several ways, but I can not change the color, it's always white, how i can do?
Try this one. Draw a rectangle filled with your background color.
use SwingUtilities.invokeLater() or EventQueue.invokeLater()
use paintComponent() method for custom painting
don't draw on JFrame directly.
use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to close the JFrame
Sample code:
public class MyFrame extends JFrame {
public MyFrame() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
setSize(new Dimension(600, 600));
setResizable(false);
JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 600, 600);
Color prevColor = g.getColor();
g.setColor(Color.BLUE); // background color
g.fillRect(0, 0, 600, 600); // fill a rectangle with background color
g.setColor(prevColor);
// your custom painting
...
}
};
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
});
}
}
Not sure if this will work for you but try
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
or you could try setting the colour in the JFrame and leaving the colour out of the pannel
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MyFirstFrame");
frame.setBackground(Color.BLACK);
frame.setLocation(0,0);
frame.setSize(150,150);
Check out this answer to change background color
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.EventQueue;
public class ColoredFrame {
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame( "TestFrame" );
frame.getContentPane().setBackground( Color.PINK );
//frame contains nothing, so set size
frame.setSize( 200, 200 );
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} );
}
}
Reference of Robin's answer.
As for your MyPanel it looks like you didn't set the size so atm it is not visible.
And for your jFrame since you override paint, why not try setting g.setBackground(Color.BLACK);.