I'm not sure why but in my paintComponent method, the fillOval function is not letting me pass in my other classes coordinates. It comes up with :
'Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException'
all the rectangles draw fine but just not the oval. Here is the paintComponent() method.
public void paintComponent(Graphics graphics){
graphics.setColor(Color.BLACK);
graphics.fillRect(0,0,600,450);
graphics.setColor(Color.WHITE);
graphics.fillRect(290,0,15,450);
graphics.fillRect(leftPaddle.getXPos(),leftPaddle.getYPos(),10,85);
graphics.fillRect(rightPaddle.getXPos(),rightPaddle.getYPos(),10,85);
graphics.fillOval(ball.getxPos(),ball.getyPos(),ball.getWidth(),ball.getHeight());
}
And here is my Ball class (the class which bears the coordinates).
public class Ball {
int xPos = 140;
int yPos = 50;
int width = 15;
int height = 15;
public int getWidth() {
return width;
}
public int getxPos() {
return xPos;
}
public int getyPos() {
return yPos;
}
public int getHeight() {
return height;
}
}
This is probably quite an easy fix, but I am relatively new to java, so excuse any formatting mistakes, etc.
Here is where I get the object from the MainClass :
public class PaintComponents extends JPanel{
Paddle leftPaddle;
Paddle rightPaddle;
Ball ball;
public PaintComponents(Paddle leftPaddle, Paddle rightPaddle, Ball ball) {
this.leftPaddle = leftPaddle;
this.rightPaddle = rightPaddle;
this.ball = ball;
}
As its showing NullPointerException
you might not have created the object "ball" of class Ball
try adding:
Ball ball=new Ball();
in public void paintComponent(Graphics graphics)
before using "ball".
I didn't see where did you get the ball. So,
You can instantiate the Ball class like Ball ball = new ball() before the fillOval() happen. Or
You can mark all the fields and methods as static then use Ball.getSth() instead.
Related
I am trying to add a custom circle to a JPanel, see this:
graphicPanel = new GraphicPanel();
JTextArea text = new JTextArea("1233", 5, 10);
graphicPanel.add(text);
Circle circle = new Circle();
circle.setX(30);
circle.setY(30);
circle.setDiameter(30);
graphicPanel.add(circle);
graphicPanel.repaint();
graphicPanel.revalidate();
GraphicPanel is just a custom JPanel that doesn't do anything interesting yet (just holds a list that is not used yet)
GraphicPanel.java
public class GraphicPanel extends JPanel {
private static final long serialVersionUID = -3813468764873993369L;
private List<Node> nodes = new ArrayList<Node>();
public GraphicPanel() {
}
public void addNode(Node node) {
nodes.add(node);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval((30 - 30 / 2), (30 - 30 / 2), 30, 30);
}
}
Circle.java
public class Circle extends JComponent {
private static final long serialVersionUID = 628299863960706428L;
private int x;
private int y;
private int diameter;
private Color color;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillOval((x - diameter / 2), (y - diameter / 2), diameter, diameter);
}
}
The JTextArea appears, the circle does not. If I add the draw code for the circle directly to the paintComponent() of graphicPanel, then a circle appears:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval((30 - 30 / 2), (30 - 30 / 2), 30, 30);
}
So the draw code itself should be fine (I considered that maybe the circle is drawn somewhere where I cant see it but this is not the case).
What do I have to change to make the circle appear? I want to draw it like this and not with g.fillOval() in paintComponent() of GraphicPanel.
I am using Java 8
The first thing is that a JPanel has a FlowLayout as a default layout manager.
A FlowLayout honors the preferred size of a Component, but Circle doesn't have a specific one, so its size is (0,0).
You may want to override getPreferredSize to give it one, or use a layout manager that will still give a size to your Circle (e.g a BorderLayout where you add your component to CENTER) .
For later , you may also want to override getMaximumSize and getMinimumSize.
The second thing is that getX and getY are existing methods from JComponent, that your code overrides (probably not on purpose).
Those methods tell the position of this component within its container and would mess up the layout if you play with them (here your Circle is located at 30,30 inside GraphicPanel and gets hidden by the textarea).
getX()
the current x coordinate of the component's origin
In the following example, I changed the name and the accessor methods of x and y to avoid overriding getX and getY (there was actually no need to change the names of x and y variables, it is just to keep coherent with those accessor methods names).
A "preferred size" has also been set by adding an overriden getPreferredSize method, computing its optimal size.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Circle extends JComponent {
private static final long serialVersionUID = 628299863960706428L;
private int xCoo;
private int yCoo;
private int diameter;
private Color color;
#Override
public Dimension getPreferredSize() {
return new Dimension(diameter + xCoo, diameter + yCoo);
}
/* #Override
public Dimension getMinimumSize() {
return new Dimension(diameter + xCoo, diameter + yCoo);
}
#Override
public Dimension getMaximumSize() {
return new Dimension(diameter + xCoo, diameter + yCoo);
}*/
public int getXCoo() {
return xCoo;
}
public void setXCoo(final int xCoo) {
this.xCoo = xCoo;
}
public int getYCoo() {
return yCoo;
}
public void setYCoo(final int yCoo) {
this.yCoo = yCoo;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(final int diameter) {
this.diameter = diameter;
}
public Color getColor() {
return color;
}
public void setColor(final Color color) {
this.color = color;
}
public void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillOval((xCoo - diameter / 2), (yCoo - diameter / 2), diameter, diameter);
}
}
Also note that JComponent has set/getBackground and set/getForeground methods that you may find useful to set and get background and foreground Color.
I'm new to Java and I want to try some graphic things with it. I want to generate two circles with two different colors and different positions. My code:
Paint Class:
package de.test.pkg;
import javax.swing.*;
public class paint {
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame("Titel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Circle d = new Circle();
Circle r = new CircleRed();
frame.add(d);
frame.add(r);
frame.setSize(600,200);
frame.setVisible(true);
}
}
Circle class
package de.test.pkg;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Circle extends JPanel {
private double iconRadius = 100;
private Color defaultColor = new Color(89,104,99);
private int positionX = 0;
private int positionY = 0;
private Ellipse2D iconBody = new Ellipse2D.Double(getPositionX(),getPositionY(),iconRadius,iconRadius);
public Icon(){
}
public Color getDefaultColor() {
return defaultColor;
}
public void setDefaultColor(Color defaultColor) {
this.defaultColor = defaultColor;
}
public int getPositionX() {
return positionX;
}
public void setPositionX(int positionX) {
this.positionX = positionX;
}
public int getPositionY() {
return positionY;
}
public void setPositionY(int positionY) {
this.positionY = positionY;
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
super.paintComponent(g2d);
this.setBackground(new Color(255,255,255));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(getDefaultColor());
g2d.draw(iconBody);
g2d.fill(iconBody);
}
}
CircleRed class
package de.design.pkg;
import java.awt.Color;
public class CircleRed extends Circle {
private Color defaultColor = Color.RED;
private int positionX = 120;
private int positionY = 0;
public CircleRed() {
}
public Color getDefaultColor() {
return defaultColor;
}
public void setDefaultColor(Color defaultColor) {
this.defaultColor = defaultColor;
}
public int getPositionX() {
return positionX;
}
public void setPositionX(int positionX) {
this.positionX = positionX;
}
public int getPositionY() {
return positionY;
}
public void setPositionY(int positionY) {
this.positionY = positionY;
}
}
The Result is that the Circles have the same positions.
My questions are:
Why do they have the same positions?
Is this a good way to do that or are there better solutions? I want to use a class so please don't gave me answers with do all that paint thing in the Main.
Is there a better way to hold the position. Maybe in an array? But how should the setter and getter look like if I want to return array[position]?
If I want to set the Position from the Main function. How can I do this?
The Result is that the Circles have the same positions.
(1) Why do they have the same positions?
Not really. The result is that only CircleRed is displayed. Your problem here is not with painting, it's with adding components to a container with a suitable layout manager. The lines
Circle d = new Circle();
Circle r = new CircleRed();
frame.add(d);
frame.add(r);
add r instead of d. This is because JFrame uses BorderLayout by default and you are replacing the center component d with r the line after. Just to show the point, add the line
frame.setLayout(new GridLayout(1, 2));
(2) Is this a good way to do that or are there better solutions? I want to use a class so please don't gave me answers with do all that paint thing in the Main.
It depends on what you are aiming to do. I would venture a guess that if you want to practice inheritance, it would be better for you to create an abstract class for a general circle with shared code and then subclass it with concrete implementation and specific code for the various types. Otherwise, you can just create a single customizable circle class and instantiate that one with different parameters.
It's not a good practical approach in any case because the placement of the circles (which are JPanels) will be determined by the layout manager. The painting only determines the location of the painted shape in the panel. It would be better to just paint the shapes on a single big panel and not with using multiple panels.
There are a few very good answers on the site about moving components around.
(3) Is there a better way to hold the position. Maybe in an array? But how should the setter and getter look like if i want to return array[position]?
There are effectively 2 positions in your design. One is for the panels in the frame, the other is for the shapes in the panels.
For the latter, I would use a Point or just an int x, y fields in the class itself. Getters and setters are the standard ones, the setters will control the position (you will need to call repaint() though).
For the first, it is determined by the layout manager and you don't (shouldn't) control it in a pixel-prefect way. You just instruct the layout manager with "guidelines" and it does the calculations for you.
(4) If I want to set the Position from the Main function. How can i do this?
Again, depends on which position you are talking about. See above.
What your doing is very overkill for just creating two colored circles. You can just use the paint method in java.awt
public void paint(Graphics g){
g.setColor(Color.YELLOW);
g.fillOval(20,20,160,160);
g.setColor(Color.RED);
g.fillOval(60,60,80,80);
}
fillOval takes the following parameters (int x, int y, int width, int height)
You can use g.setColor(Color.NAME) to change the color of your circle. Just call this method before your draw calls.
They're in the same position because the paintComponent() method in Circle is being used for both - and it's using the position variables defined in Circle. When you have CircleRed extend Circle, you don't need to define the position variables again - you inherit them from Circle. Instead, call the setPosition() methods on CircleRed. (You don't need to define these either, they're also inherited.)
There are a variety better solutions, I think the biggest improvement would be to improve your use of inheritance.
That's a fine way to hold the position. If anything else, you could also use a Point object. (Already in Java)
To set the position from the main function, you just call for example
Circle c = new Circle();
c.setPositionX(120);
c.setPositionY(40);
You are going wrong way. You can simply draw two circles by overriding paint method of JFrame class.
Here is a sample demo program.
import java.awt.*;
/**
*
* #author Pankaj
*/
public class TestFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public TestFrame() {
initComponents();
setTitle("Graphics Demo");
setSize(200,200);
}
/**
* You need to override this method.
*/
#Override
public void paint(Graphics g) {
int X1 = 10; //X coordinate of first circle
int X2 = 60; //X coordinate of second circle
int Y1 = 100; //Y coordinate of first circle
int Y2 = 100; //Y coordinate of second circle
int width = 50; //Width of the circle
int height = 50; //Height of the circle
Color color1 = Color.RED; //Color of first circle
Color color2 = Color.BLUE; //Color of second circle
g.setColor(color1);
g.fillOval(X1, Y1, width, height);
g.setColor(color2);
g.fillOval(X2, Y2, width, height);
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
pack();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
}
And here's the output
I am working on a lab to practice inheritance, in which we are to create a horizontal ellipse as "Shape1" and then create a "Shape2" which extends Shape1 which draws it's superclass Shape1, and then draws a vertical ellipse over top to create a new looking shape. The shape is displaying fine in terms of inheritance and looks (color/location etc) however when running the program, the frame width is set to 1000, and the height is set to 700, but If I drag the frame by the corner to enlarge it, the shape is drawn over and over again as I keep dragging the frame larger. Ideally the shape should just stay where it is relative to the frame size. I think this is happening because while I drag the frame larger, the draw method is being called over and over again by the system, but I am not sure where this is happening or how to fix it. Any suggestions?
All classes are displayed below:
Shape1:
public class Shape1 {
private double x, y, r;
protected Color col;
private Random randGen = new Random();
public Shape1(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
this.col = new Color(randGen.nextFloat(), randGen.nextFloat(), randGen.nextFloat());
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public double getR() {
return this.r;
}
public void draw(Graphics2D g2){
//Create a horizontal ellipse
Ellipse2D horizontalEllipse = new Ellipse2D.Double(x - 2*r, y - r, 4 * r, 2 * r);
g2.setPaint(col);
g2.fill(horizontalEllipse);
}
}
Shape2:
public class Shape2 extends Shape1 {
public Shape2(double x, double y, double r) {
super(x, y, r);
}
public void draw(Graphics2D g2) {
//Create a horizontal ellipse
Ellipse2D verticalEllipse = new Ellipse2D.Double(super.getX() - super.getR(),
super.getY() - 2*super.getR(),
2 * super.getR(), 4 * super.getR());
super.draw(g2);
g2.fill(verticalEllipse);
}
}
ShapeComponent:
public class ShapeComponent extends JComponent {
//Instance variables here
private Random coordGen = new Random();
private final int FRAME_WIDTH = 1000;
private final int FRAME_HEIGHT = 700;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Shape2 myShape = new Shape2(1 + coordGen.nextInt(FRAME_WIDTH), 1 + coordGen.nextInt(FRAME_HEIGHT), 20);
//Draw shape here
myShape.draw(g2);
}
}
ShapeViewer(Where the JFrame is created):
public class ShapeViewer {
public static void main(String[] args) {
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 700;
//A new frame
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Lab 5");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ShapeComponent component = new ShapeComponent();
frame.add(component);
//We can see it!
frame.setVisible(true);
}
}
because while I drag the frame larger, the draw method is being called over and over again by the system,
Correct, all components are repainted when the frame is resized.
Any suggestions?
Painting code should be based on properties of your class. If you want the painting to be a fixed size then you define the properties that control the painting and set these properties outside the painting method.
For example, you would never invoke Random.nextInt(...) in the painting method. This means the value will change every time the component is repainted.
So the Shape should be created in the constructor of your class and its size would be defined there, not each time you paint it.
Okay. I was writing my Ball class and the ball is not displayed. I tried adding other components to my container and they are displayed, so I think it is safe to assume that the problem is my ball. The class code:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Ball extends JComponent {
public Ball() {
ballX = (Window.WINDOW_WIDTH - BALL_DIAMETER) / 2;
ballY = (Window.WINDOW_HEIGHT - BALL_DIAMETER) / 2;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(5, 5, 50, 50);
g.setColor(Color.GREEN);
g.fillOval(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);
g.dispose();
}
public void setX(int x) {
ballX = x;
}
public void setY(int y) {
ballY = y;
}
private int ballX;
private int ballY;
public static final int BALL_DIAMETER = 30;
}
The first rect was used for testing. It doesn't appear neither....
Make sure your component has a preferred size larger than (0, 0):
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
Not seeing the code that adds the ball to a container, it's a little hard to answer your question.
However, there are a few problems with your code:
You're setting the location of the ball in the constructor, using some constants - Assuming you want to paint the ball in the middle of the component, you should calculate the location in the paintComponent method by calling getSize and doing the math on the spot
You dispose of the Graphics object at the end of the paintComponent method even though you didn't create it, don't do that, remove that method call (that's probably the culprit)
You're not keeping the state of the Graphics object. Either create a copy (using the create method of the Graphics object) and disposing of it at the end, or by restoring the state you changed at the end of the method (the Color that was set before you changed it to Color.GREEN)
So what I want to achieve, is to create a simple program that lets the user create rectangles on the screen and then move them around.
I know that I can declare a new object in the code (i.e rectangle i = new rectangle(x,y,sizex,sizey)) however that will create only one, moreover it forces me to declare it in the code:
block1 = new block
block2 = new block
etc
Question is: How can I let the user create infinite rectangles with the use of lets say a button (not necessarily a button, it can be anything) and then let the user be capable of modyfing them (location/size etc).
Examples would be nice. I just feel there is a better way than declaring a gazillion objects in the code and then displaying them one by one. In C++ i could declare a malloc expandable container that would just hold the values, and then just display things using these values, not sure for java.
Simple code for reference:
public static void main(String[] args){
JFrame frame = new JFrame("A.L.T.E.S");
//Container container = frame.getContentPane();
frame.setSize(1024, 768);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Block object = new Block(10,10,20,20);
frame.add(object);
object.reDraw();
}
public class Block extends JPanel{
int yPos;
int xPos;
int xSize;
int ySize;
public Block(int xPos, int yPos, int xSize, int ySize){
this.xPos = xPos;
this.yPos = yPos;
this.xSize = xSize;
this.ySize = ySize;
}
public void setYPos(int yPos){
this.yPos = yPos;
}
public void setXPos(int xPos){
this.xPos = xPos;
}
public void setXSize(int xSize){
this.xSize = xSize;
}
public void setYSize(int ySize){
this.ySize = ySize;
}
public int getYPos(){
return yPos;
}
public int getXPos(){
return xPos;
}
public int getYSize(){
return ySize;
}
public int getXSize(){
return xSize;
}
public void reDraw(){
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(xPos, yPos, xSize, ySize);
}
}
Nothing in Java is simple.
First, Java already has a Rectangle class that holds the origin and size of a Rectangle. In your code, you're making all the rectangles blue. Suppose you want the user to set the color of a Rectangle. You can define your Block class like this.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Block {
private Color color;
private Rectangle rectangle;
public Block(int x, int y, int width, int height) {
this(new Rectangle(x, y, width, height));
}
public Block(Rectangle rectangle) {
this.rectangle = rectangle;
this.color = Color.BLUE;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Rectangle getRectangle() {
return rectangle;
}
public void draw(Graphics g) {
g.setColor(getColor());
g.fillRect(rectangle.x, rectangle.y,
rectangle.width, rectangle.height);
}
}
Next, you need a model class to hold all of the Blocks that your user defines.
Something like this BlockList class.
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
public class BlockList {
private List<Block> blockList;
public BlockList() {
this.blockList = new ArrayList<Block>();
}
public void init() {
this.blockList.clear();
}
public void addBlock(Block block) {
this.blockList.add(block);
}
public void draw(Graphics g) {
for (int i = 0; i < blockList.size(); i++) {
blockList.get(i).draw(g);
}
}
}
Now that you have your GUI model defined, you would build your GUI. Your drawing JPanel would call the draw method in your BlockList class.
I strongly suggest that you go through the Oracle tutorial on Swing. Go through the complete tutorial before you attempt to create a Swing GUI.
In java, use a Collection. In this case, use a List. You can create an ArrayList<Block>, then invoke add to add new rectangles. Then, you can iterate through this collection by using iterator, then calling hasNext and next on the iterator to find all the elements. I'm sure this brief hypnosis is going to be confusing, and if so, check out the Java Tutorial on Collections, which explains everything in all the detail you are going to need.