Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - I need to get the equations for the circles and for the grids to match the picture with for loops

This is my code so far but I can't seem to get the equations for the circles and for the grids correct enough to match my picture and I'm only allowed to use for loops, static methods, etc. Project Outcome needed

import java.awt.*;

public class Illusion{
 public static void main(String[] args) {
  DrawingPanel panel = new DrawingPanel(500,400);
  panel.setBackground(Color.GRAY);
  Graphics g = panel.getGraphics();
  //setup
  //circles
  drawCircle(g,0,0,90,3,1);
 // drawCircle(g,120,10,90,3,1);
 // drawCircle(g,250,50,80,5,1);
  //grids
  //drawGrid(g,10,120,100,10,2);
  //drawGrid(g,350,20,40,5,3);
  //drawGrid(g,230,160,50,5,4);
}


//equation for circles and diamonds
public static void drawCircle (Graphics g, int x, int y, int size,                   int circle, int grid) {

  //diamonds
 g.drawLine(x,y+size/2,x+size/2,y);
 g.drawLine(x+size/2,y,x+size,y+size/2);
  g.drawLine(x+size,y+size/2,x+size/2,y+size);
  g.drawLine(x+size/2,y+size,x,y+size/2);


  int circleHeight=size;
  int circleWidth=size;
  int circleX=x;
  int circleY=y;

   for (int i=0; i<circle; i++) {
     int subPos=size-i*(size/circle);
     int subSize=i*(size/(circle*2));
     g.setColor(Color.RED);
     g.fillOval (x+ subPos,y+subPos,subSize,subSize);
     g.setColor (Color.BLACK);
     g.drawOval (x+subPos,y+subPos,subSize,subSize);
   }  
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Graphics g = panel.getGraphics(); is NOT how custom painting works, see Painting in AWT and Swing and Performing Custom Painting for more details about how painting should be done.

Start by defining a class which is capable of painting a single illusion, with parameters to control the number of circles and size.

public class EhrensteinIllusion {

    public EhrensteinIllusion() {
    }

    public void paint(Graphics2D graphics, int dimeter, int circleCount) {
        Graphics2D g2d = (Graphics2D) graphics.create();
        g2d.setColor(Color.RED);
        g2d.fillOval(0, 0, dimeter, dimeter);
        int divisions = dimeter / circleCount;
        g2d.setColor(Color.BLACK);
        for (int circle = 0; circle < circleCount; circle++) {

            int size = divisions * (circle + 1);
            int x = (dimeter - size) / 2;
            int y = x;

            g2d.drawOval(x, y, size, size);

        }
        g2d.drawLine(dimeter / 2, 0, dimeter, dimeter / 2);
        g2d.drawLine(dimeter, dimeter / 2, dimeter / 2, dimeter);
        g2d.drawLine(dimeter / 2, dimeter, 0, dimeter / 2);
        g2d.drawLine(0, dimeter / 2, dimeter / 2, 0);
        g2d.dispose();
    }

}

Now, note, I've not specified the x/y position, this is VERY important, as I'm going to use a feature of the Graphics API to do that instead.

Now, you need some way to be able paint these illusions...

Part 03

public class IllusionPane extends JPanel {

    private EhrensteinIllusion illusion = new EhrensteinIllusion();

    public IllusionPane() {
        setBackground(Color.DARK_GRAY);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.translate(250, 50);
        illusion.paint(g2d, 80, 5);
        g2d.dispose();
    }

}

This satisfies the third series.

Now, all we need is a simple way to paint a grid. All this does is repeatedly paints a EhrensteinIllusion at different positions, using the same technique above, by translation the origin point of the Graphics context

public class EhrensteinIllusionGrid {

    public void paint(Graphics2D graphics, int size, int circleCount, int rowsCols) {
        Graphics2D g2d = (Graphics2D) graphics.create();
        EhrensteinIllusion illusion = new EhrensteinIllusion();
        g2d.setColor(Color.LIGHT_GRAY);
        g2d.fillRect(0, 0, size * rowsCols, size * rowsCols);
        for (int row = 0; row < rowsCols; row++) {
            for (int col = 0; col < rowsCols; col++) {
                Graphics2D gCopy = (Graphics2D) g2d.create();
                // New origin point
                gCopy.translate(col * size, row * size);
                illusion.paint(gCopy, size, circleCount);
                gCopy.dispose();
            }
        }
        g2d.dispose();
    }

}

Which you can use in the same way as you did the EhrensteinIllusion class, for example...

Part 6

public class IllusionPane extends JPanel {

    private EhrensteinIllusionGrid grid = new EhrensteinIllusionGrid();
    private EhrensteinIllusion illusion = new EhrensteinIllusion();

    public IllusionPane() {
        setBackground(Color.DARK_GRAY);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.translate(250, 50);
        illusion.paint(g2d, 80, 5);
        g2d.dispose();
        g2d = (Graphics2D) g.create();
        g2d.translate(230, 160);
        grid.paint(g2d, 50, 5, 4);
        g2d.dispose();
    }

}

This satisfies the sixth series.

Make sure you have a look at 2D Graphics for more details


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...