I'm having issues with this code. For some reason, paintComponent(Graphics g) simply doesn't work and there doesn't seem to be an answer how to force it to. This is my code:
import javax.swing.JFrame;
public class Robotron extends JFrame
{
public Robotron ()
{
//add(this); This one gave me an error
setSize(800, 600);
new TestFrame();
setVisible(true);
}
public static void main(String [ ] args)
{
new Robotron();
}
and this is my TestFrame class that has the paintComponent function:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class TestFrame extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.YELLOW);
g.fillRect(100, 100, 10, 30);
System.out.println("Abdullah Paint");
}
public TestFrame()
{
setFocusable(true);
repaint();
}
}
What can I do in order to make paintComponent actually play. What I get in the end is just an empty JFrame, with no running of the System.out.println thingy.
Thanks a whole lot, been tackling this for a long time.
You're not adding anything to the JFrame, because of this
//add(this);
Here, you were trying to add the component to itself, which is not going to fly.
Instead, you need to add the TestFrame instance to the Frame.
add(new TestFrame());
setSize(800, 600);
setVisible(true);
You need to add the panel to the frame:
public Robotron ()
{
//add(this); This one gave me an error
setSize(800, 600);
add(new TestFrame());
setVisible(true);
}
As mentioned by others, you need to add the panel to the frame:
public Robotron() {
add(new TestFrame());
setSize(800, 600);
setVisible(true);
}
As not mentioned by others, you need to call super.paintComponent(g) the first thing in your overridden method:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.YELLOW);
g.fillRect(100, 100, 10, 30);
System.out.println("Abdullah Paint");
}
Notes:
Don't call repaint() in the constructor. At best it does nothing.
Don't call setSize(...) for the frame, instead call pack(). You will need to override the getPreferredSize method in your panel for this.
Related
I hope that I used the question function correctly this time. I have been confused by a question from yesterday to now. I used Google search to ask my java teacher and did not solve my problem.
When I use repaint, the child components in the shaped JPanel will exceed their display area. As in the following figures,
This is the effect I want
But when I use repaint somethings changes.
The button doesn’t seem right at first.
But sometimes the button will return to normal
these are my code. I use repaint because the information I checked tells me that I can use it. Repaint to achieve animation effects.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
class GPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.clip(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), getWidth(), getHeight()));
g2d.setPaint(Color.BLUE);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
public class MainComponentOverflow {
public static void main(String[] args) {
JFrame frame = new JFrame();
// This is a panel with a shape
GPanel panel = new GPanel();
// This one is the effect I am looking for, the rectangle is displayed in the Panel.
//panel.add(new Normal());
// The following two will have problems, the rectangle will be displayed outside the Panel
//panel.add(new Problem1());
panel.add(new Problem2());
//panel.add(new JButton("This will also cause problems, but it may also display properly when I resize the window."));
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class Normal extends JPanel {
public Normal() {
setPreferredSize(new Dimension(500, 500));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
class Problem1 extends JPanel implements ActionListener {
public Problem1() {
Timer timer = new Timer(16, this);
timer.start();
setPreferredSize(new Dimension(500, 500));
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
class Problem2 extends JPanel implements ActionListener {
public Problem2() {
Timer timer = new Timer(16, this);
timer.start();
setPreferredSize(new Dimension(500, 500));
}
#Override
public void actionPerformed(ActionEvent e) {
setBackground(new Color((float) Math.random(), (float)Math.random(), (float)Math.random()));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
}
When the frame is painted first, the clip will be set in GPanel, and then the children will be painted in Problem1 with the same clip, so it will work.
However, when you repaint Problem1, GPanel will not be repainted first, so the clip isn’t set, and there is no clip to restrict Problem1.
If instead of repainting Problem1 you repaint the parent, GPanel, it will resolve your problem.
Another solution would be to put the clip in Problem1 too.
Note that you can replace your RoundRect2D with an Ellipse2D, as you use it to paint an ellipse.
I am trying to use java swing GUI at the same time I am drawing 2Dgraphics for the game I am creating. My goal is to have a drop down over the top of the graphics. Whenever i try to make a button or drop down in swing I cannot see it. This I believe is the 2Dgrahpics covering the swing elements.
This is my class decleration and my JFrame creation:
public class Main extends JFrame{
public Main(){
setBackground(Color.LIGHT_GRAY);
setSize(1000,600);
setTitle("Scaling Tests");
setDefaultCloseOperation(3);
setVisible(true);
This is how I am double buffering and drawing the graphics:
public void paint(Graphics g){
dbImage = createImage(1000, 600);
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this.getWidth(), this.getHeight() , this);
repaint();
}
public void draw(Graphics g){
g.setColor(Color.red);
g.fillRect(100, 100, 200, 200);
I have tried to draw a JLabel like this in the constructor and the draw method:
JLabel mylabel = new JLabel();
mylabel.setText("Hello World!");
mylabel.setBounds(0, 0, 1280, 720);
mylabel.setVisible(true); //unnecessary
this.add(mylabel);
This has not worked. I cannot see the Jlabel no matter where in the code I put it. I think there is some problem with the graphics covering it up.
Your problem is that you are breaking the painting chain because you didn't invoke super.paint() so none of the child components are painted.
However you should NOT override paint() on a JFrame.
Instead, custom painting is done by overriding paintComopnent() on a JPanel and then you add the panel to the frame. And don't forget to invoke super.paintComponent(g) as the first statement in the method.
Then you can add the JLabel to the panel.
Also, never invoked repaint() in a painting method. This will cause an infinite loop.
This works -except the label is all the background you have to use some layout manager to handle it
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame{
public Main(){
setBackground(Color.LIGHT_GRAY);
setSize(1000,600);
setTitle("Scaling Tests");
setDefaultCloseOperation(3);
JLabel mylabel = new JLabel();
mylabel.setText("Hello World!");
mylabel.setBounds(0, 0, 1280, 720);
add(mylabel);
setVisible(true);
}
public static void main(String args[]) {
Main m=new Main();
}
public void paint(Graphics g){
Image dbImage = createImage(1000, 600);
Graphics dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this.getWidth(), this.getHeight() , this);
}
public void draw(Graphics g){
g.setColor(Color.red);
g.fillRect(100, 100, 200, 200);
}
}
Can anyone help me out and tell me why the rectangle will not appear? The frame runs fine, but no shapes show up. I have tried doing this a couple of different ways, including with two separate classes, but all I get is an empty frame.
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Surface extends JPanel
{
public void paintComponent(Graphics2D g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(100, 100, 30, 40);
}
public static void main(String[] args)
{
Surface s = new Surface();
JFrame jf = new JFrame();
jf.setTitle("Tutorial");
jf.setSize(600, 400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(s);
s.repaint();
}
}
If you want to override a method, then annotate it correctly:
#Override
public void paintComponent(Graphics2D g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(100, 100, 30, 40);
}
then your IDE should tell you, that you're not overriding the paintComponent method correctly, because your parameter type Graphics2D is wrong.
This is the signature of the original/parent method in JComponent:
protected void paintComponent(Graphics g)
And as you can see, it uses Graphics instead of Graphics2D. You're currently overloading paintCompoent instead of overriding it. So change your parameter type to Graphics (and import java.awt.Graphics) and it will work:
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(100, 100, 30, 40);
}
By the way, you're setting the visibility of your jf first and then add something to its contentpane. In some case this can cause trouble and the added components won't be visible until you repaint the frame (or you do something else, which cause the frame to repaint itself, like calling pack()). So it might be best to switch the order of these method calls in your main method:
Surface s = new Surface();
JFrame jf = new JFrame();
jf.setTitle("Tutorial");
jf.setSize(600, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(s);
//s.repaint(); // not needed anymore, because "jf" will repaint everything during the 'setVisible' call
jf.setVisible(true); // should almost always be the last thing you do
Why JFrame is empty after my graphics
package javaGame;
import java.awt.Graphics;
import javax.swing.JFrame;
public class javaGame extends JFrame {
public javaGame (){
setVisible(true);
setSize(250,250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Java Game");
}
public void paint (Graphics g){
g.drawString("Hello world!", 75, 75);
}
public static void main (String[] args){
new javaGame ();
}
}
You are used to applets, where overriding paint will actually work. In a JFrame, the recommended technique is a little more complicated. Add this code to your constructor:
getContentPane().add(new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello world!", 75, 75);
}
});
This creates an anonymous subclass of JPanel which overrides paintComponent to do some painting, then adds the JPanel to the frame. The call to super.paintComponent is important.
There are several other things that you should know are different from applets:
You need to call setVisible(true) to make the frame appear. This is mandatory for your program to work.
You should add setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) if you want the program to be closed if the window is closed. Highly recommended.
When you opening a new frame you should make it visible after validate or pack is applied to it, also call setVisible(true) at the end of the frame creation. The code for modification:
public void paint (Graphics g){
super.paint(g);
g.drawString("Hello world!", 75, 75);
}
1) Follow java code conventions javaGame should be JavaGame
2) Swing programs should override paintComponent() instead of overriding paint().
3) You should make custom painting in a JComponent like JPanel.
Change your code to for example something like this.
public class JavaGame {
private JFrame frame; //use composition instead of concrete inheritance
public JavaGame () {
jframe.setSize(250,250);
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setTitle("Java Game");
jframe.add(new JPanel() {
#Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawString("Hello world!", 75, 75);
}
});
jframe.pack(); //sizes the frame
jframe.setVisible(true);
}
public static void main (String[] args) {
SwingUTilities.invokeLater(new Runnable() {
#Override
public void run() {
new javaGame();
}
});
}
}
I tried to override method paintComponent in inner class JPanel and paint some picture. But if I load image in the constructor, method paintComponent is not calling. If load image in main class, everythig is fine. What is it? Here's the code, that does'nt work
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
JFrame window;
//Image image=new ImageIcon("D://domik.png").getImage();
class JPanelExt extends JPanel {
Image image;
public JPanelExt (){
image=new ImageIcon("D://domik.png").getImage();
System.out.println("constructor");
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paint");
g.drawImage(image, 0, 0, this);
g.drawRect(0, 400, 100, 100);
}
}
public Main(){
window=new JFrame("Flowers");
window.setSize(430, 480);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanelExt flower1 =new JPanelExt();
flower1.setBounds(100, 100, 200, 200);
flower1.setToolTipText("House");
window.setLayout(null);
window.add(flower1);
}
public static void main(String[] args) {
Main main=new Main();
}
}
And sysout writes only "constructor"
But if I change code this way
public class Main {
JFrame window;
Image image=new ImageIcon("D://domik.png").getImage();
class JPanelExt extends JPanel {
//Image image;
public JPanelExt (){
//image=new ImageIcon("D://domik.png").getImage();
System.out.println("constructor");
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paint");
g.drawImage(image, 0, 0, this);
g.drawRect(0, 400, 100, 100);
}
And sysout writes "constructor","paint"
I can't understand this ))
Your "problem" is the order of statements in the Main constructor.
First you are constructing a new frame. Second, you set it visible. At this point it is painted and also calls the paint methods on its associated panels. Also at this point, there is no associated panel. Third, you construct a new JPanelExt and add it to the frame. This will not cause the frame to be repainted.
Put the call
window.setVisible(true);
at the end of the construction process. Then you will see your image.