Draw circle and rectangle in java [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am new to java and I want to draw circle and rectangle by using java code. I did write code for that purpose and tried at my own. But on Panel is appearing and shapes are not appearing.
Code of "MyPanel" is given below
import javax.swing.*;
import java.awt.*;
public class MyPanel extends JPanel{
public void painComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(20,20,20,20);
g2.setColor(Color.blue);
g2.fillOval(50,20,20,20);
g2.drawString("Hello World", 120, 50);
}//end painComponent
}//end test class
Cdoe of driver class "Test" is given below.
import javax.swing.*;
import java.awt.*;
public class Test{
JFrame f;
MyPanel p;
public Test(){
f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MyPanel();
c.add(p, BorderLayout.CENTER);
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end of constructor
public static void main(String args[]){
Test t = new Test();
}
}
And as per my knowledge when frame becomes visible through the paintChildren() method then panel should become visible
Also to become visible panel will call paintComponent() method which will do your custom drawing, but it seems like panel is not calling paintComponent().

Your method in MyPanel is called painComponent :-).
That is why the method from the base class is called, your method does not override any method from JPanel.

You are doing some spelling mistake. Method inside “MyPanel” is painComponent() rather paintComponent()
“t” is missing from your prototype that is why your program was not able to override paintComponent() in MyPanel class.
So all you have to do is that just update your code and replace with “paintComponent();”
Complete class code is given below,
import javax.swing.*;
import java.awt.*;
public class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(20,20,20,20);
g2.setColor(Color.blue);
g2.fillOval(50,20,20,20);
g2.drawString("Hello World", 120, 50);
}//end painComponent
}//end test class
I have tested this code it works fine.

In MyPanel class, the method name should be paint, instead of painComponent.
It is an overridden method, hence, the name matters.
Please change the name of the method to "paint" and try.

Related

Drawing with functions on JFrame or JPanel java

I wanted to know if it is possible to use/make a function in another Class to draw an image/oval and then call it in the paint public void in our main Class.
If I have
public class Trydraw{
public void drawrcircle(Graphics g){
g.setColor(Color.RED);
g.drawOval(0, 0, 20,20);
g.fillOval(0,0,20,20);
}
}
And then call it here this way
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.*;
public class Display extends JPanel{
public static void main(String[]haha){
JFrame frame = new JFrame();
frame.setSize(800, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Trydraw l = new Trydraw();
l.drawrcircle(g);
}
}
Thanks for your future help.
Yes you can, if I get your question correctly.
Your sample code works for me if I add
frame.add(new Display());
to the end of your
public static void main(String[] haha)
method.
With your snippet the paint(g) method will never be called, because it will be executed with the initialization of the JPanel which will be initialized with the initialization the Display class (because of inheritance).
You probably want to create an instance of Display, which automatically initializes the JPanel with the overridden paint(g) method, thus the new Operator.
As the constructor of a JPanel returns a JPanel, the constructor of Display returns a type of JPanel as well, which contains the red circle. This JPanel needs to be added with the add method to your original JFrame.

How do you draw and fill a parallelogram in java swing? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've looked around a little, and I was surprised that I couldn't find a way to draw and fill a parallelogram with java swing. Is there a easy way to do that?
Thanks for your help in advance.
All (or at least most) drawing in swing is done by subclassing JPanel and overriding the paintComponent method. So it will start like this:
public class MyPanel extends JPanel{
#Override
public void paintComponent(Graphics g){
//Drawing stuff....
}
}
From here, you're going to want to create a Parallelogram Shape, and fill it. The simplest implementation of Shape for your use is Path2D.
public class MyPanel extends JPanel{
private Path2D.Double parallelogram;
public MyPanel(){
parallelogram = new Path2D.Double();
parallelogram.moveTo(0,0);
parallelogram.lineTo(50,0);
parallelogram.lineTo(100,50);
parallelogram.lineTo(50,50);
parallelogram.closePath();
setPreferredSize(new Dimension(100, 100));
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLACK);
g2d.fill(parallelogram);
}
}
Then create an instance of this panel and add it to a JFrame:
public static void main(String[] args){
JFrame f = new JFrame();
f.add(new MyPanel(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Suggestions:
Create a Shape using a Path2D object,
start your parallelogram using its moveTo(...) method
Continue drawing lines using its lineTo(...) methods.
Draw in the paintComponent(Graphics g) method override of a JPanel.
In this method cast the Graphics object into a Graphics2D object.
Then call fill(yourShape) passing in your Path2D object.

Rectangle won't display in JFrame

So I've been trying for the last two hours to get this program to draw a simple rectangle in a frame but nothing gets displayed in the frame when i run the program. I've looked through textbooks and old notebooks and everything in my program seems to be fine, yet nothing is displayed. Help?
This is the class that creates the frame and is supposed to draw the rectangle.
import javax.swing.JFrame;
public class FrameViewer {
public static void main(String[] args) {
//creates an empty frame.
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setTitle("Empty Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//draws the rectangle within the frame.
RectangleComponent component = new RectangleComponent();
frame.add(component);
frame.setVisible(true);
}
}
And here is the RectangleComponent
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class RectangleComponent extends JComponent{
public void paintCOmponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle(5,10,20,30);
g2.draw(box);
}
}
Java is case sensitive, instead of
paintCOmponent
You want
paintComponent
You should use the #Override annotation to mark methods you think you are overriding as it will highlight problems like this.
The method should also remain protected, as there is no reason any one should be calling from outside the class
You may also want to take a look at Initial Threads

who knows how to draw graphic primitives in turn [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
say at first rectangle and then a line for example
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
import javax.swing.JComponent;
import javax.swing.JFrame;
//class MyCanvas extends JComponent {
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.fillRect(20, 20, 500, 500);
}
}
class Linie extends MyCanvas {
public void paint(Graphics g) {
g.setColor(new Color(0, 255, 0));
g.drawLine(30, 40, 300, 100);
}
}
public class LinieUndRec {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(100, 100, 800, 800);
window.getContentPane().add(new MyCanvas());
window.getContentPane().add(new Linie());
window.setVisible(true);
}
}
this code draws only a line
how to draw a rectangle and a line
In your implementation of the Linie class, you're providing code for the paint method. This replaces the code of the paint method on the MyCanvas class, because Linie extends MyCanvas.
So, when you call paint() on an instance of Linie only the code in the Linie.paint() method is run, not both. If you want it to run both, add do this:
class Linie extends MyCanvas {
public void paint(Graphics g) {
super.paint(g); // This will draw the rectangle from the parent class,
// then render the line below
g.setColor(new Color(0,255,0));
g.drawLine(30,40, 300, 100);
}
}
As a side note, even though the code above should work, type inheritance isn't meant to solve this kind of problem. I'm just trying to provide an answer to the code as provided.
If you really want to draw a rectangle followed by a line, then call .paint() on an object that represents a rectangle, then call .paint() on an object that represents a line, one after the other. What you have here is...well, really bad code (that's okay if you're new to Java - we were all new once). You're using inheritance to accomplish what would be done by just calling two .paint() commands after each other.
Also as #Durandal points out, you can use a single canvas to do all drawings. You don't need one canvas per drawing operation. Think of a Canvas as a surface you can draw on, like a piece of paper or, well, like a painter's canvas. And think of the drawing operations as instructions of what to do in order to construct a painting. Generally speaking, you must draw things in the background first, followed by things in the foreground. The result is that drawing that happens first is covered up by drawing that happens later.

Using awt.Graphics in a method

I created a class called Test, most likely where I went wrong.
import javax.swing.JPanel;
import java.awt.*;
public class Test extends JPanel {
Graphics grap;
public void sun()
{
super.paintComponent(grap);
grap.setColor(Color.YELLOW);
grap.fillOval(0,0,20,20);
}
}
As you can see I want to paint a yellow "Oval" in the top left corner of the panel using a method but I did not use a PaintComponent method. Now I try to implement it in my Paint component method which is in a class called Painting.
//import...;
public class Painting extends JPanel{
protected void paintComponent(Graphics g)
{
Test test = new Test();
test.sun();
}
And now i created a main window that will create a panel and display the yellow oval.
//import...
public class main extends JFrame{
public static main(String [] args){
JFrame window = new JFrame();
window.add(new Painting());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(100,100);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
But this does not work. I have a feeling that it is the sun method in test. How would I get this to work? I have looked in all the java books and cant find anything that can help.
Please note that I do not what to add parameters to the method.
Thank You
Tom.
Few points to be noted here:
Never call super.paintComponent by yourself except within the overriden paintComponent method itself.
If you want to do some Graphics activities then override the paintComponent method and draw graphics over there
When you are overriding paintComponent method then the first statement within the method should be super.paintComponent(g).
Now, going by all above points your code should now be like this:
public class Test extends JPanel {
public void paintComponent(Graphics grap)
{
super.paintComponent(grap);
grap.setColor(Color.YELLOW);
grap.fillOval(0,0,20,20);
}
}
And your Painting class should be like this:
public class Painting extends JPanel{
Test test;
public Painting()
{
test = new Test();
setLayout(new BorderLayout());
add(test);
}
}
If i want to draw 50 ovals on different places then i would have a problem with extensive code
Then you would keep a List of the ovals that you want to paint. See Custom Painting Approaches which paints a bunch of Rectangles on a panel. All the code does is loop through the ArrayList to paint the Rectangle. Only a couple of lines of code are required.

Categories

Resources