Last Updated
Viewed 03 Times
           

Basically I'm trying to paint a bunch of lines in java that i can use code to manipulate the location later.

Here is the code I used for my paint component:

public class rectDrawer extends JComponent{
    public void drawComponent(Graphics gr) {
        Graphics2D g=(Graphics2D) gr;
        Element q= new Element();
        q=q.returnStatic();
        for(int i=1;i<5;i++) {
            g.setColor(q.getAColor(i));
            Shape s =q.getLine(i);
            AffineTransform a= AffineTransform.getScaleInstance(3,3);
            s=a.createTransformedShape(s);
            g.draw(s);
        }
    }
}

To initialize this class and add it to JFrame I used the following:

public class simulator{
    public static JFrame screen = new JFrame();
    public ArrayList<Element> canvas = new ArrayList<Element>();
    public static void main(String[] args) {
        screen.setSize(900,900);
        screen.setTitle("Simulation");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        screen.setVisible(true);

        Element field1=new Element(0,0,0,"field");
        field1.setStatics();
        rectDrawer d =new rectDrawer();
        screen.add(d);
    }
}

I really do not have any idea what is wrong with this, in theory it should paint a 432 by 432 square on the JFrame but all I'm getting is a blank JFrame. I am attaching the rest of my project (besides imports) in case there's a problem in there. Thanks!

public class Element {
    private double c1x, c1y, c2x, c2y, c3y, c3x, c4x, c4y, dSS, rSS, l, w; 
    private int canvasLocation;
    //c1 to c2 is width (short side) c2 to c4 is short 
    private Color color, frontColor;
    private String type;
    public static Element thisEl;
    private Elem t;
    public Element() {}
    public Element(double c1X, double c1Y, double degrees, String t){
        Elem el=Elem.getElem(t);
        setElem(el);
        c1x=c1X;
        c1y=c1Y;
        type=el.name();
        dSS=degrees;
        rSS=Math.PI*degrees/180;
        color=el.getEC();
        frontColor=el.getEFC();
        l=el.getEL();
        w=el.getEW();
        c2x=Math.cos(rSS)*w+c1x;
        c3x=Math.cos(rSS)*l+c2x;
        c2y=Math.sin(rSS)*w+c1x;
        c3y=Math.sin(rSS)*l+c2x;
        c4x=c1x+c3x-c2x;
        c4y=c1y+c3y-c2y;
    }
    public Element(double c1X, double c1Y, double degrees, Elem ty){
        setElem(ty);

        c1x=c1X;
        c1y=c1Y;
        type=t.name();
        dSS=degrees;
        rSS=Math.PI*degrees/180;
        color=t.getEC();
        frontColor=t.getEFC();
        l=t.getEL();
        w=t.getEW();
        c2x=Math.cos(rSS)*w+c1x;
        c3x=Math.cos(rSS)*l+c2x;
        c2y=Math.sin(rSS)*w+c1x;
        c3y=Math.sin(rSS)*l+c2x;
        c4x=c1x+c3x-c2x;
        c4y=c1y+c3y-c2y;
    }
    private void setElem(Elem ty) {
        t=ty;
    }
    public void setPosMove(double x, double y, double deg){
        c1x+=x;
        c1y+=y;
        rSS+=Math.PI*deg/180;
        dSS+=deg;
        c2x=Math.cos(rSS)*w+c1x;
        c3x=Math.cos(rSS)*l+c2x;
        c2y=Math.sin(rSS)*w+c1x;
        c3y=Math.sin(rSS)*l+c2x;
        c4x=c1x+c3x-c2x;
        c4y=c1y+c3y-c2y;
    }
    public double getdSS(){
        return dSS;
    }
    public double getrSS(){
        return rSS;
    }
    public double getc1x(){
        return c1x;
    }
    public double getc1y(){
        return c1y;
    }
    public double getc2x(){
        return c2x;
    }
    public double getc2y(){
        return c2y;
    }
    public double getc3x(){
        return c3x;
    }
    public double getc3y(){
        return c3y;
    }
    public double getc4x(){
        return c4x;
    }
    public double getc4y(){
        return c4y;
    }
    public int getCanvasLocation() {
        return canvasLocation;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc1(){
        Point2D.Double c = null;
        c.setLocation(c1x, c1y);
        return c;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc2(){
        Point2D.Double c = null;
        c.setLocation(c2x, c2y);
        return c;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc3(){
        Point2D.Double c = null;
        c.setLocation(c3x, c3y);
        return c;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc4(){
        Point2D.Double c = null;
        c.setLocation(c4x, c4y);
        return c;
    }
    public Line2D.Double getFront(){
        return new Line2D.Double(getc1(), getc4());
    }
    public Line2D.Double getBack(){
        return new Line2D.Double(getc2(), getc3());
    }
    public Line2D.Double getRight(){
        return new Line2D.Double(getc3(), getc4());
    }
    public Line2D.Double getLeft(){
        return new Line2D.Double(getc1(), getc2());
    }
    public Element returnStatic() {
        return thisEl;
    }
    public double getCorner(int cNum, boolean yCoord){
        if(yCoord) {
            cNum+=4;
        }
        switch(cNum){
            case 1: return c1x;
            case 2: return c2x;
            case 3: return c3x;
            case 4: return c4x;
            case 5: return c1y;
            case 6: return c2y;
            case 7: return c3y;
            default: return c4y;
        }
    }
    public Line2D.Double getLine(int cNum){
        switch(cNum){
            case 1: return getFront();
            case 2: return getRight();
            case 3: return getBack();
            default: return getLeft();
        }
    }
    public Point2D.Double getCenter(){
        double x=0,y=0;
        for(int i=1;i<5;i++) {
            x+=getCorner(i,false);
            y+=getCorner(i,true);
        }
        return new Point2D.Double(x/4.0,y/4.0);
    }
    public double getLength() {
        return l;
    }
    public double getWidth(){
        return w;
    }
    public String getType(){
        return type;
    }
    public Color getColor() {
        return color;
    }
    public Color getFrontColor() {
        return frontColor;
    }
    public void setStatics() {
        thisEl=new Element(c1x, c1y, dSS, type);
    }
    public Color getAColor(int i) {
        if(i==1) {
            return frontColor;
        }
        return color;
    }
    public ArrayList<Line2D.Double> getLines(){
        ArrayList<Line2D.Double> lines=new ArrayList<Line2D.Double>();
        for (int i=1;i<5;i++) {
            lines.add(getLine(i));
        }
        return lines;
    }
    public Color[] getColors() {
        Color[] c= {getFrontColor(), getColor()};
        return c;
    }
}
public enum Elem {
    skystone(8,4,new Color(255,234,86),new Color(135, 135, 15)),
    stone(8,4,new Color(255,234,86)),
    capstone(8,4,new Color(255,234,86)),
    foundation(32,16, new Color(40,40,40)),
    robot(18,18,new Color(125,125,125)),
    post(2,2,new Color(175,175,175)),
    field(144,144,new Color(128,128,0));

    public int length, width; 
    public Color frontColor, color;
    Elem(int l, int w, Color c, Color fC){
        length=l;
        width=w;
        color=c;
        frontColor=fC;
    }
    Elem(int l, int w, Color c){
        length=l;
        width=w;
        color=c;
        frontColor=c;
    }
    int getEL() {
        return length;
    }
    int getEW() {
        return width;
    }
    Color getEFC() {
        return frontColor;
    }
    Color getEC() {
        return color;
    }
    public static Elem getElem(String t) {
        for(Elem e: Elem.values()){
            if(e.name().equalsIgnoreCase(t)) {
                return e;
            }
        }
        return field;
    }
}

enter image description here

As you can see from the image, I need to delete the old lines but I'm not sure how. Been stuck on this for a few days. I am pretty sure the problem is I am moving the lines and circles wrong (using setLine, setFrame respectively), since I replaced the car with a rectangle and used translate() and it worked OK. Below is some minimal code to replicate this. Your help is greatly appreciated. (part 1 is the component, part 2 is the car class, and the main program just sets up a Jframe and and the part 1 component).

import java.awt.*;
import javax.swing.JComponent;
import java.util.*;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.Timer;
public class VehicleComponent extends JComponent
{
   //Car class which is an arraylist of line segments and a couple circles
 Car car;
 // The constructor initializes the car to the location 300, 300 on a Jframe
     public VehicleComponent()
  {
     car = new Car(300, 300);
      drive();
  }

//to be painted on a jframe
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
        car.draw(g2);


}

//this function will be called in timer triggered actionListener. will move the car forward by certain pixels per given seconds
public void drive()
{
        car.move();
        repaint();

}
}        

public class Car extends Vehicle
{

ArrayList<Line2D.Double> detailLines;
ArrayList<Ellipse2D.Double> tires;
private int height;
private int length;
private int velocity;


public Car(int xStartingPosition, int yStartingPosition)
{
    super(xStartingPosition, yStartingPosition);
    detailLines = new ArrayList<Line2D.Double>();
    tires = new ArrayList<Ellipse2D.Double>();
    setVelocity();
    height = 5;
    length = 14;


}
/** draws the car */
public void draw(Graphics2D g2){
    BasicStroke tireStroke = new BasicStroke(3);
    BasicStroke vehicleStroke = new BasicStroke(2);
    initializeLines();
    initializeTires();

    g2.setStroke(vehicleStroke);
    for (Line2D.Double e : detailLines){
                g2.draw(e);
    }
    g2.setStroke(tireStroke);
    for (Ellipse2D.Double e: tires){
        g2.draw(e);
    }
}



private void initializeLines()
{
    detailLines.add(new Line2D.Double((getX()-10), (getY()+10), (getX()+50), (getY()+10)));//hood
    detailLines.add(new Line2D.Double((getX() + 60), getY(), (getX()+50), (getY()+10)));//hoodtwo
    detailLines.add(new Line2D.Double((getX()+50), (getY()+10), (getX()+70), (getY()+30)));//Passengerwindshield
    detailLines.add(new Line2D.Double((getX()+70), (getY()+30), (getX() + 80), (getY() + 20)));//hood three
    detailLines.add(new Line2D.Double((getX() + 70), (getY() + 30), (getX()+100), (getY()+30)));//front
    detailLines.add(new Line2D.Double((getX()+100), (getY()+30), (getX()+100), (getY()+40)));//front two
    detailLines.add(new Line2D.Double((getX()+100), (getY()+30), (getX() + 110), (getY() + 20)));//sideLine horizontal
    detailLines.add(new Line2D.Double((getX() + 60), (getY()+20), (getX()-20), (getY() + 20)));//windowsperator
    detailLines.add(new Line2D.Double((getX()+30), (getY()+10), (getX() +30), (getY() +20)));//striketrhough
    detailLines.add(new Line2D.Double((getX()), (getY()), (getX() +60), (getY())));//roof
    detailLines.add(new Line2D.Double((getX()+60), (getY()), (getX() +80), (getY() +20)));//border top
    detailLines.add(new Line2D.Double((getX()+80), (getY()+20), (getX() +110), (getY() +20)));//border 3
    detailLines.add(new Line2D.Double((getX()+110), (getY()+20), (getX() +110), (getY() +30)));//border4
    detailLines.add(new Line2D.Double((getX()+110), (getY()+30), (getX() +100), (getY() +40)));//border5
    detailLines.add(new Line2D.Double((getX()+100), (getY()+40), (getX() - 30), (getY() +40)));//border6
    detailLines.add(new Line2D.Double((getX()-30), (getY()+40), (getX() - 30), (getY() +30)));//border7
    detailLines.add(new Line2D.Double((getX()-30), (getY()+30), (getX()), (getY())));//border8

}
private void initializeTires()
{
    tires.add(new Ellipse2D.Double((getX()-10), getY()+30, 20, 20));
    tires.add(new Ellipse2D.Double((getX()+65), getY()+30, 20, 20));
}

public void move()
{

    for(Line2D.Double e : detailLines){
        e.setLine((e.getX1()+velocity), (e.getY1()), (e.getX2()+velocity), (e.getY2()));

    }
    for(Ellipse2D.Double e : tires)
    {
        e.setFrame((e.getX()+velocity), (e.getY()), e.getWidth(), e.getHeight());
    }

}

Similar Question 2 : Component not added to JFrame

I originally planned to have no dependance on java's window component system. However, I was forced to use it because it had double buffering. So this lead to my current problem. There are two JPanel components MainMenuDisplay and GameDisplay. Whenever you click the start button I want it to get rid of the mainMenuDisplay and replace it with a new gameDisplay. Whenever I attempt to do this, however, it removes the mainMenuDisplay and doesn't add the gameDisplay even though the code is obviously running. So it just leaves the JFrame without any components to run their paintComponent method. No errors are thrown. So why doesn't it add the new GameDisplay? P.S. Please don't yell at me for using static objects.

Start.WINDOW.changeCurrentDisplay(new GameDisplay());
//Creates a new GameDisplay and passes it the JFrame
//This is inside MainMenuDisplay

//This is part of the JFrame class
Display mainMenuDisplay;
Display gameDisplay;
//Display class extends JPanel

public WindowManager() {
    super("Mountours - And - Men");

    setSize(settings.windowHeight, settings.windowWidth);
    setLocationRelativeTo(null);

    setResizable(settings.resizable);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.WHITE);

    currentDisplay = new MainMenuDisplay();
    addKeyListener(currentDisplay.input);
    addMouseListener(currentDisplay.input);
    add(currentDisplay);


    setVisible(true);
}

public void changeCurrentDisplay(Display display) {
    removeAll();//Removes the MainMenuDisplay
    mainMenuDisplay = display;
    add(display);//Attempts to add component to JFrame, but doesn't, no errors either
    addKeyListener(display.input);
    addMouseListener(display.input);
}

Here are two instances of the exact same code that draws a bunch of rectangles superimposed down a diagonal, except in the second instance there are parentheses around "i^2" for both the x and y values. I believe that the rectangles should begin at (0,0) in both cases, but in the first case they do not begin at the origin. Instead, they begin slightly off from the origin. Why is there a difference when the rectangles are added to the JFrame?

using javax.swing.JComponent javax.swing.JFrame and java.awt.Rectangle

for(int i=0;i<600;i++){
            Rectangle rect1 = new Rectangle(20*i^2, 20*i^2, 50, 100);
            g2.draw(rect1);
}

for(int i=0;i<600;i++){
            Rectangle rect1 = new Rectangle(20*(i^2), 20*(i^2), 50, 100);
            g2.draw(rect1);
}

My code is in two classes. Here is the main class:

import javax.swing.JFrame;

public class MainClass {
    public static void main(String[] args){
        JFrame window = new JFrame();
        window.setSize(600,600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DrawingComponent drawing = new DrawingComponent();
        window.add(drawing);

        window.setVisible(true);
    }
}

And here is the code for the DrawingComponent class that makes Rectangles to be added to the JFrame:

import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class DrawingComponent extends JComponent{
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        for(int i=0;i<600;i++){
            Rectangle rect1 = new Rectangle(20*i^2, 20*i^2, 50, 100);
            g2.draw(rect1);
        }
    }
}

Similar Question 4 (2 solutions) : Centering a JPanel in a JFrame

Similar Question 5 (3 solutions) : add different jcomponents to jframe in java

Similar Question 7 (1 solutions) : Why custom component is not showing up on JFrame?

Similar Question 8 (1 solutions) : JFrame - mouse click using JComponent and MouseListener

Similar Question 9 (2 solutions) : Why is my JFrame just a black window?

cc