I've created a class with three methods. I originally started with slightly different coding, and have changed it after looking at several videos and questions until I ended up with this. For some reason, the square I am trying to make simply will not appear on my screen. Please make any suggestions you have. I started relatively recently, which I'm sure is obvious. Please excuse any conventional errors I have made. As a side note, I figured out that the problem is that the method "paint" isn't being called. As I'm new I have no idea how to call it after trying a few different ways. If you have any clarifying questions about my code please ask. Thanks!
public class TheGame extends JPanel {
public void screen() {
JFrame f = new JFrame();
f.setTitle("Grid Game");
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(250, 250, 500, 500);
f.add(this);
f.setVisible(true);
}
public void paint(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(50, 50, 200, 200);
}
public static void main(String[] args) {
TheGame game = new TheGame();
game.screen();
}
You need to override the paintComponent() method:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(50, 50, 200, 200);
}
Related
I'm absolutely stumped as to why Paint Component isn't running in this code:
public class GraphicsWindow extends JPanel {
public static final int Width = 1000, Height = 800;
GraphicsWindow(){
setPreferredSize(new Dimension(Width, Height));
}
public void PaintComponent(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
for(int i = 0; i < Width/10; i++){
g.drawLine(i * 10, 0, i*10, Height);
}
System.out.println("paint ran");
}
}
Main function:
public static void main(String[] args) {
GraphicsWindow Graphics = new GraphicsWindow();
Graphics.setBackground(Color.green);
Graphics.setSize(1000, 800);
JFrame Window = new JFrame("Snake");
Window.add(Graphics);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setBounds(650, 200, 1200, 1000);
Window.setVisible(true);
Graphics.repaint();
}
I have been at it for about an hour looking at forum page after forum page and gotten nowhere. I can tell that it is the paint component not running because the console never gets the "Paint ran" printout. I am sorry ahead of time if it is a really stupid mistake and for the possibly messy code, I am a bit new to java.
Java names are case sensitive:
public void PaintComponent(Graphics g){
should be:
public void paintComponent(Graphics g){
When you override a method you should be using:
#Override
public void PaintComponent(Graphics g){
and the compiler will give you an error message when you don't override an existing method.
Variable names should NOT start with an upper case. Learn Java conventions by looking at examples and follow those examples.
Graphics.repaint();
There is no need for the repaint(). Swing will paint all the components when the frame is made visible.
Read the Swing Tutorial for Swing basics and working examples to get you started.
Alright so I am practicing Java myself and I found this exercise: Draw the word: "HELLO" only using lines and ovals. So I basically drew all the letters and it should work perfectly, but for some reason only the "O" (last letter) shows, and I do not know why. Here's my code:
Main class:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 750);
frame.setTitle("HELLO without using Strings");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 1000));
JComponent componentH = new LetterH();
JComponent componentE = new LetterE();
JComponent componentL1 = new LetterL1();
JComponent componentL2 = new LetterL1();
JComponent componentO = new LetterO();
frame.add(componentH);
frame.add(componentE);
frame.add(componentL1);
frame.add(componentL2);
frame.add(componentO);
frame.setVisible(true);
}
One letter (I could post the other letters aswell, but there's no point as the code looks completely the same on every letter, except for: x1, y1, x2, y2:
public class LetterH extends JComponent {
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
g.drawLine(10, 0, 10, 100);
g.drawLine(60, 0, 60, 100);
g.drawLine(10, 50, 60, 50);
g.setColor(Color.BLACK);
g.drawLine(120, 0, 120, 100);
g.drawLine(120, 100, 170, 100);
g.setColor(Color.BLACK);
g.drawLine(180, 0, 180, 100);
g.drawLine(180, 100, 230, 100);
}
}
I couldn't find an answer on this problem anywhere, so I thought I'd ask it here. I hope someone can help.
Every tip is appreciated! Thanks in advance.
Sincerely, Double.
Frames like other swing stuff should be initialized within the EDT - Thread (java.awt.EventQueue.invokeLater explained). Initialization from a main method should look like this:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
If all this stuff is done from other threads (main thread), then very strange things are going to happen: not drawn or partial drawn contents, not updating components, ...
Nevertheless there could also be a problem with the sizes of your components. Try to install a LayoutManager within your frame or initialize position and size of your Letter components.
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
so I am new to designing graphics in Java I was wondering if anyone could help me here. I have two classes and I want to display both of them at the same time in a JFrame. But only one or the other get displayed.
public class Tutorial extends JPanel implements ActionListener {
Background bc = new Background();
Timer tm = new Timer(5,this);
int x =0, velX = 2;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(x, 30, 50, 30);
tm.start();
}
#Override
public void actionPerformed(ActionEvent e) {
if(x<0 || x>550){
velX = -velX;
}
x = x+ velX;
repaint();
}
public static void main(String [] args){
Background bc = new Background();
Tutorial t = new Tutorial();
JFrame jf = new JFrame();
jf.setTitle("Tutorial");
jf.setSize(600,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
jf.add(bc);
}
My second class
public class Background extends Canvas {
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(0,0,600,125);
g.fillRect(0,250,600,125);
g.setColor(Color.black);
g.fillRect(0,125,600,125);
}
For some reason I can only get either Background to be displayed or Tutorial?
Can anyone point me in the right direction or tell me where I am going wrong. I want to be able to display multiple things like these classes in the one window
The default layout manager of a JFrame is a BorderLayout.
By using the single-argument JFrame.add() function, you're adding both of the components to the BorderLayout.CENTER portion of your JFrame. This means that you'll only see one of the components.
The solution is to either use a different layout manager, or to add the components to different sections of your BorderLayout.
More info here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
I was studying graphics and tried to use PaintComponent to draw some shapes, following is the code. I am trying for an hour but still its not working really can't get reason. What is the resolution to this simple problem?
public class MyPainting extends JPanel
{
public void PaintComponent (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(100, 100, 10, 20);
}
public static void main (String [] args)
{
MyPainting p = new MyPainting();
JFrame f= new JFrame();
f.setSize(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.setVisible(true);
}
}
When I run program there is empty JFrame, I did try it g.drawString, ImageIcon but every time nothing is visible.
The method PaintComponent is not defined in any of the super classes of JPanel. You want paintComponent
#Override
public void paintComponent (Graphics g)
and add the #Override annotation to allow compiler check for the correct method.