Don't know if it was a very specific title, but I have already asked this question but has gone dead.
I am trying to execute paintComponent() so I can draw rectangles, triangles and more with the class being a JComponent.
Here is my code so far:
public class Design extends JComponent {
private static final long serialVersionUID = 1L;
private List<ShapeWrapper> shapesDraw = new ArrayList<ShapeWrapper>();
private List<ShapeWrapper> shapesFill = new ArrayList<ShapeWrapper>();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int screenWidth = gd.getDisplayMode().getWidth();
int screenHeight = gd.getDisplayMode().getHeight();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(ShapeWrapper s : shapesDraw){
g2d.setColor(s.color);
g2d.draw(s.shape);
}
for(ShapeWrapper s : shapesFill){
g2d.setColor(s.color);
g2d.fill(s.shape);
}
}
public void drawRect(int xPos, int yPos, int width, int height) {
shapesDraw.add(new Rectangle(xPos, yPos, width, height));
repaint();
}
public void fillRect(int xPos, int yPos, int width, int height) {
shapesFill.add(new Rectangle(xPos, yPos, width, height));
repaint();
}
public void drawTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesDraw.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}
public void fillTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesFill.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(getWidth(), getHeight());
}
public int getWidth() {
return screenWidth;
}
public int getHeight() {
return screenHeight;
}
}
class ShapeWrapper {
Color color;
Shape shape;
public ShapeWrapper(Color color , Shape shape){
this.color = color;
this.shape = shape;
}
}
As shown above, everything works perfectly fine, except for being able to choose a colour.
I want to be able to define the rectangles and triangles with their respective positions and lengths but also want to add a colour with it.
But I get an error.
The error says:
The method add(ShapeWrapper) in the type List< ShapeWrapper > is not applicable for the arguments (Rectangle)
And:
The method add(ShapeWrapper) in the type List< ShapeWrapper > is not applicable for the arguments (Polygon)
Please help! I am so stressed trying to figure this out as it is blocking me from doing many things.
The answer is pretty basic...Shape is not a type of ShapeWrapper, therefore it can't be added to a List decalred as List<ShapeWrapper>
What you should be doing instead of
shapesDraw.add(new Rectangle(xPos, yPos, width, height));
is something more like...
shapesDraw.add(new ShapeWrapper(Color.BLACK, new Rectangle(xPos, yPos, width, height)));
The same goes for your ...Triangle methods. You need to wrap the resulting Polygon in a ShapeWrapper before trying to add it to the List
Related
I am attempting to draw Sierpinski's triangle on a pixel-by-pixel basis that resizes itself any time the window size is changed. I believe I have most of the project done but I don't quite know how to draw the rectangle from the separate recursive function that is outside of the paintComponent method.
public class SierpTriangle extends JPanel
{
public final int x = this.getWidth();
public final int y = this.getHeight();
public final int side = getsize();
public int getsize()
{
int width = this.getWidth();
int height = this.getHeight();
if (width <= height)
{
return width;
}
else
{
return height;
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawSierpTriangle(this.x, this.y, this.side);
g.drawRect(x,y,1,1);
}
public void drawSierpTriangle(int x, int y, int size)
{
if (size == 1)
{
//Draw rectangle? This is where I need help
g.drawRect(x,y,1,1); //this does not work, passing Graphics g into the method also does not work
}
else
{
drawSierpTriangle(x/2, y, size/2);
drawSierpTriangle(x,y/2,size/2);
drawSierpTriangle(x/2,y/2,size/2);
}
}
public static void main(String[] args)
{
new SierpFrame();
}
}
Pass the reference of Graphics from paintComponent to drawSierpTriangle
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawSierpTriangle(g, this.x, this.y, this.side);
g.drawRect(x,y,1,1);
}
public void drawSierpTriangle(Graphics g, int x, int y, int size)
{
if (size == 1)
{
//Draw rectangle? This is where I need help
g.drawRect(x,y,1,1); //this does not work, passing Graphics g into the method also does not work
}
else
{
drawSierpTriangle(g, x/2, y, size/2);
drawSierpTriangle(g, x,y/2,size/2);
drawSierpTriangle(g, x/2,y/2,size/2);
}
}
This results in: Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at the first recursive call of the method. Any input?
public final int side = getsize();
will make side for ever 0.
Replace it with something more like...
public int getSide() {
int width = this.getWidth();
int height = this.getHeight();
if (width <= height) {
return width;
} else {
return height;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int side = getSide();
if (side == 0) return;
drawSierpTriangle(g, this.x, this.y, side);
g.drawRect(x, y, 1, 1);
}
This will evaluate side every time the component is painted. It will also skip painting the shape if side is 0
You'll also have the same problem with x and y, since there state is never changed
Ok so I'm currently making a snakes and ladders game for a project and I have encountered a problem in which I was never faced before with. I have a class called Player which would be a circle on the game board.
The paintComponent method should be printing true constantly however, it does not
public class Player extends JComponent {
private double playerX;
private double playerY;
private double diameter;
private String playerColor;
HashMap<String, Color> colorMap = new HashMap();
public Player(String playerColor, double playerX, double playerY, double diameter) {
this.playerColor = playerColor;
this.playerX = playerY;
this.playerY = playerY;
this.diameter = diameter;
setSize(getPreferredSize());
setLocation((int) diameter, (int) diameter);
}
#Override
public Dimension getPreferredSize() {
return new Dimension((int) diameter * 2, (int) diameter * 2);
}
#Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
setDoubleBuffered(true);
g2d.drawOval((int) playerX, (int) playerY, (int) diameter * 2, (int) diameter * 2);
System.out.println(true);
}
}
You should not extend JComponent to create a new component, but to customize the painting process by overriding paintComponent(..).
If you want to create a new component with its custom painting scheme, use a JPanel and override the paint(..) method.
public class Player extends JPanel {
private static final long serialVersionUID = 1L;
private double playerX;
private double playerY;
private double diameter;
private String playerColor;
HashMap<String, Color> colorMap = new HashMap<>();
public Player(String playerColor, double playerX, double playerY, double diameter) {
this.playerColor = playerColor;
this.playerX = playerY;
this.playerY = playerY;
this.diameter = diameter;
setSize(getPreferredSize());
setLocation((int) diameter, (int) diameter);
}
#Override
public Dimension getPreferredSize() {
return new Dimension((int) diameter * 2, (int) diameter * 2);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
setDoubleBuffered(true);
g2d.drawOval((int) playerX, (int) playerY, (int) diameter * 2, (int) diameter * 2);
System.out.println(true);
}
}
More info here.
I'm making a simple app that allows you to show different circle objects browse through them with buttons.
The problem is i have no idea how to print out my circleobjects into the jPanel.
When you first run the program the first circleobject should appear in the jPanel. Here's my circleclass:
public class Circle {
private int height;
private int width;
private Color color;
public Circle (int height, int width, Color color){
this.height = height;
this.width = height;
this.color = color;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
And here is the first part of the GUI code. I've made 5 circleobjects in an arraylist.
public class CircleGUI extends javax.swing.JFrame{
public ArrayList<Circle> circles = new ArrayList<Circle>();
public CircleGUI(){
initComponents();
circles.add(new Circle(15, 15, Color.blue));
circles.add(new Circle(20, 15, Color.black));
circles.add(new Circle(30, 10, Color.green));
circles.add(new Circle(20, 10, Color.orange));
circles.add(new Circle(35, 35, Color.red));
}
Now, how do i make my first object appear in the jPanel which is marked on the screenshot?
You will have to override the paintComponent(Graphics) method of your JPanel.
Then you do the drawing on the Graphics object, based on the data your current Circle object contains.
Check out Custom Painting Approaches. It does exactly what you want (except it paint Rectangles).
It actually shows two approaches:
Painting objects from an ArrayList (which is what you want)
Painting objects onto a BufferedImage
The basic painting code in your case is:
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// Custom code to paint all the Rectangles from the List
for (DrawingArea.ColoredRectangle cr : coloredRectangles)
{
g.setColor( cr.getForeground() );
Rectangle r = cr.getRectangle();
g.drawRect(r.x, r.y, r.width, r.height);
}
}
Of course you would paint ovals and use your Circle class
So I've been working on a homework on abstraction for my programming class and fell into a problem. The goal for me right now is to be able to use abstraction, then later be able to draw with rectangles and ovals a simple city, like a rectangular building or a oval light on a light post.
The error I am receiving when I compile is: MyTestApp.Rectangle is not abstract and does not override abstract method drawEllipse(java.awt.Graphics) in MyTestApp.Shape. This Error shows up on the line "class Rectangle extends Shape{" right below the class Shape.
My question is what am I doing wrong with my abstraction? I've been messing with the constructors and draw() methods in classes Rectangle and Ellipse for a while now and still to no luck happen to find a solution.
Code is below:
import java.awt.*;
import javax.swing.*;
public class MyTestApp extends JPanel {
Rectangle rect;
Ellipse oval;
public static void main(String [] args) {
MyTestApp myTestApp = new MyTestApp ();
myTestApp.test();
}
public MyTestApp () { //creates the jframe
JFrame frame = new JFrame("MyClass Driver");
setBackground(new Color(200, 250, 200));
setPreferredSize(new Dimension(500, 400));
frame.add(this);
frame.pack();
frame.setVisible(true);
}
public void delay(int msecs) {
try {
Thread.sleep(msecs);
} catch (InterruptedException e) {
}
}
public void paint(Graphics g) {//paints the rectangle and ellipse
super.paint(g);
if (rect != null)
rect.drawRectangle(g);
if (oval != null)
oval.drawEllipse(g);
}
public void test() {//gives the x/y position, width/height, and fill/outline color for the rectangle and oval
delay(1000);
rect = new Rectangle(20, 30, 23, 75, Color.GREEN, Color.BLUE);
oval = new Ellipse(10, 10, 10 , 34, Color.RED, Color.MAGENTA);
repaint();
}
public abstract class Shape{//abstract class Shape that sets the x/y, width/height, and colors for the shapes
private int x, y, width, height;
private Color fillColor;
private Color outlineColor;
public Shape(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
setXY(x, y);
setSize(width, height);
setFillColor(fillColor);
setOutlineColor(outlineColor);
}
public boolean setXY(int x, int y) {
this.x = x;
this.y = y;
return true;
}
public void setSize(int width, int height) {
if (width > 0)
this.width = width;
if (height > 0)
this.height = height;
}
public boolean setFillColor(Color fillColor){
if (fillColor == null) return false;
this.fillColor = fillColor;
return true;
}
public boolean setOutlineColor(Color outlineColor){
if (outlineColor == null) return false;
this.outlineColor = outlineColor;
return true;
}
public Color getFillColor() {
return fillColor;
}
public Color getOutlineColor() {
return outlineColor;
}
public abstract void drawRectangle(Graphics g);//do i need two?
public abstract void drawEllipse(Graphics g);//do i need both?
}
class Rectangle extends Shape{//!!!!!!!!!! where the error shows
public Rectangle(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
super(x, y, width, height, fillColor, outlineColor);
}
public void drawRectangle(Graphics g){//draws the retangle
g.setColor(fillColor);
g.fillRect(x, y, width, height);
g.setColor(outlineColor);
g.drawRect(x, y, width, height);
}
}
class Ellipse extends Shape{
public Ellipse(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
super(x, y, width, height, fillColor, outlineColor);
}
public void drawEllipse(Graphics g){//draws the ellipse
g.setColor(fillColor);
g.fillOval(x, y, width, height);
g.setColor(outlineColor);
g.drawOval(x, y, width, height);
}
}
}
Thanks for reading and helping!
Both classes Rectangle and Ellipse need to override both of the abstract methods.
To work around this, you have 3 options:
Add the two methods
Make each class that extends Shape abstract
Have a single method that does the function of the classes that will extend Shape, and override that method in Rectangle and Ellipse, for example:
abstract class Shape {
// ...
void draw(Graphics g);
}
And
class Rectangle extends Shape {
void draw(Graphics g) {
// ...
}
}
Finally
class Ellipse extends Shape {
void draw(Graphics g) {
// ...
}
}
And you can switch in between them, like so:
Shape shape = new Ellipse();
shape.draw(/* ... */);
shape = new Rectangle();
shape.draw(/* ... */);
Again, just an example.
If you're trying to take advantage of polymorphic behavior, you need to ensure that the methods visible to outside classes (that need polymorphism) have the same signature. That means they need to have the same name, number and order of parameters, as well as the parameter types.
In your case, you might do better to have a generic draw() method, and rely on the subclasses (Rectangle, Ellipse) to implement the draw() method as what you had been thinking of as "drawEllipse" and "drawRectangle".
I want to do a bouncing balls application in java. Each ball should take place by mouse clicking and each of them should have random speed, color, radius and starting position. I managed to do everything except the part where mouse listener takes place. Whatever i do in the mousePressed method didn't work. What should i do to make user create a random ball when he presses the mouse?
EDIT: This is the last version of my code. Now the problem is that i can't create more than one ball. When i click on the screen same ball is just keeps speeding.
BouncingBalls Class
public class BouncingBalls extends JPanel implements MouseListener{
private Ball ball;
protected List<Ball> balls = new ArrayList<Ball>(20);
private Container container;
private DrawCanvas canvas;
private int canvasWidth;
private int canvasHeight;
public static final int UPDATE_RATE = 30;
int x = random(480);
int y = random(480);
int speedX = random(30);
int speedY = random(30);
int radius = random(20);
int red = random(255);
int green = random(255);
int blue = random(255);
int count = 0;
public static int random(int maxRange) {
return (int) Math.round((Math.random() * maxRange));
}
public BouncingBalls(int width, int height){
canvasWidth = width;
canvasHeight = height;
ball = new Ball(x, y, speedX, speedY, radius, red, green, blue);
container = new Container();
canvas = new DrawCanvas();
this.setLayout(new BorderLayout());
this.add(canvas, BorderLayout.CENTER);
this.addMouseListener(this);
}
public void start(){
Thread t = new Thread(){
public void run(){
while(true){
update();
repaint();
try {
Thread.sleep(1000 / UPDATE_RATE);
} catch (InterruptedException e) {}
}
}
};
t.start();
}
public void update(){
ball.move(container);
}
class DrawCanvas extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
container.draw(g);
ball.draw(g);
}
public Dimension getPreferredSize(){
return(new Dimension(canvasWidth, canvasHeight));
}
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame f = new JFrame("Bouncing Balls");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setContentPane(new BouncingBalls(500, 500));
f.pack();
f.setVisible(true);
}
});
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
balls.add(new Ball(x, y, speedX, speedY, radius, red, green, blue));
start();
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Ball Class
import java.awt.Color;
import java.awt.Graphics;
public class Ball{
public static int random(int maxRange) {
return (int) Math.round((Math.random() * maxRange));
}
private BouncingBalls balls;
int x = random(480);
int y = random(480);
int speedX = random(30);
int speedY = random(30);
int radius = random(20);
int red = random(255);
int green = random(255);
int blue = random(255);
int i = 0;
public Ball(int x, int y, int speedX, int speedY, int radius, int red, int green, int blue){
this.x = x;
this.y = y;
this.speedX = speedX;
this.speedY = speedY;
this.radius = radius;
this.red = red;
this.green = green;
this.blue = blue;
}
public void draw(Graphics g){
for(Ball ball : balls){
g.setColor(new Color(red, green, blue));
g.fillOval((int)(x - radius), (int)(y - radius), (int)(2 * radius), (int)(2 * radius));
}
}
public void move(Container container){
x += speedX;
y += speedY;
if(x - radius < 0){
speedX = -speedX;
x = radius;
}
else if(x + radius > 500){
speedX = -speedX;
x = 500 - radius;
}
if(y - radius < 0){
speedY = -speedY;
y = radius;
}
else if(y + radius > 500){
speedY = -speedY;
y = 500 - radius;
}
}
}
Container Class
import java.awt.Color;
import java.awt.Graphics;
public class Container {
private static final int HEIGHT = 500;
private static final int WIDTH = 500;
private static final Color COLOR = Color.WHITE;
public void draw(Graphics g){
g.setColor(COLOR);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
}
ERROR: I get "Can only iterate over an array or an instance of java.lang.Iterable" error in this part of code:
public void draw(Graphics g){
for(Ball ball : balls){
g.setColor(new Color(red, green, blue));
g.fillOval((int)(x - radius), (int)(y - radius), (int)(2 * radius), (int)(2 * radius));
}
}
First, if you want to render more than one ball, you should create another class to contain all the properties needed to draw a ball (maybe even a draw (Graphics g) method to delegate the drawing). Then you would have a list of balls on your BouncingBalls class which should be iterated over and painted on the paintComponent method.
Said that, your mouseClicked handler would just create a new Ball instance and add it to the list.
EDIT:
Example of how the drawing process would be on your DrawCanvas class:
class DrawCanvas {
public void paintComponent(Graphics g){
super.paintComponent(g);
container.draw(g);
for (Ball ball : balls)
//the draw method should only care of the specific ball instance
//you are calling it from
ball.draw(g);
}
...
I think you are having problems separating your problem into classes and making their instances cooperate to do what you want. If you are indeed having doubts about this, I recommend you read some articles/books about the topic to get a better idea of the concepts of a class and an object and how they work; it'll definitely help you do your programming with ease.
You need to add the MouseListener to the component:
public BouncingBalls() {
this.addMouseListener(this); // <-- Add this object as a MouseListener.
this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
Try using this code in your main method:
frame.addMouseListener(this);
You need to add the mouse listener to the frame/panel.
(response to this comment by you) Alternatively, if you want to add the listener to the panel, first you must call
setFocusable(true);
requestFocusInWindow();
In your BouncingBalls constructor. Then you can add the mouse listener to the panel with:
addMouseListener(this);
This is because the panel does not initially have focus.
The easiest way to do it, though, is to just add the mouse listener to the frame.