Java Graphics Chappe [закрыто]JAVA

Программисты JAVA общаются здесь
Anonymous
Java Graphics Chappe [закрыто]

Сообщение Anonymous »

Когда я пытаюсь использовать функцию Drawline () для создания «треков» для моего проекта TrainsImgui, размер становится беспрепятственным и изменяется. Лучше, если вы посмотрите на изображения и проверяете комментарии в файлах. Я скопировал код Retvent этого проекта, пожалуйста, напишите, если вам нужно больше. Сделал быструю UML -диаграмму, поэтому вы понимаете код ниже:
simgui - -> paintgui - -> station & rack
simgui:

simgui:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimGUI extends JFrame implements ActionListener {

PaintGUI GUI = new PaintGUI();
JPanel inputPanel = new JPanel();
String[] stationAmounts = { "2", "3", "4", "5" };
JComboBox stationAmountComboBox = new JComboBox(stationAmounts);
JButton confirmButton = new JButton("Confirm");

SimGUI() {
Font mainFont = new Font("SansSerif", Font.BOLD, 30);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(new BorderLayout());

stationAmountComboBox.setFont(mainFont);
confirmButton.setFont(mainFont);
confirmButton.addActionListener(this);

inputPanel.setLayout(new GridLayout(1, 2));
inputPanel.add(stationAmountComboBox);
inputPanel.add(confirmButton);

this.add(GUI, BorderLayout.CENTER);
this.add(inputPanel, BorderLayout.SOUTH);

this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == confirmButton) {
int selectedStations = Integer.parseInt((String) stationAmountComboBox.getSelectedItem());

GUI.updateState(selectedStations);
}
}
}

paintgui:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class PaintGUI extends JPanel {

int windowX = 850;
int windowY = 850;

int tracksPerStation = 2;

int roundaboutAmount = 2;
int stationAmount = roundaboutAmount;

int mainroundaboutSize = 75;
int roundaboutSize = 75;
int stationSize = 50;

int roundaboutAngleInterval = 360 / roundaboutAmount;
int stationAngleInterval = 360 / stationAmount;

int constructionCircle = 265;
int subConstructionCircle = 125;

List stations = new ArrayList();
List tracks = new ArrayList();
String[] stationNames = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"
};

PaintGUI() {
this.setPreferredSize(new Dimension(windowX, windowY));
}

public void updateState(int _roundaboutAmount) {
this.roundaboutAmount = _roundaboutAmount;
this.stationAmount = _roundaboutAmount;
roundaboutAngleInterval = 360 / _roundaboutAmount;
stationAngleInterval = 360 / _roundaboutAmount;

stations.clear();
tracks.clear();

repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.GRAY);

System.out.println(tracks.size());

stations.clear();
tracks.clear();

Graphics2D g2D = (Graphics2D) g;
setCenteredCoordinate(g2D);

g2D.setStroke(new BasicStroke(7));
g2D.setPaint(Color.WHITE);

int letterIretor = 0;

for (int i = 0; i < roundaboutAmount; i++) {
double radians = Math.toRadians(roundaboutAngleInterval * i);
int x = (int) (constructionCircle * Math.cos(radians));
int y = (int) (constructionCircle * Math.sin(radians));

// g2D.drawOval(x - roundaboutSize / 2, y - roundaboutSize / 2,
// roundaboutSize,roundaboutSize);

for (int j = 0; j < tracksPerStation; j++) {
tracks.add(new Track(0, 0, x, y));
}

for (Track t : tracks) {
t.draw(g2D);
}

Graphics2D g2Dsub = (Graphics2D) g2D.create();
g2Dsub.translate(x, y);

if (roundaboutAmount % 2 == 0) {
g2Dsub.rotate(Math.toRadians(stationAngleInterval / 2));
}

for (int j = 0; j < stationAmount; j++) {
double subRadians = Math.toRadians(stationAngleInterval * j);
int xSub = (int) (subConstructionCircle * Math.cos(subRadians));
int ySub = (int) (subConstructionCircle * Math.sin(subRadians));

// I expect the out come of this line:
g2Dsub.drawLine(0, 0, xSub, ySub);

// But using these lines instead
tracks.add(new Track(0, 0, xSub, ySub));
for (Track t : tracks) {
t.draw(g2Dsub);
}

stations.add(new Station(xSub, ySub, stationSize, stationNames[letterIretor]));
letterIretor++;

for (Station s : stations) {
s.draw(g2Dsub);
}

}
g2Dsub.dispose();
}

System.out.println(tracks.size());

}

private void setCenteredCoordinate(Graphics2D g2D) {
int width = getWidth();
int height = getHeight();
g2D.translate(width / 2, height / 2);
g2D.scale(1, 1);
}
}

station:
import java.awt.*;
import javax.swing.*;

public class Station extends JComponent {

int x;
int y;
int size;
String stationName;

Station(int _x, int _y, int _size, String _stationName) {
this.x = _x;
this.y = _y;
this.size = _size;
this.stationName = _stationName;
}

public void draw(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;

g2D.setStroke(new BasicStroke(7));
g2D.setPaint(Color.WHITE);

g2D.drawOval(x - size / 2, y - size / 2,
size, size);
g2D.fillOval(x - size / 2, y - size / 2,
size, size);

g2D.setFont(new Font("Arial", Font.BOLD, 20));
g2D.setPaint(Color.BLACK);

// Draw the station name at the center of the station
FontMetrics fm = g2D.getFontMetrics();
int textWidth = fm.stringWidth(stationName);
int textHeight = fm.getAscent();
g2D.drawString(stationName, x - textWidth / 2, y + textHeight / 4);

g2D.setPaint(Color.WHITE);
}
}

трек:
import javax.swing.*;
import java.awt.*;

public class Track extends JComponent {

int startX;
int startY;
int endX;
int endY;
boolean isEmpty = true;

Track(int _startX, int _startY, int _endX, int _endY) {
this.startX = _startX;
this.startY = _startY;
this.endX = _endX;
this.endY = _endY;
}

public void draw(Graphics g) {
Graphics2D g2D = (Graphics2D) g;

g2D.setStroke(new BasicStroke(7));
g2D.setPaint(Color.WHITE);
g2D.drawLine(startX, startY, endX, endY);

}
}


Подробнее здесь: https://stackoverflow.com/questions/794 ... hics-issue

Вернуться в «JAVA»