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
Related
I need to do a java code with Threads (implements Runnable()) for a college assigment. I need to draw 4 fractals at the same time ( with thread.sleep()). I already tried almost everything I knew, and this still doesn't working. So I cleaned up the source.
I have four classes ( four fractals). In my JPanel, i call a paint method to draw them ( they are recursive ). Can anyone save me please?
public class MainPanel extends JPanel {
FractalTree tree = new FractalTree();
FractalCircle circle = new FractalCircle();
FractalSquare square = new FractalSquare();
FractalCircle2 circle2 = new FractalCircle2();
#Override
public void paint(Graphics g) {
setBackground(Color.black,g);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.WHITE);
DrawBounds(g);
tree.drawTree(g,200,290,-90,9);
circle.drawCircle(g2,675,175,300);
square.drawSquares(g, 200, 525, 100,7);
circle2.drawCircle(g2,675,518,300);
}
public void DrawBounds(Graphics g){
g.drawLine(0,350,900,350);
g.drawLine(450,0,450,700);
}
public void setBackground(Color c,Graphics g){
g.fillRect(0, 0, 900, 700);
}
}
public class FractalSquare{
public void drawSquares(Graphics g,int x, int y, int side ,int size){
g.setColor(Color.BLUE);
if(size >2){
size--;
g.fillRect(x-side/2, y-side/2, side, side);
side = side/2;
x = x-side;
y = y-side;
drawSquares(g,x,y,side,size);
drawSquares(g,x+side*2,y,side,size);
drawSquares(g,x,y+side*2,side,size);
drawSquares(g,x+side*2,y+side*2,side,size);
} else return;
}
}
public class FractalCircle {
public void drawCircle(Graphics2D g, float x, float y, float radius) {
g.setColor(Color.RED);
g.draw(new Ellipse2D.Float(x-radius/2, y-radius/2, radius,radius));
if(radius > 2) {
radius *= 0.75f;
drawCircle(g,x, y, radius);
} else return ;
}
}
public class FractalCircle2 {
public void drawCircle(Graphics2D g, float x, float y, float radius) {
Color color = new Color(255,0,255);
g.setColor(color);
g.draw(new Ellipse2D.Float(x-radius/2, y-radius/2, radius,radius));
if(radius > 1) {
radius *= 0.75f;
drawCircle(g,x + radius/2, y, radius/2);
drawCircle(g,x - radius/2, y, radius/2);
} else return ;
}
}
public class FractalTree {
public void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
g.setColor(Color.GREEN);
if (depth == 0) return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 5.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 5.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - 20, depth - 1);
drawTree(g, x2, y2, angle + 20, depth - 1);
}
}
You'll have to render your fractals into separate images and copy them to the screen as the rendering progresses.
Swing will do most of the work for you if you wrap each fractal in a JComponent.
Here's an example using one of your fractals:
public class FractalPanel extends JPanel {
final FractalSquare square = new FractalSquare(450, 350);
public FractalPanel() {
add(square);
}
public static void main(String[] args) {
FractalPanel fractalPanel = new FractalPanel();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(fractalPanel);
frame.pack();
frame.setVisible(true);
Thread squareThread = new Thread(() -> {
try {
fractalPanel.square.render();
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
});
squareThread.start();
}
}
class Fractal extends JComponent {
final BufferedImage image;
final Graphics2D offscreenGraphics;
public Fractal(int width, int height) {
setPreferredSize(new Dimension(width, height));
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
offscreenGraphics = image.createGraphics();
}
// Copy the offscreen image to the main graphics context
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
synchronized (image) { // synchronize with the render thread
g2.drawImage(image, 0, 0, null);
}
}
}
class FractalSquare extends Fractal {
public FractalSquare(int width, int height) {
super(width, height);
}
public void render() throws InterruptedException {
drawSquares(getWidth() / 2, getHeight() / 2, 100, 7);
}
public void drawSquares(int x, int y, int side, int size) throws InterruptedException {
// Sleep for 10ms between frames
Thread.sleep(10);
if (size > 2) {
size--;
synchronized (image) { // synchronize with the draw thread
offscreenGraphics.setColor(Color.BLUE);
System.out.printf("Filling [%d, %d, %d, %d]\n", x - side / 2, y - side / 2, side, side);
offscreenGraphics.fillRect(x - side / 2, y - side / 2, side, side);
}
// Tell Swing that we've updated the image and it needs to be redrawn
repaint();
side = side / 2;
x = x - side;
y = y - side;
drawSquares(x, y, side, size);
drawSquares(x + side * 2, y, side, size);
drawSquares(x, y + side * 2, side, size);
drawSquares(x + side * 2, y + side * 2, side, size);
}
}
}
In the JFrame which contains your MainPanel, add this code (a private Thread and its instance somewhere in the constructor):
private Thread threadRepaint;
...
threadRepaint = new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
yourPanel.repaint();
FractalSquare.decreaseLimit();
FractalCircle.decreaseLimit();
FractalCircle2.decreaseLimit();
FractalTree.decreaseLimit();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
threadRepaint.start();
The idea behind this, is that the Thread has to decrease the limit of your fractals each second (1000ms).
In each of your fractal classes you have to change the conditionals to something like this, and also add a static variable. That way, you'll see a progress with each iteration.
For your FractalSquare, it will stop when it gets to 2:
private static limit = 10;
...
public static void decreaseLimit() {
if (limit > 2) limit--;
}
...
// Inside your drawSquares method
if (size > limit){
...
} else return;
It's basically the same idea for each of your other fractals: you set an upper limit which will decrease over time (each iteration of the Thread). The decreasing limit will give the illusion of progression with each iteration.
I'm writing a game in which i need to paint bricks of a random color. This is my progress so far. I have a class of Brick, AllBricks, and Overridden paint method for JPanel:
private static class Brick {
static int x;
static int y;
static Color color;
Brick(int _x, int _y, Color _color){
x = _x;
y = _y;
color = _color;
}
void paint(Graphics g) {
g.setColor(Color.WHITE);
g.drawRoundRect(x, y, BRICK_SIZE, BRICK_SIZE, BRICK_ARC_SIZE, BRICK_ARC_SIZE);
g.setColor(color);
g.fillRoundRect(x + 1, y + 1, BRICK_SIZE-2, BRICK_SIZE-2, BRICK_ARC_SIZE-1, BRICK_ARC_SIZE-1);
}
private static class AllBricks {
private ArrayList<Brick> bList = new ArrayList<>();
AllBricks(){ bList.clear(); }
void add (Brick b){ bList.add(b); }
void paint(Graphics g) {
if(bList.size()>0) {
for (Brick brick : bList) brick.paint(g);
}
}
}
private static class GameField extends JPanel {
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
allBricks.paint(g);
}
}
And now, when I call my main loop, adding new blocks and trying to draw them, i only see the last added block, but not all of them...
private void loop()
{
while (true) {
delay.wait(1000);
Brick b1 = new Brick(random.nextInt(WIN_WIDTH - BRICK_SIZE), random.nextInt(WIN_HEIGHT - BRICK_SIZE), COLORS.get(random.nextInt(COLORS.size() - 1)));
allBricks.add(b1);
mainField.repaint();
}
}
Can you, please, help me save previously painted blocks on the screen?
Your brick x and y coordinate shoudn't be static. Since it's static all Bricks have one shared x and y value (so all Bricks are drawn at the same position)
So I have two classes here:
PhotoComponent class:
(This class is to handle a specific image as a JComponent. When "flipped" I want to draw pen strokes instead of having an image. So I replace the image with a rectangle, attempting to draw pen strokes over it.)
public class PhotoComponent extends JComponent {
private Image pic;
private boolean flipped;
private int contentAreaWidth;
private int contentAreaHeight;
p
#Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
System.out.println("Draw: " + draw + ", Pic: " + pic);
if (draw && pic != null) {
super.paintComponent(g);
System.out.println("width using this: " + this.getWidth() + ", actual width of JPanel: " + contentAreaWidth);
System.out.println("height using this: " + this.getHeight() + ", actual height of JPanel: " + contentAreaHeight);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null)) / 2;
int y = (contentAreaHeight - pic.getHeight(null)) / 2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x,y,pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (drawingMode) {
g2.setPaint(Color.RED);
if (drawOval) {
penStrokes.put(ovalX, ovalY);
if (penStrokes != null) {
for (Integer xCoor : penStrokes.keySet()) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((xCoor - (brushSize / 2)), (penStrokes.get(xCoor) - (brushSize / 2)), brushSize, brushSize);
//System.out.println("SIZE OF HASHTABLE: " + penStrokes.size());
}
}
System.out.println("Filling an oval!" + ovalX + ", " + ovalY);
}
} else if (textMode) {
g2.setPaint(Color.YELLOW);
if (drawRect) {
rectDimensions.add(rectX);
rectDimensions.add(rectY);
rectDimensions.add(rectWidth);
rectDimensions.add(rectHeight);
for (int i = 0; i < rectDimensions.size(); i+=4) {
g2.fillRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
g2.drawRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
}
}
}
System.out.println("This is being called again!");
}
}
}
public void setRectangle(int x, int y, int width, int height) {
drawRect = true;
rectX = x;
rectY = y;
rectWidth = width;
rectHeight = height;
}
public void removeRectangle() {
drawRect = false;
}
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX =
NOTE THE DRAWLINE() METHOD ABOVE. I am drawing at the given points, repainting, and setting the old variables to be the current variables.
Main class:
private static PhotoComponent img;
private static JFrame frame;
private static JPanel contentArea;
//Mouse Coordinates for PenStrokes
private static int oldX, oldY;
//Mouse Coordinates for Sticky Notes
private static Point clickPoint;
public static void main (String[] args) {
frame = new JFrame("PhotoManip");
img = null;
contentArea = null;
oldX = 0;
oldY = 0;
setupMenubar(frame);
setupJFrame(frame);
}
private static void addPhotoComponent(File file) {
}
if (img.getTextMode()) {
img.removeRectangle();
clickPoint = null;
}
}
});
img.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
if (img.getDrawingMode()) {
if (withinRange(e.getX(), e.getY())) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
img.repaint();
}
}
if (img.getTextMode()) {
if (withinRange(e.getX(), e.getY())) {
Point dragPoint = e.getPoint();
h, height);
img.repaint();
}
}
}
});
if (img!=null) {
contentArea.add(img);
}
}
private static boolean withinRange(int x, int y) {
if (x > img.getX() && x < img.getX() + img.getWidth()) {
if (y > img.getY() && y < img.getY() + img.getHeight()) {
return true;
}
}
return false;
}
private static void flipImage() {
if (!img.isFlipped()) {
img.setFlipped(true);
} else if (img.isFlipped()) {
img.setFlipped(false);
}
}
drawLine() is called above in this main class, when a mousedrag occurs. Problem is that the strokes don't appear to show.
I know that the program is calling g2.fillOval() because I am printing out a verification statement afterwards.
Additionally, I have created print statements for when the mouse is pressed and dragged and they are getting the correct coordinates?
Why don't red strokes appear? I'm confused. Is it the way my code is structured?
The crux of your problem is that you are trying to draw something outside the paintComponent method, which is never supported. Whatever you draw will get overwritten by the next call of paintComponent, which will happen almost instantly. We can solve this by storing the co-ordinates of the oval and drawing it within paintComponent instead of trying to draw on a graphics object outside of the paintComponent method. See code below:
First we are going to add the following variables to your PhotoComponent class:
private boolean drawOval = false;
private int ovalX = 0;
private int ovalY = 0;
Then we will add methods for controlling them:
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX = currentX;
ovalY = currentY;
return toReturn;
}
public void removeOval() {
drawOval = false;
}
After that we can change the paintComponent method to have it draw the oval based on those variables:
#Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
super.paintComponent(g);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null)) / 2;
int y = (contentAreaHeight - pic.getHeight(null)) / 2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.RED);
}
//took the code you already used for drawing the oval and moved it here
if (drawOval) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((ovalX - (brushSize / 2)), (ovalY - (brushSize / 2)), brushSize, brushSize);
}
}
Finally change the addPhotoComponent method to update those variables instead of trying to draw the oval directly:
private static void addPhotoComponent(File file) {
Image image = null;
try {
image = ImageIO.read(file);
} catch (IOException e2) {
System.out.println("ERROR: Couldn't get image.");
}
img = new PhotoComponent(image, contentArea);
img.revalidate();
img.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// your code here
System.out.println("You flipped the photo!!!");
flipImage();
img.repaint();
}
}
#Override
public void mousePressed(MouseEvent e) {
img.setOval(e.getX(), e.getY());
}
#Override
public void mouseReleased(MouseEvent e) {
img.removeOval();
}
});
img.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
}
});
if (img != null) {
contentArea.add(img);
}
}
I'm in the process of making a simple 2-D game, however I am having trouble drawing images. Below are a few classes that are relevant to the problem
private Vector<Bullet> ammo = new Vector<Bullet>(100);
public class Bullet{
Image img;
int x, y, speed;
boolean show;
Bullet(Image img,int x, int y, int speed, boolean show){
this.x = x;
this.y = y;
this.speed = speed;
this.img = img;
this.show = show;
}
public void draw(ImageObserver obs) {
if(show)
g2.drawImage(img, this.x, this.y, obs);
}
public void update(){
this.y -= 1;
}
}
public class Movement{
....
Movement(....){
.....
}
public void fly(){
......
ammo.add(new Bullet(bullet1, m.x, m.y, 7, true));
}
public class MyPlane {
KeyControl key;
Movement flight;
Image img;
int x, y, speed, move = 0;
int boom;
...
}
public void drawDemo() {
...
for(Bullet bullets: ammo)
bullets.update();
...
for(Bullet bullets: ammo)
bullets.draw(this);
}
}
When I call bullets.draw(this) nothing actually is drawn on the screen. I know however that the ammo vector does contain the correct information, such as the x coordinate, y coordinate... I'm using Graphics 2-D by the way. Any help and or suggestions would be greatly appreciated thanks.
public void paint(Graphics g) {
if(bimg == null) {
Dimension windowSize = getSize();
bimg = (BufferedImage) createImage(windowSize.width,
windowSize.height);
g2 = bimg.createGraphics();
}
drawDemo();
g.drawImage(bimg, 0, 0, this);
}
I think you should draw the image like this:
public void draw(Graphics g2) {
if(show)
g2.drawImage(img, this.x, this.y, null);
}
Then in your plane class you have to add Graphics as an argument of the drawDemo() method:
public void drawDemo(Graphics g2) {
...
for(Bullet bullets: ammo)
bullets.update();
...
for(Bullet bullets: ammo)
bullets.draw(g2);
}
}
and finally in you paint(Graphics g) method you call this:
public void paint(Graphics g) {
/*
...
*/
drawDemo(g2);
g.drawImage(bimg, 0, 0, this);
}
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