我是新来的,并澄清我的问题与学校作业有关所以我正在寻找指导而不是代码。我正在开发一个程序,其中读取关于简单基元的信息的文本文件以在界面上产生形状和文本。可以在此处找到作业:http://www.cs.colostate.edu/~cs165/.Spring18/assignments/P2/doc/
不幸的是,我的输出与提供的样本不同,因为某些形状的颜色和填充是关闭的。
我花了很长时间试图找到问题,我能想到的最后一件事是因为我对十六进制值的经验很少,或许我解析它们是错误的。这似乎是一个延伸,我对任何其他输入开放,为什么我的输出不同。
我将在下面列出我为Triangle类编写的代码以及DrawProgram,DrawInterface和UserInterface类以供参考,这里是simplePrimitives.txt的内容:
TEXT,FF79E1,Programming is Fun,220,240,TimesRoman,15
TEXT,44B4D5,Java Rules!,240,270,TimesRoman,25
TEXT,0AFE47,Computer Science,230,300,TimesRoman,15
CIRCLE,FF7575,210,170,190,outline
CIRCLE,FFE920,180,150,40,filled
CIRCLE,6FFF64,390,150,40,filled
CIRCLE,CACAFF,420,180,40,filled
CIRCLE,BBDAFF,450,210,40,outline
CIRCLE,C06A45,450,250,40,filled
CIRCLE,4A9586,420,280,40,filled
CIRCLE,FF2626,390,310,40,filled
CIRCLE,F206FF,180,310,40,filled
CIRCLE,62FDCC,150,280,40,outline
CIRCLE,E8C6FF,120,250,40,filled
CIRCLE,800080,120,210,40,filled
CIRCLE,800080,150,180,40,filled
OVAL,62A9FF,10,50,80,30,outline
OVAL,F900F9,450,50,80,30,filled
OVAL,74FEF8,450,500,80,30,outline
OVAL,F7DE00,10,500,80,30,filled
SQUARE,99FD77,100,400,100,filled
SQUARE,FF800D,200,400,100,filled
SQUARE,FE8BF0,300,400,100,filled
TRIANGLE,FF4848,10,150,70,150,40,100,filled
TRIANGLE,6094DB,10,450,70,450,40,400,filled
TRIANGLE,4AE371,430,150,490,150,460,100,outline
TRIANGLE,DFDF00,430,450,490,450,460,400,filled
RECTANGLE,52FF20,120,20,300,100,filled
RECTANGLE,8C8CFF,125,25,290,90,filled
RECTANGLE,FFFF99,130,30,280,80,filled
RECTANGLE,C27E3A,135,35,270,70,filled
RECTANGLE,FF2DFF,140,40,250,60,filled
RECTANGLE,8ED6EA,145,45,240,50,filled
RECTANGLE,F70000,150,50,230,40,filled
RECTANGLE,0000Ce,155,55,220,30,filled
RECTANGLE,FFFFFF,160,60,210,20,filled
RECTANGLE,0000FF,165,65,200,10,filled
三角类:
public class Triangle extends Primitive {
public int color;
public int x0,y0;
public int x1,y1;
public int x2,y2;
public int[] xPoints;
public int[] yPoints;
public boolean isFilled;
public void setColor(int color) {
this.color = color;
}
public void setFilled(boolean isFilled) {
this.isFilled = isFilled;
}
public Triangle(int x0, int y0, int x1, int y1, int x2, int y2) {
this.xPoints = new int[3];
this.yPoints = new int[3];
this.xPoints[0] = x0;
this.yPoints[0] = y0;
this.xPoints[1] = x1;
this.yPoints[1] = y1;
this.xPoints[2] = x2;
this.yPoints[2] = y2;
}
public void draw(UserInterface ui) {
ui.drawPolygon(xPoints, yPoints, isFilled);
if (isFilled) {
ui.fillColor(color);
} else {
ui.lineColor(color);
}
}
}
DrawProgram:
// DrawProgram.java - Solution for drawing program
// Author: Chris Wilcox
// Date: 2/2/2017
// Class: CS165
// Email: wilcox@cs.colostate.edu
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class DrawProgram {
public static UserInterface ui;
/**
* Calls load, passing args[0] as the name of the text file containing graphics primitives.
* Calls draw to draw the primitives.
* @param args in this case, a valid filename for a Primitive file
* @throws InterruptedException thrown when graphics program fails
*/
public static void main(String[] args) throws InterruptedException {
// Drawing primitives
ArrayList<Primitive> primitives;
// Instantiate user interface
ui = new UserInterface();
// Close window
ui.open();
// Read primitives
primitives = load(args[0]);
// Draw primitives
draw(primitives);
// Wait for awhile (change if you want the drawing to be rendered for a
// longer period of time.
Thread.sleep(10000);
// Close window
ui.close();
}
/**
* Read graphics primitives from file
* @param filename the filename with primitive values
* @return a list of primitives
*/
public static ArrayList<Primitive> load(String filename) {
ArrayList<Primitive> primitives = new ArrayList<>();
try {
Scanner scan = new Scanner(new File(filename));
while (scan.hasNextLine()) {
int x0,y0,x1,y1,x2,y2,width,height,radius;
// Read and parse line
String line = scan.nextLine();
String[] fields = line.split(",");
if (fields.length == 0) continue; // discard empty
// Interpret primitives
switch (fields[0]) {
case "TEXT":
x0 = Integer.parseInt(fields[3]);
y0 = Integer.parseInt(fields[4]);
Text text = new Text(x0, y0 , fields[2]);
text.setColor(Integer.parseInt(fields[1], 16));
text.setFont(fields[5], Integer.parseInt(fields[6]));
primitives.add(text);
break;
case "SQUARE":
x0 = Integer.parseInt(fields[2]);
y0 = Integer.parseInt(fields[3]);
Square square = new Square(x0, y0, Integer.parseInt(fields[4]));
square.setColor(Integer.parseInt(fields[1], 16));
square.setFilled(fields[5].equals("filled"));
primitives.add(square);
break;
case "RECTANGLE":
x0 = Integer.parseInt(fields[2]);
y0 = Integer.parseInt(fields[3]);
width = Integer.parseInt(fields[4]);
height = Integer.parseInt(fields[5]);
Rectangle rect = new Rectangle(x0, y0, width, height);
rect.setColor(Integer.parseInt(fields[1], 16));
rect.setFilled(fields[6].equals("filled"));
primitives.add(rect);
break;
// YOUR CODE HERE - add triangle
case "TRIANGLE":
x0 =Integer.parseInt(fields[2]); //location on x-axis for the first point
y0 =Integer.parseInt(fields[3]); //location on y-axis for the first point
x1 =Integer.parseInt(fields[4]); //location on x-axis for the second point
y1 =Integer.parseInt(fields[5]); //location on y-axis for the second point
x2 =Integer.parseInt(fields[6]); //location on x-axis for the third point
y2 =Integer.parseInt(fields[7]); //location on y-axis for the third point
Triangle tri = new Triangle (x0,y0,x1,y1,x2,y2);
tri.setColor(Integer.parseInt(fields[1],16));
tri.setFilled(fields[8].equals("filled"));
primitives.add(tri);
break;
// YOUR CODE HERE - add circle
case "CIRCLE":
x0 = Integer.parseInt(fields[2]); //location on the x-axis
y0 = Integer.parseInt(fields[3]); //location on the y-axis
radius = Integer.parseInt(fields[4]); // - size
Circle circ = new Circle (x0,y0,radius);
circ.setColor(Integer.parseInt(fields[1],16));
circ.setFilled((fields[5].equals("Filled")));
primitives.add(circ);
break;
// YOUR CODE HERE - add oval
case "OVAL":
x0 = Integer.parseInt(fields[2]); //location on the x-axis
y0 = Integer.parseInt(fields[3]); //location on the y-axis
width = Integer.parseInt(fields[4]); //the width of the oval
height = Integer.parseInt(fields[5]); //the height of the oval
Oval oval = new Oval (x0,y0,width,height);
oval.setColor(Integer.parseInt(fields[1],16));
oval.setFilled((fields[6].equals("Filled")));
primitives.add(oval);
break;
}
}
scan.close();
} catch (IOException e) {
System.out.println("Cannot open file: " + filename);
}
return primitives;
}
/**
* Draw graphics primitives in arraylist
* @param primitives the list of Primitives
*/
public static void draw(ArrayList<Primitive> primitives) {
for (Primitive primitive : primitives) {
if (primitive instanceof Text)
((Text)primitive).draw(ui);
else if (primitive instanceof Square)
((Square)primitive).draw(ui);
else if (primitive instanceof Rectangle)
((Rectangle)primitive).draw(ui);
// YOUR CODE HERE - add triangle
else if (primitive instanceof Triangle)
((Triangle)primitive).draw(ui);
// YOUR CODE HERE - add circle
else if (primitive instanceof Circle)
((Circle)primitive).draw(ui);
// YOUR CODE HERE - add oval
else if (primitive instanceof Oval)
((Oval)primitive).draw(ui); }
}
}
DrawInterface:
// DrawInterface.java - Interface class for drawing program
// Author: Chris Wilcox
// Date: 2/2/2017
// Class: CS165
// Email: wilcox@cs.colostate.edu
public interface DrawInterface {
//
// Graphics control
//
/**
* Open window
*/
public abstract void open();
/**
* Close window
*/
public abstract void close();
/**
* Clear surface
*/
public abstract void clear();
//
// Graphics attributes
//
// Color selection
public abstract void lineColor(int color);
public abstract void fillColor(int color);
public abstract void textColor(int color);
/**
* Font selection
* @param fontName either Ariel or Times Roman
* @param fontSize size of the font
*/
public abstract void setFont(String fontName, int fontSize);
//
// Graphics primitives
/**
* Draw text
* @param x the location on the x-axis
* @param y the location on the y-axis
* @param string the text that will be rendered
*/
public abstract void drawText(int x, int y, String string);
/**
* Draw line
* @param x0 location on the x-axis for the first point
* @param y0 location on the y-axis for the first point
* @param x1 location on the x-axis for the second point
* @param y1 location on the y-axis for the second point
*/
public abstract void drawLine(int x0, int y0, int x1, int y1);
/**Trin
* Draw rectangle (and square)
* @param x location on the x-axis
* @param y location on the y-axis
* @param width the width of the rectangle
* @param height the height of the rectangle
* @param isFilled whether or not the rectangle is solid
*/
public abstract void drawRectangle(int x, int y, int width, int height, boolean isFilled);
/**
* Draw polygon (and triangle)
* @param xPoints the coordinates for each point on the x-axis
* @param yPoints the coordinates for each point on the y-axis
* @param isFilled whether or not the polygon is solid
*/
public abstract void drawPolygon(int[] xPoints, int[] yPoints, boolean isFilled);
/**
* Draw oval (and circle)
* @param x location on the x-axis
* @param y location on the y-axis
* @param width the width of the oval or circle
* @param height the height of the oval or circle
* @param isFilled whether or not the shape is solid
*/
public abstract void drawOval(int x, int y, int width, int height, boolean isFilled);
}
UserInterface:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
/**
* UserInterface.java - Graphics Code for drawing program
* Author: Chris Wilcox
* Date: 2/2/2017
* Class: CS165
* Email: wilcox@cs.colostate.edu
*/
public class UserInterface extends JFrame implements DrawInterface {
// User interface variables
private static final long serialVersionUID = 1L;
private JPanel topPanel; // Text panel
private JPanel bottomPanel; // Drawing panel
private JLabel textLabel;
private Font font = new Font("Serif", Font.PLAIN, 24);
private Color topColor = new Color(0x23A34A);
private Color bottomColor = new Color(0x6B8E23);
private BufferedImage surface; // Drawing surface
private Graphics2D gc; // Graphics context
private Color colorLine = Color.WHITE;
private Color colorFill = Color.WHITE;
private Color colorText = Color.WHITE;
private int gWidth; // Surface width
private int gHeight; // Surface height
private int numberPrimitives = 0; // Counts primitives
/**
* noargs constructor for UserInterface
*/
public UserInterface() {
// Platform customization
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setupTopPanel(); // Setup text area (top panel)
setupBottomPanel(); // Setup drawing area (bottom panel)
// Combine panels
add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.CENTER);
// Window setup
setupWindow();
// Initialize graphics
initializeGraphics();
}
/**
* Sets up the top panel for status area
*/
private void setupTopPanel() {
textLabel = new JLabel("Number Primitives: " + numberPrimitives);
textLabel.setFont(font);
textLabel.setForeground(new Color(0xFFFFFF));
topPanel = new JPanel();
topPanel.add(textLabel);
topPanel.setBackground(topColor);
}
/**
* Sets up the bottom panel for drawing primitives
*/
@SuppressWarnings("serial")
private void setupBottomPanel() {
// Setup for rendering
bottomPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(surface, 0, 0, null);
}
};
bottomPanel.setBackground(bottomColor);
}
/*
* Sets up the window attributes
*/
private void setupWindow() {
setSize(550, 650);
setTitle("Drawing Application");
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
/**
* Sets up the graphics context
*/
public void initializeGraphics() {
gWidth = bottomPanel.getWidth();
gHeight = bottomPanel.getHeight();
surface = new BufferedImage(gWidth, gHeight, BufferedImage.TYPE_INT_RGB);
gc = (Graphics2D) surface.getGraphics();
gc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gc.setBackground(bottomColor);
gc.clearRect(0, 0, gWidth, gHeight);
}
/**
* Opens the window
*/
public void open() {
setVisible(true);
}
/**
* Closes the window
*/
public void close() {
setVisible(false);
dispose();
}
/**
* Clears the screen
*/
public void clear() {
gc.setBackground(bottomColor);
gc.clearRect(0, 0, gWidth, gHeight);
}
/**
* Sets the line color
* @param color the line color for the shapes
*/
public void lineColor(int color) {
colorLine = new Color(color);
}
/**
* Sets the fill color
* @param color the fill color for the shapes
*/
public void fillColor(int color) {
colorFill = new Color(color);
}
/**
* Sets the text color
* @param color hexadecimal color stored as an int
*/
public void textColor(int color) {
colorText = new Color(color);
}
/**
* Sets the font
* @param fontName
* @param fontSize
*/
public void setFont(String fontName, int fontSize) {
gc.setFont(new Font(fontName, Font.BOLD, fontSize));
}
/**
* Draws text
* @param x location on the x-axis
* @param y location on the y-axis
* @param string the text to render
*/
public void drawText(int x, int y, String string) {
gc.setColor(colorText);
gc.drawChars(string.toCharArray(), 0, string.length(), x, y);
update();
}
/**
* Draws a line
* @param x0 location on the x-axis for the first point
* @param y0 location on the y-axis for the first point
* @param x1 location on the x-axis for the second point
* @param y1 location on the y-axis for the second point
*/
public void drawLine(int x0, int y0, int x1, int y1) {
gc.setColor(colorLine);
gc.drawLine(x0, y0, x1, y1);
update();
}
/**
* Draws a rectangle
* @param x location on the x-axis
* @param y location on the y-axis
* @param width width of the square or rectangle
* @param height height of the square or rectangle
* @param isFilled whether or not the shape is filled
*/
public void drawRectangle(int x, int y, int width, int height, boolean isFilled) {
if (isFilled) {
gc.setColor(colorFill);
gc.fillRect(x, y, width, height);
}
else {
gc.setColor(colorLine);
gc.drawRect(x, y, width, height);
}
update();
}
/**
* Draws a polygon
* @param xPoints the x coordinates for the polygon
* @param yPoints the y coordinates for the polygon
* @param isFilled whether or not the polygon is filled
*/
public void drawPolygon(int[] xPoints, int[] yPoints, boolean isFilled) {
if (isFilled) {
gc.setColor(colorFill);
gc.fillPolygon(xPoints, yPoints, xPoints.length);
}
else {
gc.setColor(colorLine);
gc.drawPolygon(xPoints, yPoints, xPoints.length);
}
update();
}
/**
* Draws an oval or circle
* @param x location on the x-axis
* @param y location on the y-axis
* @param width the width of the oval or circle
* @param height the height of the oval or circle
* @param isFilled whether or not the shape is filled
*/
public void drawOval(int x, int y, int width, int height, boolean isFilled) {
if (isFilled) {
gc.setColor(colorFill);
gc.fillOval(x, y, width, height);
}
else {
gc.setColor(colorLine);
gc.drawOval(x, y, width, height);
}
update();
}
/**
* Update number of primitives
*/
private void update() {
numberPrimitives++;
textLabel.setText("Number Primitives: " + numberPrimitives);
repaint();
}
}
我非常感谢任何建议,感谢您阅读我的问题。
答案 0 :(得分:0)
颜色问题是setSelection
方法顺序的逻辑错误。如果查看输出,每个形状将使用先前设置的颜色而不是正确的颜色进行绘制。您想在绘制形状之前设置颜色,因此代码如下所示:
draw
此外,您在public void draw(UserInterface ui) {
if (isFilled) {
ui.fillColor(color);
} else {
ui.lineColor(color);
}
ui.drawPolygon(xPoints, yPoints, isFilled);
}
课程的load
方法中也有一些拼写错误。在圆形和椭圆形的情况下,您已经写了DrawProgram
而不是小写"Filled"
,并且您可以告诉您,因为所有椭圆和圆都是输出中的轮廓。